#!/bin/sh
#
# chkconfig: 3 90 08
# description: ProScan Management Tool Agent
### BEGIN INIT INFO
# Provides: agent
# Required-Start: proscan
# Required-Stop: proscan
# Default-Start: 3
# Default-Stop: 0 1 2 5 6
# Description: Start the ProScan antivirus engin daemon
### END INIT INFO
AGENT_BIN=/opt/proscan/agent/bin/agent
OPTIONS="-L 8191 -t 600"

pstart() {
  if [ -x $AGENT_BIN ]; then
      $AGENT_BIN $OPTIONS > /dev/null 2>&1
       if [ $? -eq 0 ]; then
          echo "PSMT agent was started"
        else
          echo "PSMT agent couldn't be started"
      fi
  else
      echo "$AGENT_BIN was not found"
      exit 2
  fi

}

pstop() {
  if [ -x $AGENT_BIN ]; then
      PS=`ps ax|grep $AGENT_BIN |grep -v grep|awk '{print $1}'`
      if [ "$PS" != "" ]; then
          kill $PS
      else
	  echo "PSMT agent already stopped"
	  return 0
      fi
      sleep 1
      PS=`ps ax|grep $AGENT_BIN |grep -v grep|awk '{print $1}'`
      if [ "$PS" = "" ]; then
          echo "PSMT agent was stoped"
          return 0
      else
          echo "PSMT agent couldn't be stoped"
      fi
  else
      echo "$AGENT_BIN was not found"
      exit 2
  fi
}

pstatus() {
  PS=`ps ax |grep $AGENT_BIN |grep -v grep`
  if [ -n "$PS" ]; then
      echo "PSMT agent is  running"
    else
      echo "PSMT agent is not running"
  fi
}

prestart() {
  pstop
  sleep 1
  pstart
}

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

exit 0
