#!/bin/bash
###############################################################
#  uw2dbmail.sh  (Version: 2003/04/25)                                
#
#  This is a script for migrating a UW-IMAP mail server to DBMAIL
#  This script was tested with redhat 8.0
#
#  Jacques-Beaudoin@cspi.qc.ca                                
############################################################### 
#  This script as 5 sections 
#
#  Section-1: Migrate all users-name-password to DBMAIL
#  Section-2: Migrate clear text password to DBMAIL 
#             I keep a clear text password of my users in the
#             name field of the "passwd" file if i want to
#             store this password uncrypted in dbmail I execute 
#             section-5. You probably dont want to do this. 
#  Section-3: Migrate all users-inbox and imap folders using "mb2db" 
#             or
#             Migrate all users-inbox using "formail"
#  Section-4: Delete all users from /root/passwd file 
#             (maybe you want to do that to restart a migration)
#  Section-5: Migrate all users-postfix-aliases to DBMAIL 
#
###############################################################
#  To do before executing this script                          
#                                                             
#  1: Make shure postfix and mysql are running                           
#  2: Make shure you have compile the "mb2db" utility      
#     in directory /usr/local/sbin if you migrate with "mb2db"        
#  3: Make shure you have the procmail "formail" utility      
#     in directory /usr/bin if you migrate with "formail" 
#  4: Copy this script (uw2dbmail.sh) in the /root directory 
#     and make shure this script has the execute permissions       
#  5: Copy /etc/passwd and /etc/shadow file from your existing
#     UW-IMAP mail server to the /root directory
#  6: Edit the /root/passwd file and delete all users that are    
#     not mail users (root,bin,...etc)                        
#  7: Copy all /var/spool/mail files from your existing UW-IMAP server
#     to your dbmail server in the /var/spool/mail directory 
#  8: Copy all /home/"user" dirtectory from your existing UW-IMAP server
#     to your dbmail server in the /home/home/"users" directory
#  9: If you plan to migrate postfix aliases files to dbmail 
#     Create a /w1/etc/postfix/list directory and copy all 
#     postfix aliases files in it. 
#     Delete any blank lines at the end of each 
#     aliases files because they will create bogus aliases in dbmail.
# 10: Set the variables that follows                           
# 11: To execute change to the /root directory
#     and type ./uw2dbmail.sh
###############################################################
SECTION1="NO"                    # Migrate users-name-password (YES or NO)
SECTION2="NO"                    # Migrate passwords from name field (YES or NO) 
SECTION3="NO"                    # Migrate users-inbox and folders using mb2db or migrate  
                                 # users-inbox using formail (MB2DB or FORMAIL or NO) 
SECTION4="NO"                    # Delete all users in dbmail from the 
                                 # /root/passwd file (if you want to do this)
SECTION5="NO"                    # Migrate all users-postfix-aliases (YES or NO) 
SERVER_NAME="mail1.cspi.qc.ca"   # Your server dnsname
QUOTA="20M"                      # Mail quota to give to each users (10M = 10 megabytes)
PASSWD="passwd"                  # password file name (EX: passwd , passtest)
#####################
#  Start of script  #
#####################
echo "What will be process from file $PASSWD"
echo "============================================="
echo "Migrate users-name-password = $SECTION1" 
echo "Migrate password from name field = $SECTION2" 
echo "Migrate users-inbox and folders = $SECTION3"
echo "Delete all users from /root/passwd file = $SECTION4" 
echo "Migrate all users-postfix-aliases = $SECTION5" 
echo "Pause for 10 seconds before starting"
echo "Ctrl c   to abort"
sleep 10
cd /root
#
#  Loop to process all the users in the /root/passwd file
#
while read RECORD 
do
USER=`echo $RECORD|cut -d':' -f1`   # To get $USER from /root/passwd
NAME=`echo $RECORD|cut -d':' -f5`   # To get $NAME from /root/passwd 
###############################################################
#  Section-1: Migrate all users-name-password 
###############################################################
if [ "$SECTION1" = "YES" ]; then
#
#  Create the user in DBMAIL with a temporary password "temp" 
#
/usr/local/bin/dbmail-adduser a $USER temp 0 $QUOTA $USER@$SERVER_NAME
#
#  Change password to is crypted password from the /root/shadow file   
#
/usr/local/bin/dbmail-adduser c $USER -P:/root/shadow
fi
###############################################################
#  Section-2: Migrate clear text password 
###############################################################
if [ "$SECTION2" = "YES" ]; then
# 
#  Change the password (Only if  $NAME not = to "secret")
#
if [ ! "$NAME" = "secret" ]; then
/usr/local/bin/dbmail-adduser c $USER -p $NAME
echo "Password = $NAME"
fi
fi
###############################################################
#  Section-3: Migrate users-inbox and folders with mb2db 
#             or
#             Migrate usres-inbox with formail
###############################################################
if [ "$SECTION3" = "MB2DB" ]; then
#
#  Migrate users-inbox and folders using mb2db
#
echo "*** MB2DB [ User = $USER ] ***"
#mb2db $USER                # /var/spool/mail seulement
mb2db $USER /home/home/$USER   # /var/spool/mail et /home/$user
fi
#
if [ "$SECTION3" = "FORMAIL" ]; then
#
#  Migrate users-inbox using formail
#
echo "*** FORMAIL [ User = $USER ] ***"
cat /var/spool/mail/$USER | /usr/bin/formail -s /usr/local/sbin/dbmail-smtp -m "INBOX" -u $USER
fi
###############################################################
#  Section-4: Delete all users from /root/passwd file
###############################################################
if [ "$SECTION4" = "YES" ]; then
#
#  Delete the user in DBMAIL 
#
dbmail-adduser e $USER  # Empty mailbox
dbmail-adduser c $USER -a $USER@SERVER_NAME  # Delete alias
dbmail-adduser d $USER
fi
###############################################################
#  Continue with next user 
###############################################################
done<$PASSWD
mysql -s -u root  -e "SELECT userid from dbmail.users" > codes
#
###############################################################
#  Section-5: Migrating users-postfix-aliases  
###############################################################
if [ "$SECTION5" = "YES" ]; then
#
#  Create a file named /root/temp5 containing all the files names
#  in the /w1/etc/postfix/list directory
#
dir -1 /w1/etc/postfix/list > /root/temp5
#
#  2 loops to process all users in all aliases-files 
#
while read ALIAS 
do
     while read USER
     do
     dbmail-adduser f $ALIAS@$SERVER_NAME $USER@$SERVER_NAME
     done</w1/etc/postfix/list/$ALIAS     # Continue with next user in this alias file  
done</root/temp5                 # Continue with next file-name  
fi
###################
#  End of script  #
###################
echo "Pause for 5 seconds before terminating"
sleep 5
exit 0
