#! /bin/bash
### BEGIN INIT INFO
# Provides:            vrfydmn
# Required-Start:      $remote_fs $syslog
# Required-Stop:       $remote_fs $syslog
# Should-Start:                $local_fs
# Should-Stop:         $local_fs
# Default-Start:       2 3 4 5
# Default-Stop:                0 1 6
# Short-Description:   Start vrfydmn daemon
# Description:         Start up vrfydmn, a anti-spoofing milter for Postfix
### END INIT INFO

. /lib/lsb/init-functions

NAME=vrfydmn
COMMAND="/usr/local/sbin/${NAME}"
PIDFILE="/var/run/vrfydmn/vrfydmn.pid"

start_daemon() {
	mkdir -p /var/run/${NAME}
	chown milter:milter /var/run/${NAME}

        echo "Starting ${NAME}"
        start-stop-daemon --start --pidfile "${PIDFILE}" \
                --exec ${COMMAND} -- -p "${PIDFILE}" -f /etc/postfix/relay_domains
        return $?
}

stop_daemon() {
        echo "Stopping ${NAME}"
	killproc -p ${PIDFILE} ${COMMAND}
        return $?
}

case "$1" in
	start)
		start_daemon
		exit $?
		;;
	stop)
		stop_daemon
		exit $?
		;;
	restart)
		$0 stop && $0 start
		;;
	reload)
		echo "${NAME}: Reloading Postfix domain maps"
		kill -SIGHUP $(cat ${PIDFILE})
		;;
	status)
		status_of_proc -p ${PIDFILE} vrfydmn vrfydmn && exit 0 || exit $?
		;;
	*)
		echo "Usage: ${NAME} {start|stop|restart|reload|status}" >&2
		;;
esac

exit 0
