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

### BEGIN INIT INFO
# Provides: RCSSchedulerService
# X-UnitedLinux-Should-Start: oracle
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: RCS Scheduler
### END INIT INFO

rcsSchedulerIsRunning()
{
      rcsScheduler_ps_log=`mktemp /var/tmp/rcsScheduler-ps.log.XXXXXX`
      ps aux --cols 1024 >"$rcsScheduler_ps_log"
      rcsScheduler_is_running="false"
      if grep RCSScheduler/lib/RCS.jar "$rcsScheduler_ps_log" >/dev/null 2>/dev/null ; then
            rcsScheduler_is_running="true"
      fi
      rm -f "$rcsScheduler_ps_log"
      test "$rcsScheduler_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"
}

# 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 RCSSCHEDULER_HOME=/usr/local/tstv/RCSScheduler

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

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

CMD_START="cd $RCSSCHEDULER_HOME/bin; ./startRCSScheduler.sh"
CMD_STOP="cd $RCSSCHEDULER_HOME/bin; ./stopRCSScheduler.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 -r
          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 -r
          exit 1
        fi

        echo -n "Starting RCS Scheduler service: "
        if rcsSchedulerIsRunning;
        then
            rc_failed 0
        else
            $SUBIT "$CMD_START &"
        fi

        # Remember status and be verbose
        rc_status -v
        ;; 
stop)   
        echo -n "Stopping RCS Scheduler service: "
		if rcsSchedulerIsRunning;
		then
			$SUBIT "$CMD_STOP"
		else
			rc_failed 3
		fi

        # Remember status and be verbose
        rc_status -v
        ;;
status)
        echo -n "Checking for RCS Scheduler service status: "
        ## 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 rcsSchedulerIsRunning ; 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
