#!/bin/sh
# RCS SNMP SubAgent auto-start
#
# description: Auto-starts RCS SNMP SubAgent
# processname: rcsSnmpSubAgent (java)
# pidfile: /usr/local/tstv/RCSSnmpSubAgent/config/rcsSnmpSubAgent.pid 

### BEGIN INIT INFO
# Provides: rcssnmpsubagent
# X-UnitedLinux-Should-Start: oracle snmpd
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: RCS SNMP SubAgent
### END INIT INFO

rcsSubagentIsRunning()
{
      rcsSubagent_ps_log=`mktemp /var/tmp/rcsSubagent-ps.log.XXXXXX`
      ps aux --cols 1024 >"$rcsSubagent_ps_log"
      rcsSubagent_is_running="false"
      if grep RCSSnmpSubAgent/lib/rcsSnmpSubAgent.jar "$rcsSubagent_ps_log" >/dev/null 2>/dev/null ; then
            rcsSubagent_is_running="true"
      fi
      rm -f "$rcsSubagent_ps_log"
      test "$rcsSubagent_is_running" = "true"
}

oracleIsRunning()
{
      oracle_ps_log=`mktemp /var/tmp/oracle-ps.log.XXXXXX`
      ps aux --cols 1024 >"$oracle_ps_log"
      oracle_is_running="false"
      if grep [o]ra_pmon "$oracle_ps_log" >/dev/null 2>/dev/null ; then
            oracle_is_running="true"
      fi
      rm -f "$oracle_ps_log"
      test "$oracle_is_running" = "true"
}

listenerIsRunning()
{
      listener_ps_log=`mktemp /var/tmp/listener-ps.log.XXXXXX`
      ps aux --cols 1024 >"$listener_ps_log"
      listener_is_running="false"
      if grep [t]nslsnr "$listener_ps_log" >/dev/null 2>/dev/null ; then
            listener_is_running="true"
      fi
      rm -f "$listener_ps_log"
      test "$listener_is_running" = "true"
}

snmpdIsRunning()
{
      snmpd_ps_log=`mktemp /var/tmp/snmpd-ps.log.XXXXXX`
      ps aux --cols 1024 >"$snmpd_ps_log"
      snmpd_is_running="false"
      if grep [s]nmpd "$snmpd_ps_log" >/dev/null 2>/dev/null ; then
            snmpd_is_running="true"
      fi
      rm -f "$snmpd_ps_log"
      test "$snmpd_is_running" = "true"
}

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

export JAVA_HOME=/usr/local/tstv/jdk
export RCSSNMPSUBAGENT_HOME=/usr/local/tstv/RCSSnmpSubAgent

#define the user under which RCSSnmpSubAgent will run, or use RUNASIS to run as the current user
RCSSNMPSUBAGENTUSER=${RCSSNMPSUBAGENTUSER:-"tstv"}

if [ "$RCSSNMPSUBAGENTUSER" = "RUNASIS" ]; then
  SUBIT=""
else
  SUBIT="su - $RCSSNMPSUBAGENTUSER -c "
fi

CMD_START="cd $RCSSNMPSUBAGENT_HOME/bin; ./startsubagent.sh"
CMD_STOP="cd $RCSSNMPSUBAGENT_HOME/bin; ./stopsubagent.sh"

case $1 in
start)
        echo -n "Checking Oracle server status: "
        if oracleIsRunning ; then
          rc_failed 0
          rc_status -v
        else
          rc_failed 3
          rc_status -v
          exit 1
        fi

        echo -n "Checking Oracle Listener status: "
        if listenerIsRunning ; then
          rc_failed 0
          rc_status -v
        else
          rc_failed 3
          rc_status -v
          exit 1
        fi

        echo -n "Checking Snmp Master Agent (snmpd) status: "
        if snmpdIsRunning ; then
          rc_failed 0
          rc_status -v
        else
          rc_failed 3
          rc_status -v
          exit 1
        fi

        echo -n "Starting RCS SNMP SubAgent service: "
        if rcsSubagentIsRunning;
        then
            rc_failed 0
        else
            $SUBIT "$CMD_START &"
        fi

        # Remember status and be verbose
        rc_status -v
        ;; 
stop)
        echo -n "Stopping RCS SNMP SubAgent service: "
		if rcsSubagentIsRunning;
		then
			$SUBIT "$CMD_STOP"

                  while rcsSubagentIsRunning
                  do
                          echo "... waiting for shutdown..."
                          sleep 3
                  done
		else
			rc_failed 3
		fi

        # Remember status and be verbose
        rc_status -v
        ;;
status)
        echo -n "Checking for RCS SNMP SubAgent service (rcssnmpsubagent): "
        ## Check status with checkproc(8), if process is running
        ## checkproc will return with exit status 0.

        # Return value is slightly different for the status command:
        # 0 - service up and running
        # 1 - service dead, but /var/run/  pid  file exists
        # 2 - service dead, but /var/lock/ lock file exists
        # 3 - service not running (unused)
        # 4 - service status unknown :-(
        # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)

        if rcsSubagentIsRunning ; then
            rc_failed 0
        else
            rc_failed 3
        fi

        # NOTE: rc_status knows that we called this init script with
        # "status" option and adapts its messages accordingly.
        rc_status -v
        ;;
restart)
    	$0 stop
    	$0 start

	    # Remember status and be quiet
	    rc_status
    	;;
*)
    	echo "Usage: $0 {start|stop|status|restart}"
    	exit 1
esac    
rc_exit
exit 0