#! /bin/bash
#
#  $Id: esmtpd.in,v 1.18 2005/02/16 23:42:13 mrsam Exp $
#
#  Copyright 1998 - 2005 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.

RUNLEVEL=3
NEEDS="+network +remotefs"

.  /etc/init.d/smgl_functions

prefix="/usr"
exec_prefix="/usr"
sysconfdir="/etc/courier"
sbindir="/usr/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 $ACCESSFILE -maxprocs=$MAXDAEMONS -maxperc=$MAXPERC -maxperip=$MAXPERIP -pid=$PIDFILE"

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

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

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

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