#! /bin/sh
#
# This zshell script recreates the traditional aliases file database.
#
# NB!  If your kernel supports #! but the mechanism fails mysteriously when
# invoking this file, remember the total length of the command's path is
# limited to 30 characters or so.
#
ZCONFIG=/usr/local/etc/zmailer/zmailer.conf

. $ZCONFIG

PATH=/bin:/usr/bin:$PATH # ZMailer router paths are a bit restrictive..

SOPT=""
if [ "x$1" = "x-s" ]; then
 SOPT="-s"
 shift
fi

ALIASFILE=${1:-$MAILVAR/db/aliases}

#echo -n 'Do you really want to rebuild alias file "'$ALIASFILE'"? [n] '
#read answer
#case "$answer" in
#[yY]*)	;;
#*)	exit 1 ;;
#esac

if [ -f $ALIASFILE.private ]; then
	cat - $ALIASFILE $ALIASFILE.private > $ALIASFILE.all <<- EOF
		# Do not edit this file, instead edit $ALIASFILE
		# and $ALIASFILE.private.
		EOF
	ALIASES=$ALIASFILE.all
else
	ALIASES=$ALIASFILE
fi

#
# We are compiling ALIAS file, using '-a' to tell that we may
# have continuation lines lurking there...
#
$MAILBIN/newdb -l -a $SOPT $ALIASFILE $ALIASES

case $? in
0)	;;
*)	x=$? ; echo '"'$ALIASFILE'" rebuilding aborted'
	exit $x
	;;
esac

# The 'trap' takes care of removing the old dat/idx files, which triggers
# the reopen of the alias database due to the '-m' flag on the alias relation.
# (*)If the '-m' flag is not used, something like this construct should be used:

#if [ -s $POSTOFFICE/.pid.router ]; then
#	kill -CONT $(cat $POSTOFFICE/.pid.router) 2>&1 > /dev/null
#	kill -16 $(cat $POSTOFFICE/.pid.router) ||
#		echo "The router isn't running, remember to restart it!"
#fi
