#!/bin/sh
LOGFILE=`basename $0`.log

MU=`dirname $0`/mailutil
HACK=`dirname $0`/mailutil-hack

MBX_BAK_EXT=mbxbak
MBOX_EXT=mboxnew

MAILBOX=$1
MBX_BAK=$MAILBOX.$MBX_BAK_EXT
MBOX_NEW=$MAILBOX.$MBOX_EXT

#=========================
# Functions
#=========================
RED=`tput setf 4``tput bold`
ORANGE=`tput setf 6``tput bold`
GREEN=`tput setf 2``tput bold`
WHITE=`tput sgr0`
function fail() {
	echo "[${RED}FAIL${WHITE}]"
	echo "[FAIL]" >> $LOGFILE
	echo "*** ERROR: $1"
	echo "*** ERROR: $1" >> $LOGFILE
	echo "FAILED: $MAILBOX" >> $LOGFILE
	exit 1
}

function ok() {
	echo "[${GREEN}OK${WHITE}]"
	echo "[OK]" >> $LOGFILE
}

function step() {
	echo -n "  - $1 "
	echo -n "  - $1 " >> $LOGFILE
}

#=======================
# Check inputs
#=======================
if [[ -z "$MAILBOX" || ! -f "$MAILBOX" ]]; then
	echo "You must provide, as a parameter, the path to an MBX format mailbox."
	exit 1
	echo "FAILED: $MAILBOX" >> $LOGFILE
fi

echo "Logging to $LOGFILE"
echo -n "+ Processing $MAILBOX..."
date >> $LOGFILE
echo -n "+ Processing $MAILBOX..." >> $LOGFILE

if [[ `file -b "$MAILBOX"` != "MBX mail folder" ]]; then
	echo "[SKIPPED - not MBX]" >> $LOGFILE
	echo "[${ORANGE}SKIPPED${WHITE}] (not MBX)"
	exit 0
else
	echo
	echo >> $LOGFILE
fi

#======================
# Create backups
#======================
step "Creating backup at $MBX_BAK"

if [[ -f "$MBX_BAK" ]]; then
	fail "File with that name already exists"
fi

cp -a "$MAILBOX" "$MBX_BAK"
diff -q "$MAILBOX" "$MBX_BAK" > /dev/null
if [[ $? -ne 0 ]]; then
	fail "Backup differs from original"
else
	ok
fi


#=======================
# Migrate
#=======================
step "Migrating $MSG_TOTAL messages to $MBOX_NEW"

# Get number of messages in current (mbx) mailbox
CHECK=`$MU check "$MAILBOX" 2>>$LOGFILE`
if [[ $? -ne 0 ]]; then
	fail "Check status failed on existing mailbox"
else
	declare `echo $CHECK| sed -r -e 's/^.*([0-9]+) total.*$/MSG_TOTAL=\1/'`
fi

$HACK copy "$MAILBOX" "#driver.unix/$MBOX_NEW" >> $LOGFILE
if [[ $? -ne 0 ]]; then
	fail "Migration failed"
fi

CHECK=`$MU check "$MBOX_NEW" 2>>$LOGFILE`
if [[ $? -ne 0 ]]; then
	fail "Mailutil check of migrated box failed"
fi

declare `echo $CHECK| sed -r -e 's/^.*([0-9]+) total.*$/MSG_TOTAL2=\1/'`
#declare `echo "$CHECK" | sed -r -e 's/^([0-9]+) new message\(s\) \(([0-9]+) unseen\), ([0-9]+) total.*/MSG_NEW2=\1 MSG_UNSEEN2=\2 MSG_TOTAL2=\3/'`
if [[ $MSG_TOTAL -eq MSG_TOTAL2 ]]; then
	ok
else
	fail "Migrated folder has different message count(s)"
fi

#======================
# Fix permissions
#======================
step "Setting ownership and permissions"
chown --reference="$MAILBOX" "$MBOX_NEW"
if [[ $? -ne 0 ]]; then
	fail "Ownership change failed"
fi

chmod --reference="$MAILBOX" "$MBOX_NEW"
if [[ $? -ne 0 ]]; then
	fail "Permission change failed"
else
	ok
fi

#======================
# Move folder over
#======================
step "Moving migrated folder into place"
mv -f "$MBOX_NEW" "$MAILBOX"
if [[ $? -ne 0 ]]; then
	fail "Moving migrated folder failed"
else
	ok
fi

#======================
# Delete backup
#======================
step "Removing old MBX format backup"
rm -f "$MBX_BAK"
if [[ $? -ne 0 ]]; then
	fail "Removing backup failed"
else
	ok
fi

