#!/bin/bash
#
# Init file for SpamAssassin sendmail milter.
#
# chkconfig: - 79 21
# description: spamass-milter is a daemon that hooks into sendmail and \
#              routes email messages to spamassassin
#
# processname: spamass-milter
# config: /etc/sysconfig/spamass-milter
# pidfile: /var/run/spamass-milter.pid

### BEGIN INIT INFO
# Provides: spamass-milter
# Required-Start: $local_fs $network $syslog $named
# Required-Stop: $local_fs $network $syslog $named
# Default-Stop: 0 1 6
# Should-Start: spamd spamassassin
# Should-Stop: spamd spamassassin
# Short-Description: Start or stop SpamAssassin Milter
# Description: Spamass-milter is an add-on to sendmail that can route mail \
#       messages through SpamAssassin and mark or reject mail deemed \
#       to be spam
### END INIT INFO

# Default variables
PATH=/sbin:/bin:/usr/sbin:/usr/bin
RUN_AS_USER=sa-milt
SOCKET="/var/run/spamass-milter/spamass-milter.sock"
SOCKET_OPTIONS=""
EXTRA_FLAGS=""
SYSCONFIG="/etc/sysconfig/spamass-milter"

# If Postfix support package is installed, use a postfix-group-writable
# socket for communication with the MTA
if [ -d /var/run/spamass-milter/postfix ]; then
	SOCKET="/var/run/spamass-milter/postfix/sock"
	SOCKET_OPTIONS="-g postfix"
fi

# Read configuration
source /etc/rc.d/init.d/functions
for configfile in /etc/sysconfig/network "${SYSCONFIG}"; do
	[ -r "${configfile}" ] && source "${configfile}"
done

[ -x /usr/sbin/spamass-milter ] || exit 5

RETVAL=0
prog="spamass-milter"
desc="SpamAssassin milter"
pidfile=/var/run/spamass-milter.pid

# Fix ownership of socket directory if necessary
chown ${RUN_AS_USER} /var/run/spamass-milter

start() {
	echo -n $"Starting ${desc} (${prog}): "
	touch ${pidfile}
	chown ${RUN_AS_USER} ${pidfile}
	[ -x /sbin/restorecon ] && /sbin/restorecon ${pidfile}
	daemon --user ${RUN_AS_USER} /usr/sbin/${prog} ${SOCKET_OPTIONS} -p ${SOCKET} -P ${pidfile} -f ${EXTRA_FLAGS}
	RETVAL=$?
	echo
	if [ ${RETVAL} -eq 0 ]; then
		touch /var/lock/subsys/spamass-milter
		return 0
	else
		return 1
	fi
}

stop() {
	echo -n $"Shutting down ${desc} (${prog}): "
	rm -f /var/lock/subsys/spamass-milter
	killproc ${prog}
	RETVAL=$?
	echo
	if [ ${RETVAL} -eq 0 ]; then
		rm -f ${pidfile}
		return 0
	else
		return 1
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|force-reload)
		if [ "x`pidof spamass-milter`" != x ]; then
			stop
			sleep 2
		fi
		start
		;;
	condrestart|try-restart)
		if [ -e /var/lock/subsys/spamass-milter ]; then
			stop
			sleep 2
			start
		else
			exit 0
		fi
		;;
	status)
		status ${prog}
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
		exit 2
esac
