#!/bin/sh
#
# clamsmtp init.d script for Debian
#
# This script is Public Domain.
#
# See also: clamsmtpd.conf(5), clamsmtpd(8)
#
###########################################################################

DESC="virus filtering SMTP proxy"
NAME=clamsmtpd                                  # Daemon's Name
DAEMON=/usr/sbin/${NAME}                        # Binary
CONFFILE=/etc/${NAME}.conf                      # Configuration File
SCRIPTNAME=/etc/init.d/clamsmtp

test -f ${DAEMON} || exit 0

# Pull in configuration options set in $CONFFILE.
if [ -r ${CONFFILE} ] ; then
eval `sed -e '
# Delete comments
/^#.*/d

# Delete blank lines
/^[[:space:]]*$/d

# Replace first ": " with "=" and surround with quotes
s/^\([a-zA-Z]*\):[[:space:]]*\(.*\)$/\1\="\2";/
' < ${CONFFILE}`
fi

# Now, use the variables that we care about
PIDFILE=${PidFile:="/var/run/${NAME}/${NAME}.pid"}      # PID File
RUNDIR=`dirname ${PIDFILE}` 
CUSER=${User:=clamav}                                   # clamsmtpd user
SPOOLDIR=${TempDirectory:="/var/spool/clamsmtp"}        # Spool Directory

# Init script variables
DAEMON_OPTS=""

# Override default values
test -f /etc/default/clamsmtp && . /etc/default/clamsmtp

# Start the daemon
d_start() {
        start-stop-daemon --start \
            --pidfile ${PIDFILE} --quiet --oknodo \
            --exec ${DAEMON} -- ${DAEMON_OPTS}
}

# Stop the daemon
d_stop() {
        start-stop-daemon --stop --quiet --oknodo --pidfile ${PIDFILE} \
                --exec ${DAEMON}
}

###########################################################################
# Main Body

case ${1} in
    start)
        echo -n "Starting ${DESC}: ${NAME}"
	d_start
	echo "."
        ;;
    stop)
        echo -n "Stopping ${DESC}: ${NAME}"
	d_stop
	echo "."
        ;;
    restart|force-reload)
	echo -n "Restarting ${DESC}: ${NAME}"
        d_stop
	sleep 1
        d_start
	echo "."
        ;;
    *)
        echo "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
	exit 1
        ;;
esac

exit 0
