#!/bin/bash
# m2f - mail to fiction Dirk Essl <dirk@essl.de>

PROGNAME=`basename $0`
VERSION=0.5
MUSER=$1

# don't convert the root mailbox and exit if $1 is empty
	
if [ $1 = root ]; then
	echo "you try to convert root (stupid)"
	exit 1
fi

# MFROM : Server from which will be migrated from
MFROM=mercury

# MFROMDIR : Directory holding the actual INBOX file (with trailing slash)
MFROMDIR=/net/mercury/var/mail/

# MFROMH : Directory holding the actual mbox format mailboxes
MFROMH=/home/$MUSER/MailImap

# MTO : Server beeing migrated to
MTO=fiction

# MTODIR : Directory holding the new maildirectory (with trailing slash)
MTODIR=/var/maildir/

# POPMAPS : Server holding the NIS map for the perdition Imap Proxy
#POPMAPS=poisson
POPMAPS=tweezers

# POPMAP : filename of the NIS map.
POPMAP=/etc/yp/mailserver

################################################################################
# Don't change anything below here

# create lockfile while compiling the nis map and locking the users mailbox
if [ -f /root/m2f.lock ]; then
	echo -e "\033[1;33m##############################################"
        echo -e "\033[7;5;1;33m# # # # # # #  E R R O R  # # # # # # # # #\\033[0m"
        echo -e "\033[1;33man instance of m2f is running"
        echo -e "\033[1;33mplease wait until the NIS map is compiled"
        echo -e "\033[1;33mif you yhink this is an error, remove the file m2f.lock"
        echo -e "\033[1;33m##############################################"
        exit 1
fi

id $MUSER
errlv=$?
if [ $errlv != "0" ]; then
        echo -e "\033[7;5;1;33muser $MUSER is not in ESO domain\\033[0m"
        exit 1
fi

# check if user is currently hosted in kila or sc6
KILA=`ssh root@mercury grep $MUSER /etc/mail/mailusers.kila`

if [ "X$KILA" != "X" ]; then
	echo "$MUSER is active in kila!"
	exit 1
fi

SC6=`ssh root@mercury grep $MUSER /etc/mail/mailusers.sc6`

if [ "X$KILA" != "X" ]; then
	echo "$MUSER is active in sc6!"
	exit 1
fi

# check if user has sbd files by grep .mailboxlist

NO_SBD=`su - $MUSER -c 'cat ~/.mailboxlist' | egrep '\.sbd$|\.sbd/'|  wc -l`
echo -e $NO_SBD "sbd-files inside .mailboxlist"


if [ $NO_SBD != "0" ]; then
	echo -e "\033[1;33m##############################################"
        echo -e "\033[7;5;1;33m# # # # # # #  E R R O R  # # # # # # # # #\\033[0m"
        echo -e "\033[1;33mUser's .mailboxlist mentiones *.sbd-files"
        echo -e "\033[1;33mplease postpone converting this user"
        
        echo -e "\033[1;33m##############################################"
        exit 1
fi

# check if user has sbd files by find

NO_SBD=`su - $MUSER -c 'find ~/MailImap/ -name "*.sbd"' | egrep 'sbd$'|  wc -l`
echo -e $NO_SBD "sbd-files inside MailImap"


if [ $NO_SBD != "0" ]; then
	echo -e "\033[1;33m##############################################"
        echo -e "\033[7;5;1;33m# # # # # # #  E R R O R  # # # # # # # # #\\033[0m"
        echo -e "\033[1;33mUser has one or more *.sbd-files inside MailImap-Folder"
        echo -e "\033[1;33mplease postpone converting this user"
        
        echo -e "\033[1;33m##############################################"
        exit 1
fi

# check if user is logged on

IMAPDAEMONS=`ssh root@mercury "ps -elf" | grep $MUSER | egrep 'imapd$|ipop3d$' | grep -v root | wc -l`
echo $IMAPDAEMONS " imap- or pop3-Daemons on Mercury for " $MUSER

if [ $IMAPDAEMONS != "0" ]; then
	echo -e "\033[1;33m##############################################"
        echo -e "\033[7;5;1;33m# # # # # # #  E R R O R  # # # # # # # # #\\033[0m"
        echo -e "\033[1;33mUser is still logged on to Mercury"
        echo -e "\033[1;33mCall this user by phone"
        ssh root@mercury "/usr/local/bin/phone $MUSER"
  #      PHONE=`ssh root@mercury "/usr/local/bin/phone $MUSER"`
   #     echo $PHONE
        exit 1
fi


if [ -f /root/convert/$MUSER ]; then
	echo -e "\033[1;33m##############################################"
	echo -e "\033[7;5;1;33m# # # # # # #  W A R N I N G # # # # # # # # #\\033[0m"
	echo -e "\033[1;33m$MUSER Already converted"
	echo -e "\033[1;33mif really want to migrate $MUSER"
	echo -e "\033[1;33magain, remove the file '/root/convert/$MUSER'"
	echo -e "\033[1;33m##############################################"
	exit 1
fi

if [ -f /home/$MUSER/.procmailrc ]; then
	echo -e "\033[1;33m##############################################"
	echo -e "\033[7;5;1;33m# # # # # # #  W A R N I N G # # # # # # # # #\\033[0m"
	echo -e "\033[1;33m$MUSER has a procmail script"
	echo -e "\033[1;33mpress 1 to show the content of the script"
	echo -e "\033[1;33mpress 2 to continue"
	echo -e "\033[1;33mpress 3 to exit"
	echo -e "\033[1;33m##############################################\033[0m"
	read option
	
	case $option in	
		1)	cat /home/$MUSER/.procmailrc
			exit 1
		  ;;
		2)
		  ;;
		3)	exit 1
		  ;;
		*) echo -e "\033[1;33mthis is not an option, script will exit";exit 1	
		  ;;
	esac
fi



touch /root/m2f.lock
touch /root/convert/$MUSER

echo "migrating $MUSER"
echo "# lock the users mailbox"
touch $MFROMDIR/$MUSER.lock

echo "# Change the mailusertable" 
ssh root@mercury "echo $MUSER >> /etc/mail/mailusers.fiction"
echo "# restart sendmail"
ssh root@mercury pkill -HUP sendmail

echo "# point popmap to new server and compile new map"
ssh root@$POPMAPS "echo $MUSER  fiction.hq.eso.org >> /etc/yp/mailserver"
ssh root@$POPMAPS  make -C /var/yp/
rm -f /root/m2f.lock


echo "# convert the mbox files"
mkdir $MTODIR/$MUSER
chown $MUSER $MTODIR/$MUSER
su $MUSER -c "/usr/bin/mb2md -s $MFROMDIR$MUSER -R -d $MTODIR/$MUSER/"
su $MUSER -c "/usr/bin/mb2md -s $MFROMH -R -d $MTODIR$MUSER/"

# convert the mbox file in the users home if exists
if [ -f /home/$MUSER/mbox ]; then
	su $MUSER -c "/usr/bin/mb2md -s /home/$MUSER/mbox -R -d $MTODIR/$MUSER/"
fi

# read the .mailboxlist file in the homedir and figure out if there are additional folders subscribed
if [ -f /home/$MUSER/.mailboxlist ]; then
	cat  /home/$MUSER/.mailboxlist | grep -i -v "^inbox$"| grep -v "^MailImap/" | grep -v "^~/MailImap" |grep -v "/home/$MUSER/MailImap/"| while read line
	do
   		echo sorce mbox file: /home/$MUSER/`echo $line| sed -e "s/^~//g"|sed -e "s/\ /\\\\\ /g"`
   		TPATH=`echo $line| sed -e 's/^~//'|sed -e 's/\ /\\\\\ /g'`
   		TARGET=`basename "$TPATH"`
    		su $MUSER -c "/usr/bin/mb2md -s /home/$MUSER/`echo $line| sed -e "s/^~//"|sed -e "s/\ /\\\\\ /g"` -R -d $MTODIR/$MUSER/.$TARGET"
	done
fi

# if theres an mbx style INBOX file, convert it to mbox and then to maildir
if [ -f /home/$MUSER/INBOX ]; then
	ssh sonic su - $MUSER -c '"mailutil -verbose -debug" copy INBOX fictionmbox'
	su - $MUSER -c "/usr/bin/mb2md -s /home/$MUSER/fictionmbox -R -d $MTODIR/$MUSER/"
	su - $MUSER -c "rm -f fictionmbox"
fi

echo "# create subscriptions file"
cd $MTODIR$MUSER/
#su $MUSER -c "find /var/maildir/$MUSER -type d -name '\.*' | sed -e 's/\.\///' | sed -e 's/^.//'" > $MTODIR$MUSER/subscriptions
su $MUSER -c "find /var/maildir/$MUSER -type d -name '\.*' | sed -e 's/\.\///' | sed -e 's/^\/var\/maildir\/$MUSER\/\.//'" > $MTODIR$MUSER/subscriptions
chown $MUSER $MTODIR$MUSER/subscriptions

echo "# remove lock file"
rm -f $MFROMDIR/$MUSER.lock

echo "move inbox to $MUSER.old"
su $MUSER -c "mv /net/mercury/var/mail/$MUSER /net/mercury/var/mail/$MUSER.old"

echo "##############################################"
echo "$MUSER has been converted"

if [ -f /home/$MUSER/.forward ]; then
	echo -e "\033[7;5;1;33mplease have a look at the .forward file:	   "
	echo -e "\033[1;33m--"
	cat /home/$MUSER/.forward
	echo -e "\033[1;33m--"
fi

echo "have a nice day!"
