#!/bin/bash
###############################################################
#  Thanks to hhtp://www.birdbrained.org/dbmail.php for the    
#  info about importing messages from mbox to DBMAIL          
###############################################################
#  uw2dbmail.sh  (Version: 2002/12/09)                                
#
#  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 all users-inbox to DBMAIL
#  Section-3: Migrate all users-imap-folders to DBMAIL 
#             SECTION-3 NEADS A MODIFIED DBMAIL-SMTP TO WORK
#             IM WORKING ON IT. 
#  Section-4: Migrate all users-postfix-aliases to DBMAIL 
#  Section-5: 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. 
###############################################################
#  To do before executing this script                          
#                                                             
#  1: Make shure postfix and mysql are running                           
#  2: Make shure you have the procmail "formail" utility      
#     in directory /usr/bin                                   
#  3: Copy this script (uw2dbmail.sh) in the /root directory 
#     and make shure this script has the execute permissions       
#  4: Create a $WORK directory (I use /work). You will copy all
#     the mail files from your existing uw-imap mail server under $WORK
#  5: Create a $WORK/mail subdirectory  
#  6: Create a $WORK/home subdirectory
#  7: Create a $WORK/alisases subdirectory
#  8: Copy /etc/passwd and /etc/shadow file from your existing
#     UW-IMAP mail server to the $WORK directory (/work)
#  9: Edit the $WORK/passwd file and delete all users that are    
#     not mail users (root,bin,...etc)                        
# 10: Copy all /var/spool/mail files from your existing UW-IMAP
#     server to the $WORK/mail directory 
# 11: Copy all /home/"users" directory under the $WORK/home directory
# 12: Copy all postfix aliases files in the $WORK/aliases 
#     directory and delete any blank lines at the end of each 
#     aliases files because they will create bogus aliases in dbmail.
# 13: Set the variables that follows                           
# 14: To execute change to the /root directory
#     and type ./uw2dbmail.sh
###############################################################
SECTION1="NO"                     # YES or NO Migrate all users-name-password 
SECTION2="NO"                     # YES or NO Migrate all users-inbox
SECTION3="NO"                     # YES or NO Migrate all users-imap-folders
SECTION4="NO"                     # YES or NO Migrate all users-postfix-aliases  
SECTION5="NO"                     # YES or NO Migrate password from name field 
SERVER_NAME="mail.your-dnsname"   # Your server dnsname
WORK="/work"                      # Your work directory
QUOTA="10M"                       # Mail quota to give to each users (10M = 10 megabytes)
#####################
#  Start of script  #
#####################
echo "What will be process"
echo "============================================="
echo "Migrate all users-name-password = $SECTION1" 
echo "Migrate all users-inbox = $SECTION2" 
echo "Migrate all users-imap-folders = $SECTION3"
echo "Migrate all users-postfix-aliases = $SECTION4" 
echo "Migrate clear text password = $SECTION5" 
echo "Pause for 5 seconds before starting"
sleep 5
###############################################################
#  Section-1: Migrate all users-name-password 
###############################################################
if [ "$SECTION1" = "YES" ]; then
cd $WORK
#
#  Loop to process all the users in the $WORK/passwd file
#
while read RECORD 
do
USER=`echo $RECORD|cut -d':' -f1`   # To get $USER from $WORK/passwd
NAME=`echo $RECORD|cut -d':' -f5`   # To get $NAME from $WORK/passwd 
#
#  Create the user in DBMAIL with a temporary password "temp" 
#
dbmail-adduser quiet a $USER temp 0 $QUOTA $USER@$SERVER_NAME
#
#  Change password to is crypted password from the $WORK/shadow file   
#
dbmail-adduser c $USER -P:$WORK/shadow
#
#  Continue with next user 
#
done<passwd
fi
###############################################################
#  Section-2: Migrate all users-inbox 
###############################################################
if [ "$SECTION2" = "YES" ]; then
cd $WORK
#
#  Loop to process all the user in the $WORK/passwd file
#
while read RECORD 
do
USER=`echo $RECORD|cut -d':' -f1`   # To get $USER from $WORK/passwd
NAME=`echo $RECORD|cut -d':' -f5`   # To get $NAME from $WORK/passwd 
#
#  Transfert this $USER inbox
#
echo "Migrating inbox [ User = $USER ]"
cat $WORK/mail/$USER | /usr/bin/formail -s /usr/local/sbin/dbmail-smtp -m "INBOX" -u $USER
#
#  Continue with next user 
#
done<passwd
fi
###############################################################################
#  Section-3: Migrate all users-imap-folders  
###############################################################################
if [ "$SECTION3" = "YES" ]; then
cd $WORK
#
#  Loop to process all the users in the $WORK/passwd file
#
while read RECORD 
do
USER=`echo $RECORD|cut -d':' -f1`   # To get $USER from $WORK/passwd
NAME=`echo $RECORD|cut -d':' -f5`   # To get $NAME from $WORK/passwd 
#
#  Create a file named $WORK/files containing all the files names
#  in the $WORK/home/$USER directory
#
dir -1 /$WORK/home/$USER > $WORK/files
#
#  Loop to process all files in the $USER home directory 
#
while read FOLDER 
do
echo "Migrating imap folder [ User = $USER / Folder = $FOLDER ]"
cat $WORK/home/$USER/$FOLDER | /usr/bin/formail -s /usr/local/sbin/dbmail-smtp -m "$FOLDER" -u $USER
done<$WORK/files    # Continue with next folder 
#
#  Continue with next user 
#
done<passwd 
fi
###############################################################################
#  Section-4: Migrating users-postfix-aliases  
###############################################################################
if [ "$SECTION4" = "YES" ]; then
cd $WORK
#
#  Create a file named $WORK/files containing all the files names
#  in the $WORK/aliases directory
#
dir -1 $WORK/aliases > $WORK/files
#
#  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<$WORK/aliases/$ALIAS  # Continue with next user in this alias file  
done<$WORK/files                # Continue with next file-name  
fi
###############################################################
#  Section-5: Migrate clear text password 
###############################################################
if [ "$SECTION5" = "YES" ]; then
cd $WORK
#
#  Loop to process all the users in the $WORK/passwd file
#
while read RECORD 
do
USER=`echo $RECORD|cut -d':' -f1`   # To get $USER from $WORK/passwd
NAME=`echo $RECORD|cut -d':' -f5`   # To get $NAME from $WORK/passwd 
# 
#  Change the password (Only if  $NAME not = to "secret")
#
if [ ! "$NAME" = "secret" ]; then
dbmail-adduser c $USER -p $NAME
echo "Password = $NAME"
fi
#
#  Continue with next user 
#
done<passwd
fi
#
###################
#  End of script  #
###################
echo "Pause for 5 seconds before terminating"
sleep 5
exit 0
