#!/bin/sh
#
# openpgpkey-milter      Postfix and Sendmail message filter
#
# chkconfig: 2345 80 30
# description: Message filter that auto-encrypts email absed on OPENPGPKEY
#              DNS records
# processname: openpgpkey-milter
#

### BEGIN INIT INFO
# Provides: postfix MTA
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:
# Default-Stop: 0 1 6
# Short-Description: start and stop openpgpkey-milter
# Description: Message filter that auto-encrypts email absed on OPENPGPKEY
#              DNS records
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# for $OPTIONS
OPTIONS=""
[ -r /etc/sysconfig/openpgpkey-milter ] && . /etc/sysconfig/openpgpkey-milter

RETVAL=0
prog="openpgpkey-milter"
lockfile=/var/lock/subsys/${prog}
pidfile=/var/run/${prog}/${prog}.pid

start() {
	[ "${EUID}" != "0" ] && exit 4
	# Check that networking is up.
	[ ${NETWORKING} = "no" ] && exit 1
	echo -n $"Starting ${prog}: "
	daemon --pidfile ${pidfile} /usr/sbin/openpgpkey-milter ${OPTIONS} --pid ${pidfile} >/dev/null 2>&1 &
	touch ${lockfile}
	usleep 10000
	# Result of start is unknown when we start to background so we check
	pidof -o %PPID ${prog} >/dev/null 2>&1
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    echo_success
	else
	    echo_failure
	    rm -f ${lockfile}
	fi
        echo
	return $RETVAL
}

stop() {
	[ "${EUID}" != "0" ] && exit 4
        # Stop daemons.
	echo -n $"Shutting down ${prog}: "
	killproc -p ${pidfile} ${prog}
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f ${lockfile} ${pidfile}
	echo
	return $RETVAL
}

check() {
	pidof -o %PPID ${prog} 2>/dev/null 1>&2 && success || failure $"${prog} check"
	return $?
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload)
	stop
	start
	;;
  abort)
	abort
	;;
  flush)
	flush
	;;
  check)
	check
	;;
  status)
  	status -p ${pidfile} -l ${prog} ${prog}
	;;
  condrestart)
	stop
	start
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}"
	exit 2
esac

exit $?
