#! /bin/sh

set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=proxsmtp
SCRIPTNAME=/etc/init.d/$NAME
NAME2=proxsmtpd
DAEMON=/usr/sbin/$NAME2

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0


d_start() {
    conf="$1"
    inst=`basename $conf .conf`
    pidfile=/var/run/$NAME/$inst.pid

    echo -n "Starting $NAME ($inst)"
    start-stop-daemon --start --quiet --pidfile $pidfile \
        --exec $DAEMON -- -f $conf -p $pidfile
    echo "."
}

d_stop() {
    conf="$1"
    inst=`basename $conf .conf`
    pidfile=/var/run/$NAME/$inst.pid

    echo -n "Stoping $NAME ($inst)"
    start-stop-daemon --stop --quiet --pidfile $pidfile --name $NAME2
    rm -f $pidfile
    echo "."
}

d_all() {
    op="$1"
    for conf in `ls -B /etc/$NAME/*.conf 2>/dev/null`
    do
        $op $conf
    done
}

case "$1" in
    start)
        d_all d_start
	;;
    stop)
        d_all d_stop
        ;;
    force-reload|restart)
        d_all d_stop
        d_all d_start
	;;
    *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
