#! @MAILBIN@/router -f
#
#  Generate default (DBTYPE) type database with 'namendbm'
#  utility with following args:
#     1  base file name
#     2  Optional input name
#

LUOPT=""
if [ "x-l" = "x$1" ]; then
  LUOPT="-l"
  shift
fi
if [ "x-u" = "x$1" ]; then
  LUOPT="-u"
  shift
fi

if [ -z "$1" ]; then
  echo "newdb [-l|-u] dbfilenamebase [inputfile]"
  exit 64
fi


BASENAME="$1"
INPUTNAME=${2:-$BASENAME}

ZCONFIG=@ZMAILERCFGFILE@
. $ZCONFIG

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

trap "rm -f $BASENAME.$$*" 0

$MAILBIN/makedb $LUOPT $DBTYPE $BASENAME.$$ < $BASENAME

case $? in
0)	;;
*)	x=$? ; echo '"'$BASENAME'" rebuilding aborted'
	rm -f $BASENAME.$$.*
	exit $x
	;;
esac

# Now install the files

case "$DBTYPE" in
btree)
	mv $BASENAME.$$.db $BASENAME.db
	;;
bhash)
	mv $BASENAME.$$.dbh $BASENAME.dbh
	;;
ndbm)
	mv $BASENAME.$$.dir $BASENAME.dir
	mv $BASENAME.$$.pag $BASENAME.pag
	;;
dbm)
	mv $BASENAME.$$.dir $BASENAME.dir
	mv $BASENAME.$$.pag $BASENAME.pag
	;;
gdbm)
	mv $BASENAME.$$.gdbm $BASENAME.gdbm
	;;
esac
