#!/bin/sh
#

# Basic support for IRIX style chkconfig
# chkconfig: 3 50 20
# description: XXXX

# Basic support for the Linux Standard Base Specification 1.0.0 (to be used by
# insserv for exemple)
### BEGIN INIT INFO
# Provides: XXXX
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start: 
# Default-Stop:
# Description: 
### END INIT INFO

AVESERVER_BIN=/opt/kav/bin/aveserver
KAV_CONF=/etc/kav/5.0/kav4mailservers.conf

get_avpid() {
  if [ -s $KAV_CONF ]; then
      AVPID=`grep AVSpidPATH $KAV_CONF | cut -d= -f2` 
      PID=`head $AVPID 2> /dev/null`
      echo $PID
      return 0 
    else
      echo "Config file not found"
      exit 1
  fi 
}


start() {
  if [ -x $AVESERVER_BIN ]; then
      $AVESERVER_BIN
      if [ $? -eq 0 ]; then
          echo "aveserver was started"
        else 
          echo "aveserver couldn't be started"
      fi
    else 
      echo "$AVESERVER_BIN was not found"
      exit 2
  fi  
  
}
stop() {
  PID=`get_avpid`
  kill -TERM $PID 2> /dev/null
  if [ $? -eq 0 ]; then
      echo "aveserver was stoped"
      return 0
    else
      echo "aveserver couldn't be stoped" 
  fi
}

status() {
  NAME=`basename $AVESERVER_BIN`	
  PS=`ps ax |grep $NAME |grep -v grep`
  if [ -n "$PS" ]; then 
      echo "aveserver is  running"
    else
      echo "aveserver is not running"
  fi	  
}

restart() {
  stop
  start
}

reload() {
  PID=`get_avpid`
  kill -HUP $PID 2> /dev/null
  if [ $? -eq 0 ]; then
      echo "aveserver was reloaded"
      return 0
    else
      echo "aveserver couldn't be reloaded " 
  fi
}



case $1 in 
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
		stop
		start
		;;
	reload)
		reload
		;;	
	*)
		echo "Usage: ${0##*/} {start|stop|restart|reload|status}"
		;;
esac

exit 0
