#!/bin/sh
#
# policyd	This shell script takes care of starting and stopping
#		policy daemon.
#
# chkconfig:	345 85 35
#
# description:	Policyd is an anti-spam plugin for Postfix.
#
# pidfile:      /var/run/policyd/policyd.pid
# config:       /etc/policyd/cluebringer.conf

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

# Get network config
. /etc/sysconfig/network

# Get service config
CLUEBRINGER_CONF="/etc/policyd/cluebringer.conf"
if [ -r /etc/sysconfig/policyd ]; then
	. /etc/sysconfig/policyd
fi

pidfile="/var/run/policyd/policyd.pid"

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "PolicyD"
		exit 1
	fi
else
	exit 0
fi

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/policyd ]; then
		msg_already_running "PolicyD"
		return
	fi

	msg_starting "PolicyD"
	daemon $SERVICE_RUN_NICE_LEVEL /usr/sbin/cbpolicyd --config "$CLUEBRINGER_CONF"
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/policyd
}

stop() {
	# Stop daemon.
	if [ ! -f /var/lock/subsys/policyd ]; then
		msg_not_running "PolicyD"
		return
	fi

	msg_stopping "PolicyD"
	killproc --pidfile $pidfile cbpolicyd
	rm -f /var/lock/subsys/policyd
}

condrestart() {
	if [ ! -f /var/lock/subsys/policyd ]; then
		msg_not_running "PolicyD"
		RETVAL=$1
		return
	fi

	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	status --pidfile $pidfile policyd cbpolicyd
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
