#! /bin/sh
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#
# This file was automatically customized by dh-make on Fri,  4 Jan 2002 20:05:18 +0100

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=MailScanner
DAEMON=/usr/sbin/$NAME
DESC="mail spam/virus scanner"
CONFFILE=/etc/MailScanner/MailScanner.conf

test -f $DAEMON || exit 0

#set -e

run_mailscanner=0
run_nice=0
if [ -f /etc/default/mailscanner ]; then
    . /etc/default/mailscanner
fi

if [ $run_mailscanner = 0 ]; then
    if [ -z "$satisfy_nitpicking_on_removal" ]; then
	cat <<-EOF
	
	Please edit the file /etc/MailScanner/MailScanner.conf according to
	your needs.  Then configure sendmail or exim for use with mailscanner.
	
	After you are done you will have to edit /etc/default/mailscanner as
	well. There you will have to set the variable run_mailscanner to 1,
	and then type "/etc/init.d/mailscanner start" to start the mailscanner
	daemon.
	
	EOF
    fi
    exit 0
fi

# sanity check for permissions
check_dir()
{
    if [ ! -d $1 ]; then
	echo >&2 "$0: directory $1: does not exist"
	exit 1
    fi
    actual="$(stat -c %U $1)"
    if [ "$actual" != "$2" ]; then
	echo >&2 "$0: directory $1: wrong owner (expected $2 but is $actual)"
	exit 1
    fi
    actual="$(stat -c %G $1)"
    if [ "$actual" != "$3" ]; then
	echo >&2 "$0: directory $1: wrong group (expected $3 but is $actual)"
	exit 1
    fi
}

user=$(echo $(awk -F= '/^Run As User/ {print $2; exit}' $CONFFILE))
group=$(echo $(awk -F= '/^Run As Group/ {print $2; exit}' $CONFFILE))

check_dir /var/spool/MailScanner       ${user:-mail} ${group:-mail}
check_dir /var/lib/MailScanner         ${user:-mail} ${group:-mail}
check_dir /var/run/MailScanner         ${user:-mail} ${group:-mail}
check_dir /var/lock/subsys/MailScanner ${user:-mail} ${group:-mail}

case "$1" in
    start)
	echo -n "Starting $DESC: "
	/usr/bin/nice -$run_nice \
	start-stop-daemon --start --quiet \
		--exec $DAEMON >/dev/null 2>&1
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    touch /var/lock/subsys/mailscanner
	fi
	echo "$NAME."
	;;
    stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet \
		--name $NAME --quiet >/dev/null 2>&1
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    rm -f /var/lock/subsys/mailscanner
	fi
	if ps axww | grep -i $DAEMON | grep -qv grep; then
	    echo -n "(waiting"
	    for i in 1 2 3 4 5 6 7 8 9 10; do
		sleep $i
		if ! ps axww | grep -i $DAEMON | grep -qv grep; then
		    break;
		fi
		echo -n .
	    done
	    echo -n ") "
	fi
	rm -f /var/run/$NAME/*
	echo "$NAME."
	;;
    restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	$0 stop
	$0 start
	;;
    *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
