#!/bin/sh

# description: start/stop sendmail milter plugin milter-greylist
#
# default: S/K40 0/K40 1/K40 2/S87

# ACCOUNT, under which the milter should run
ACCOUNT=smmsp

# configuration file
CONF=/etc/mail/milter-greylist.conf

# the socket to use
SOCKET=local:/var/milter/milter-greylist.sock

# If "yes", log verbose to syslog with mail.debug
# Make sure, you have a lot of space on your logging device, when enabling
# this option !!!
VERBOSE="no"

# Other options, you wanna add
MISCOPT=""

#
# no further changes should be required 
#
MILTERDIR=@CLIENT_BASEDIR@

case "$1" in
'start')
	echo "Starting milter-greylist sendmail filter ..."
	if [ ! -x ${MILTERDIR}/sbin/milter-greylist ]; then	
		echo "failed (unable to execute ${MILTERDIR}/sbin/milter-greylist)."
		exit 1
	fi
	if [ ! -r ${CONF} ]; then
		echo "failed (unable to read config $CONF)."
		exit 2
	fi
	PIDS=`pgrep -x -u $ACCOUNT -P 1 milter-greylist`
	if [ -n "$PIDS" ]; then
		echo "is already running."
		exit 0;
	fi
	if [ "$VERBOSE" = "yes" ]; then
		OPT="-v"
	else 
		OPT=""
	fi 
	${MILTERDIR}/sbin/milter-greylist -u $ACCOUNT -f $CONF $OPT ${MISCOPT} \
		-p ${SOCKET}
	if [ $? -ne 0 ]; then
		echo "failed."
	else
		echo "done."
	fi
	;;
'stop')
	echo "Stopping milter-greylist sendmail filter ..."
	pkill -9 -x -u $ACCOUNT -P 1 milter-greylist
	echo "done."
	;;
*)
	echo "Usage: $0 { start | stop }"
	exit 1
	;;
esac
exit 0
