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

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

# Get network config
. /etc/sysconfig/network

sagator_binary=/usr/sbin/sagator
CHROOT="/var/spool/vscan"
[ -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 $@
	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?
	if [ "$CLAMD" ]; then
		start_daemon clamd chroot $CHROOT $CLAMD
	fi
	if [ "$NOD32" ]; then
		start_daemon nod32d chroot $CHROOT $NOD32
	fi
	if [ "$AVG" ]; then
		start_daemon AVG chroot $CHROOT $AVG
	fi
	start_daemon sagator $sagator_binary --daemon "$SAGATOR_OPTIONS"
	;;
  stop)
	stop_daemon sagator
	if [ "$CLAMD" ]; then
		stop_daemon clamd
	fi
	if [ "$NOD32" ]; then
		stop_daemon nod32d -TERM
	fi
	if [ "$AVG" ]; then
		stop_daemon avg
	fi
	;;
  restart)
	export CLAMD=""
	export NOD32=""
	export AVG=""
	stop_daemon sagator
	start_daemon sagator $sagator_binary --daemon "$SAGATOR_OPTIONS"
	;;
  restartall)
	$0 stop
	$0 start
	;;
  status)
	if [ "$NOD32" ]; then
		status nod32d
	fi
	if [ "$AVG" ]; then
		status avg
	fi
	status sagator
	exit $?
	;;
  reload)
	killproc sagator -HUP
	;;
  *)
	echo "Usage: $0 {start|stop|init|status|restart|restartall|reload}"
	exit 1
esac

exit $RETVAL
