#!/bin/sh
#
# Original author: Matthew Farrellee < mafarrel <at> cs <dot> indiana <dot> edu >
# Date: Monday January 28, 2002
#
# Modified:
# Tuesday January 29, 2002: Made errors more readable.
#
# Modified by Darren Spruell < sancho <at> sancho2k <dot> net >:
# Friday January 10, 2003: Removed the stuff regarding the
# user's domain, etc. My mail host serves a single domain.
# Made some minor editions to the script.
#
# Modified by Daniel Tams: < dantams <at> sdf-eu <dot> org >:
# Saturday July 05, 2003: Fixed non-existant $VMAIL_HOME.

##################################################################
# .: START CONFIGURABLE OPTIONS SECTION :.
##################################################################

# The following paths need to be set to reflect the location of
# the following commands, in order:
# makeuserdb, userdb
MAKEUSERDB=/usr/local/sbin/makeuserdb
USERDB=/usr/local/sbin/userdb

# These variables should reflect:
# -The virtual user account's home directory
# -The qmail base directory (generally /var/qmail)
VUSER_HOME=~vmail
QMAIL_HOME=/var/qmail

##################################################################
# .: END CONFIGURABLE OPTIONS SECTION :.
##################################################################

OLD_USER=$1

# Make sure the username argument was given.
if [ -z "$OLD_USER" ]
then
echo "Usage:" `basename $0` "<username>"
exit 1
fi

# Make sure the user already exists.
if [ ! -d $VUSER_HOME/Maildir-$OLD_USER ]
then
echo "Error: Account" $OLD_USER "doesn't exist."
exit 1
fi

# Give the user a chance to back out before destroying a Maildir.
echo -n "WARNING!! Are you sure you want to delete $OLD_USER's IMAP account? [y/n]: "
read DELANS
if [ $DELANS == "y" ]; then
:
else [ $DELANS != "y" ]
echo "Exiting. $OLD_USER's IMAP account unaffected."
exit 1
fi

# Remove the user's Maildir.
echo "Removing $OLD_USER's Maildir"
rm -Rf $VUSER_HOME/Maildir-$OLD_USER
# And the .qmail files
echo "Removing $OLD_USER's .qmail files"
rm $QMAIL_HOME/alias/.qmail-$OLD_USER
rm $VUSER_HOME/.qmail-$OLD_USER

# Remove the user's entry in the userdb.
echo "Removing" $OLD_USER "from" /etc/userdb
$USERDB -f /etc/userdb $OLD_USER del

# Make sure the deletion of the user is noticed.
$MAKEUSERDB

# Finalize for user.
echo
echo " Done!!! $OLD_USER has been removed from Courier-IMAP and the Maildir deleted."
echo
exit 0
