#! /bin/sh
#
# This shell script recreates the traditional aliases file database.
#
#  THIS IS IN PRINCIPLE SUPERCEDED BY THE  'zmailer newdb'  MECHANISM!
#
# IF  dbases.conf  file defines default location 'aliases' file with
# default ('$DBTYPE') type, this script can be used to recompile it.
#
# Supplying file-path argument to this script, this can be used to
# compile other aliases(5)-format files.
#

if [ -z "$ZCONFIG" ] ; then
    ZCONFIG=/usr/local/etc/zmailer/zmailer.conf
fi

. $ZCONFIG
export 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
