#!/bin/sh
#
# sagatord	sagator daemon
#
# chkconfig:	345 82 40
#
# description:	sagator daemon, an interface for SMTPd and antivirus

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

sagator_binary=/usr/sbin/sagator
[ -f /etc/sysconfig/sagator ] && . /etc/sysconfig/sagator

start_daemon() {
  name=$1
  shift 1
  echo -n "Starting $name: "
  daemon "$@"
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && touch /var/lock/subsys/$name
}

stop_daemon() {
  if [ -f /var/lock/subsys/$1 ]; then
	echo -n "Stopping $1: "
	rm -f /var/lock/subsys/$1
	killproc $@
	$sagator_binary --wait
	RETVAL=$?
	echo
  else
	echo "$1 is not running"
  fi	
}

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	start_daemon sagator $sagator_binary "$SAGATOR_OPTIONS"
	;;
  stop)
	stop_daemon sagator -TERM
	rm -f /var/run/sagator.pid
	;;
  restart)
	$0 stop $2
	$0 start $2
	;;
  condrestart)
  	if [ -f /var/lock/subsys/sagator ]; then
  		# check configuration file
  		if /usr/sbin/sagator --test; then
	  		$0 stop $2
  			$0 start $2
  		fi
  	fi
  	;;
  status)
	status sagator
	exit $?
	;;
  *)
	echo "Usage: $0 {start|stop|init|status|restart}"
	exit 1
esac

exit $RETVAL
