#!/bin/bash
###############################################################
#  uw2dbmail.sh  (Version: 2004/11/24)                                
#
#  This is a script for migrating a UW-IMAP mail server to DBMAIL
#  This script was tested with mandrake 9.2
#
#  Jacques-Beaudoin@cspi.qc.ca                                
############################################################### 
#  This script as 6 sections 
#
#  Section-0: Delete all users from /root/passwd file 
#             (maybe you want to do that to restart a migration)
#  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. 
#             You probably dont want to do this. 
#  Section-3: Migrate all users-inbox using "formail"
#  Section-4: Migrate all users-inbox and imap folders using "mb2db" 
#  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/bin 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 of your new dbmail mail server.
#  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. 
# 10: Set the variables that follows                           
# 11: To execute change to the /root directory
#     and type ./uw2dbmail.sh
###############################################################

SECTION0="NO"                   # Delete all users in dbmail from the /root/passwd file (YES or NO)
                                # (maybe you want to do this to resart all over)
		
SECTION1="NO"                   # Migrate users-name-password (YES or NO)

SECTION2="NO"                   # Migrate passwords from name field (YES or NO) 
                                # You probably dont want to do this

SECTION3="NO"                   # Migrate users-inbox and folders using formail (YES or NO)
		
SECTION4="NO"                   # Migrate all users-postfix-aliases (YES or NO) 

SERVER_NAME="cspi.qc.ca"        # Your mail dns name 

QUOTA="40M"                     # Mail quota to give to each users (40M = 40 megabytes)

PASSWD="passwd"                 # Linux password file name (Default: is the passwd in /root)

LIB="/usr/local/bin"            # Directory of the dbmail binaries programs  

#####################
#  Start of script  #
#####################
cd /root
START_TIME=`date '+%y/%m/%d - %H.%M.%S'`
echo "$START_TIME : START TIME"
TOTAL=0
#
#  To know how many users i have to migrate 
#
wc -l $PASSWD > temp5              # How many lines in passwd file
TOTAL_USERS=`sed 's/  //g' temp5`  # Take ou spaces in temp5 and put total users in TOTAL_USERS

echo "$TOTAL_USERS users will be process from file $PASSWD"
echo "=============================================================="
echo "Delete all users from /root/passwd file = $SECTION0"
echo "Migrate users-name-password = $SECTION1" 
echo "Migrate password from name field = $SECTION2" 
echo "Migrate users-inbox and folders using formail = $SECTION3"
echo "Migrate users-inbox and folders using mb2db = $SECTION4"
echo "Migrate all users-postfix-aliases = $SECTION4" 
echo "Pause for 10 seconds before starting"
echo "Ctrl c   to abort"
sleep 5


##############################################################
#  Loop to process all the users in the /root/passwd file
##############################################################
if [ ! "$SECTION0-$SECTION1-$SECTION2-$SECTION3-$SECTION4" = "NO-NO-NO-NO-NO" ]; then
while read RECORD 
do
TOTAL=`expr $TOTAL + 1`
echo "***                                          Migrating $TOTAL of $TOTAL_USERS"
USER=`echo $RECORD|cut -d':' -f1`   # To get $USER from /root/passwd
NAME=`echo $RECORD|cut -d':' -f5`   # To get $NAME from /root/passwd 
PW=`echo $NAME|cut -d',' -f1`       # To get the password from $NAME 


###############################################################
#  Section-0: Delete all users from /root/passwd file
###############################################################
if [ "$SECTION0" = "YES" ]; then
#
#  Delete the user in DBMAIL 
#
   mysql -s -u root  -e "SELECT alias from dbmail.aliases WHERE deliver_to='$USER@$SERVEUR_MAIL'" > temp1
   while read RECORD                      
   do
     dbmail-adduser x $RECORD $USER@$SERVEUR_MAIL | grep "Removing"      # Delete deliver_to  
   done<temp1                                                   
$LIB/dbmail-adduser e $USER                                # Delete mailboxes
$LIB/dbmail-adduser quiet c $USER -a $USER@$SERVEUR_MAIL   # Delete Alias
$LIB/dbmail-adduser d $USER                                # Delete User
fi


###############################################################
#  Section-1: Migrate all users-name-password 
###############################################################
if [ "$SECTION1" = "YES" ]; then
#
#  Create the user in DBMAIL with a temporary password "temp" 
#
$LIB/dbmail-adduser a $USER temp 0 $QUOTA $USER@$SERVER_NAME
$LIB/dbmail-adduser c $USER -q $QUOTA   
#
#  Change password to is crypted password from the /root/shadow file   
#
$LIB/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 [ ! "$PW" = "secret" ]; then
$LIB/dbmail-adduser c $USER -p $PW
echo "Password = $PW"
fi
fi


###############################################################
#  Section-3: Migrate users-inbox and folders with formail 
###############################################################
if [ "$SECTION3" = "YES" ]; then
echo "*** FORMAIL  User = $USER   Folder = INBOX   Migrating $TOTAL of $TOTAL_USERS"
cat /var/spool/mail/$USER | /usr/bin/formail -n 10 -s $LIB/dbmail-smtp -m "INBOX" -u $USER
#
#  Loop to migrate all user folder using formail
#
dir -1 /home/home/$USER > /root/temp5
while read FOLDER 
do
echo "*** FORMAIL [ User = $USER ] [ Folder = $FOLDER ]   ***"
cat /home/home/$USER/$FOLDER | /usr/bin/formail -n 10 -s $LIB/dbmail-smtp -m "$FOLDER" -u $USER 
done</root/temp5    
fi

###############################################################
#  Section-4: Migrate users-inbox and folders with mb2db 
###############################################################
if [ "$SECTION3" = "YES" ]; then
#
#  Migrate users-inbox and folders using mb2db
#
echo "*** MB2DB [ User = $USER ] ***"
#LIB/mb2db $USER                    # /var/spool/mail ONLY
$LIB/mb2db $USER /home/home/$USER   # /var/spool/mail and /home/$user
fi

###############################################################
#  Continue with next user 
###############################################################
done<$PASSWD
fi


###############################################################
#  You may comment this lise 
###############################################################
mysql -s -u root  -e "SELECT userid from dbmail.users" > codes   # List all users in files 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
       if [ ! "$USER@$SERVER_NAME" = "@$SERVER_NAME" ]; then    # Only if not a blank line
       $LIB/dbmail-adduser f $ALIAS@$SERVER_NAME $USER@$SERVER_NAME
       fi
     done</w1/etc/postfix/list/$ALIAS     # Continue with next user in this alias file  
done</root/temp5                          # Continue with next file-name  
fi

echo "###################"
echo "#  End of script  #"
echo "###################"
STOP_TIME=`date '+%y/%m/%d - %H.%M.%S'`
echo "$START_TIME : START TIME"
echo "$STOP_TIME : STOP TIME"
exit 0
