#!/bin/sh
# $Id: rc-redhat.sh.in,v 1.7 2006/08/20 05:20:51 manu Exp $
#  init file for milter-greylist
#
# chkconfig: - 79 21
# description: Milter Greylist Daemon
#
# processname: milter-greylist
# config: /etc/mail/greylist.conf
# pidfile: /var/run/milter-greylist.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# source function library
. /etc/init.d/functions

PIDFILE=/var/run/milter-greylist.pid
LOCKFILE=/var/run/milter-greylist
SOCKET=/var/lib/milter-greylist/milter-greylist.sock
USER=grmilter
PROG=/usr/sbin/milter-greylist
OPTIONS="-P $PIDFILE -p $SOCKET"

if [ -f /etc/sysconfig/milter-greylist ]
then
    . /etc/sysconfig/milter-greylist
fi

RETVAL=0

start() {
        start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user $USER -- $PROG $OPTIONS
        RETVAL=$?
        return $RETVAL
}

stop() {
        stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user $USER -- milter-greylist
	RETVAL=$?
        return $RETVAL
}


restart() {
	stop
	start
}

conftest() {
	action "Checking configuration sanity for milter-greylist: " milter-greylist -c
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		if conftest; then
			start
			RETVAL=$?
			if [ $RETVAL -eq 0 ]; then
				[ -S "$SOCKET" ] && chmod 660 "$SOCKET" ||:
			fi
		else
			RETVAL=1
		fi
		;;
	stop)
		stop
		;;
	restart)
		if conftest; then
			restart
			RETVAL=$?
		else
			RETVAL=1
		fi
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user $USER -- milter-greylist
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
