#!/bin/sh

### BEGIN INIT INFO
# Provides:          postfix-cluebringer
# Required-Start:    $local_fs $remote_fs $syslog $named $network
# Required-Stop:     $local_fs $remote_fs $syslog $named $network
# Should-Start:      mysql postgresql-8.3 postgresql-8.4
# Should-Stop:       mysql postgresql-8.3 postgresql-8.4
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starting postfix policy daemon
# Description:       cluebringer is the postfix policy daemon v2.0
### END INIT INFO

NAME="cbpolicyd"
DAEMON=/usr/sbin/cbpolicyd
LIB=/usr/lib/postfix-cluebringer
PIDDIR=/var/run/cluebringer
PIDFILE=${PIDDIR}/$NAME.pid
RUN_CLUEBRINGER="NO"
CONFIG_FILE=""


[ ! -r /etc/default/postfix-cluebringer ] || . /etc/default/postfix-cluebringer

. /lib/lsb/init-functions

PATH=/bin:/usr/bin:/sbin:/usr/sbin

if [ "$CHUID" = "" ]; then 
	CHUID="root"
fi

# Check for the existence of the configuration file
if [ ! -f $CONFIG_FILE ]; then
	log_failure_msg "No config file found."
	exit 0;
fi

# Check that the default has been changed
if [ "$RUN_CLUEBRINGER" = "NO" ]; then
	log_failure_msg "Please edit /etc/default/cluebringer to run postfix-cluebringer."
	exit 0;
fi

[ -x $DAEMON ] || exit 0

start() {

	if [ "$DAEMON_OPTIONS" = "" ]; then
		DAEMON_OPTIONS="--config=$CONFIG_FILE"
	fi
	# create run dirs:
	if [ ! -d ${PIDDIR} ]; then
		mkdir ${PIDDIR}
	fi
	chown -R ${CHUID}:www-data ${PIDDIR}
	chmod -R u+rwx,g-w,o= ${PIDDIR}

	# Test if we are already running the daemon
	start-stop-daemon --start --quiet \
		--pidfile $PIDFILE \
		--chuid $CHUID \
		--test \
		--exec $DAEMON -- $DAEMON_OPTIONS > /dev/null || return 1
	# If all is well we start
	start-stop-daemon --quiet --start \
		--pidfile $PIDFILE \
		--chuid $CHUID \
		--exec $DAEMON -- $DAEMON_OPTIONS || return 2
}

stop() {
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2

	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --pidfile $PIDFILE --name $NAME
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	if [ -f $PIDFILE ]; then 
	 	rm -f $PIDFILE || true
	fi
	return "$RETVAL"
}


case "$1" in
	start)
		log_daemon_msg "Starting postfix policy daemon" "cluebringer"
		start
		case "$?" in
			0|1)
				log_end_msg 0
				;;
			2) log_end_msg 1
				;;
		esac
		;;
	stop)
		log_daemon_msg "Stopping postfix policy daemon" "cluebringer"
		stop
		case "$?" in
			0|1)
				log_end_msg 0
				;;
			2)
				log_failure_msg "Failed"
				log_end_msg 1
				;;
		esac
		;;
	restart|reload|force-reload)
		log_action_begin_msg "Restarting postfix policy daemon cluebringer"
		log_action_cont_msg "Stopping"
		stop
		case "$?" in
			0|1)
				log_action_cont_msg "Starting"
				start
				case "$?" in
					0|1) 
						log_action_end_msg 0
						;;
					2) 
						log_failure_msg "Failed"
						log_action_end_msg 1 
						;;
				esac
				;;
			*) 
				log_failure_msg "Failed"
				log_action_end_msg 1 
				;;
		esac
		;;
	status)
			status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
		;;
	*)
		echo 'Usage: /etc/init.d/$NAME {start|stop|reload|force-reload|restart|status}'
		exit 3
		;;
esac

exit 0
