#!/bin/sh
#
# (c) Copyright 2005 Hewlett-Packard Development Company, L.P.
# All rights reserved
#
# $Id: hp-vt,v 1.7 2005/07/14 07:16:23 tonycu Exp $
#
# Author: Tony Cureington
#
### BEGIN INIT INFO
# Provides:          hp-vt
# Required-Start:    $network $local_fs
# Should-Start:
# Required-Stop:
# Should-Stop:       $network $local_fs
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: HP Virus Throttle
# Description: The HP Virus Throttle attempts to detect and
#	limit "virus-like" network activity.
#	what it's needed for ...
### END INIT INFO
# 
# Return values according to LSB v2.0.1 for all commands except status
# (see status command in this file for status command return codes):
# 0	    - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.
#

# initialized vars
################################################################################
APP_BASENAME=hp-vtd
APP_BIN_DIR=/opt/hp/hp-vt
APP_BIN=/opt/hp/hp-vt/$APP_BASENAME
APP_CONF=/etc/opt/hp/hp-vt/hp-vt.conf
APP_PIDFILE=/var/run/$APP_BASENAME.pid

# must match VT_START_STATUS_FILE in vt.h
APP_STATUS_FILE=/var/opt/hp/hp-vt/.start_status.txt


APP_FAILURE="hp-vtd failed to start"
APP_STAT_COMPLETE="^end"

RC=0

# functions
################################################################################

# source LSB init functions
#-----------------------------------------------------------
if [ ! -f /lib/lsb/init-functions ]; then
  echo "Error: your distribution is not supported by $0" > /dev/stderr
  exit 1
fi
. /lib/lsb/init-functions

# print usage
#-----------------------------------------------------------
usage()
{
   echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload}"
}

# check caller privilages, exit if not root.
# initscript argument passed as argument
#-----------------------------------------------------------
root_uid()
{
   if [ $EUID != 0 ]; then
      echo "$1 argument requires root privilages"
      exit 4
   fi
}


# report any startup error messages from daemon
#-----------------------------------------------------------
StartStatus()
{
   # wait up to six seconds for contents of status file to be
   # written by the daemon. a single "." in the first column means
   # all has been written.
   loop_count=0
   secs_to_wait=6
   while [ $loop_count -lt $secs_to_wait ]; do
      grep "$APP_STAT_COMPLETE" $APP_STATUS_FILE &>/dev/null
      if [ $? -eq 0 ]; then
         break
      fi
      ((loop_count=loop_count+1))
      sleep 1
   done

   # don't allow grep of status if the daemon was not done writting,
   # or did not write, to the file 
   if [ $loop_count -eq $secs_to_wait ]; then
      pidofproc $APP_BIN &> /dev/null
      if [ $? -eq 0 ]; then
         # app is running but could not get start status...
         log_warning_msg ", could not get start status of $APP_BASENAME"
      else
         log_failure_msg ", failed to start $APP_BASENAME"
      fi
      return 1
   fi

   # the daemon has finished logging the start status, so print the results
   # from the daemon. errors would be from such things as the daemon detecting 
   # errors in he config file, etc.
   grep "$APP_FAILURE" $APP_STATUS_FILE &>/dev/null
   if [ $? -eq 0 ]; then
      # daemon had problems starting, inform user
      log_failure_msg " "
      cat $APP_STATUS_FILE | grep -v "$APP_STAT_COMPLETE"
      return 1
   else
      log_success_msg " "
      cat $APP_STATUS_FILE | grep -v "$APP_STAT_COMPLETE"
      return 0
   fi

   # not reached
   return 1
}


# main
################################################################################

# check for missing binaries (stale symlinks should not happen)
# note: Special treatment of stop for LSB conformance
test -x $APP_BIN || { echo "$APP_BIN not present, exiting"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# check for existence of required config file
#test -r $APP_CONF || { echo "$APP_CONF not present, exiting";
#	if [ "$1" = "stop" ]; then exit 0;
#	else exit 6; fi; }
# read config	
#. $APP_CONF




# check number of args; should be one argument per LSB v2.0.1 Init Script 
# Actions section.
if [ $# -ne 1 ]; then
   usage
   exit 2
fi

# change path to 64 bit binaries is on 64 bit server
uname -m | grep x86_64 &> /dev/null
if [ $? -eq 0 ] && [ -x /opt/hp/hp-vt/64bit/$APP_BASENAME ]; then
   APP_BIN=/opt/hp/hp-vt/64bit/$APP_BASENAME
   APP_BIN_DIR=/opt/hp/hp-vt/64bit
fi

case "$1" in
   start)
      root_uid $1
      echo -n "Starting $APP_BASENAME"
      # per LSB v2.0.1 "Init Script Actions" section, return success
      # if daemon is already running
      pidofproc $APP_BIN &> /dev/null
      if [ $? -eq 0 ]; then
         log_success_msg " "
         exit 0
      fi
      rm -f $APP_STATUS_FILE

      # export config vars to daemon
      # set -a
      set -o allexport
      . $APP_CONF
      if [ $? -ne 0 ]; then
         echo "ERROR: problem parsing $APP_CONF configuration file"
         log_failure_msg " "
         exit 1
      fi

      start_daemon $APP_BIN &>/dev/null
      RC=$?
      if [ $RC -eq 0 ] || [ -f $APP_STATUS_FILE ]; then
         # the daemon was started, make it sure successfully starts (is
         # happy with config files, etc); StartStatus reports the status 
         # message
         StartStatus
         RC=$?
      else
         log_failure_msg " "
      fi
      rm -f $APP_STATUS_FILE
      ;;

   stop)
      root_uid $1
      echo -n "Shutting down $APP_BASENAME"
      killproc $APP_BIN
      # killproc -TERM $APP_BIN, one distro did not like passing the -TERM
      # per LSB v2.0.1 "Init Script Actions" section, return success if 
      # the daemon was or was not running
      #RC=$?
      log_success_msg " "
      ;;

   restart)
      root_uid $1
      echo "Restarting $APP_BASENAME:"
      # stop and restart the service if the service is already running,
      # otherwise start the service 
      $0 stop
      $0 start
      RC=$?
      ;;

   try-restart|condrestart)
      root_uid $1
      # restart the service if the service is already running
      pidofproc $APP_BIN &> /dev/null
      if [ $? -eq 0 ]; then
         $0 restart
         RC=$?
      fi
      ;;

   force-reload)
      root_uid $1
      # cause the configuration to be reloaded if the service supports
      # this, otherwise restart the service if it is running
      $0 try-restart
      RC=$?
      ;;

    reload)
      root_uid $1
      log_failure_msg "$1 not supported"
      # return 3 - unimplemented feature, per LSB v2.0.1
      RC=3
      ;;

   status)
      # Return values according to LSB v2.0.1 "Init Script Actions" section
      # for status command:
      # 0       program is running or service is OK
      # 1       program is dead and /var/run pid file exists
      # 2       program is dead and /var/lock lock file exists
      # 3       program is not running
      # 4       program or service status is unknown
      # 5-99    reserved for future LSB use
      # 100-149 reserved for distribution use
      # 150-199 reserved for application use
      # 200-254 reserved
      pidofproc $APP_BIN &> /dev/null
      if [ $? -eq 0 ]; then
         echo "$APP_BASENAME is running"
         # dump stats from daemon
         $APP_BIN_DIR/hp-vt-status -status
         exit 0
      elif [ -f $APP_PIDFILE ]; then
         RC=1
         echo "$APP_BASENAME dead but pid file exists"
      else
         RC=3
         echo "$APP_BASENAME is not running"
      fi
      ;;

   *)
      usage
      # return 2 - invalid or excess argument(s), per LSB v2.0.1
      RC=2
      ;;
esac
exit $RC
