#! /bin/sh
#
#
#  Copyright 1998 - 2009 Double Precision, Inc.  See COPYING for
#  distribution information.
#
#  This is a simple script to start/stop the smtp port daemon, courieresmtpd.
#
#  Argument "start" starts the daemon.
#  Argument "stop" stops the daemon (just the listening daemon).
#  Argument "restart" rereads the smtpaccess.dat file.

prefix="/usr/pkg"
exec_prefix="/usr/pkg"
sysconfdir="/usr/pkg/etc/courier"
sbindir="/usr/pkg/sbin"

cd ${prefix} || exit 1
case `basename $0` in
esmtpd)
	configfiles="${sysconfdir}/esmtpd"
	. ${sysconfdir}/esmtpd
	;;
esmtpd-msa)
	configfiles="${sysconfdir}/esmtpd ${sysconfdir}/esmtpd-msa"
	. ${sysconfdir}/esmtpd
	. ${sysconfdir}/esmtpd-msa
	;;
*)
	echo "Must be invoked as esmtpd or esmtpd-msa" >&2
	exit 1
	;;
esac

case $1 in
start)
	;;
stop)
	${sbindir}/couriertcpd -pid=$PIDFILE -stop
	exit 0
	;;
restart)
	${sbindir}/couriertcpd -pid=$PIDFILE -restart
	exit 0
	;;
*)
	exit 0
	;;
esac

if test "${ACCESSFILE}" != ""
then
	if test ! -f "$ACCESSFILE.dat"
	then
		ACCESSFILE=""
	else
		ACCESSFILE="-access=${ACCESSFILE}.dat"
	fi
fi

if test "$ADDRESS" != ""
then
	ADDRESS="-address=$ADDRESS"
fi

TCPDOPTS="$TCPDOPTS -user=$MAILUSER -group=$MAILGROUP $ADDRESS $BLACKLISTS $DROP $ACCESSFILE -maxprocs=$MAXDAEMONS -maxperc=$MAXPERC -maxperip=$MAXPERIP -pid=$PIDFILE"

(
	echo prefix=${prefix}
	echo exec_prefix=${exec_prefix}
	echo sysconfdir=${sysconfdir}
	echo sbindir=${sbindir}

	echo set -a
	for f in $configfiles
	do
		echo . $f
	done

	echo "ulimit -d $ULIMIT"
	echo ${sbindir}/couriertcpd $TCPDOPTS $PORT \
		${sbindir}/courieresmtpd'>/dev/null 2>&1 </dev/null'

) | /usr/bin/env -i /bin/sh
exit 0
