#!/bin/sh
#
# RMScheduler Control Script
#
# To use this script
#
# It assumes that RMScheduler lives in /usr/local/tstv/RMScheduler
# This can be changed in the script itself.
#
# Either amend this script for your requirements
# or just ensure that the following variables are set correctly
# before calling the script.
#
# NOTE: Install vsftpd on the RM machine
#
### BEGIN INIT INFO
# Provides: rmscheduler
# X-UnitedLinux-Should-Start: vsftpd
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start/Stop/Restart the RMScheduler.
### END INIT INFO

# chkconfig: - 62 48

rmSchedulerIsRunning()
{
      rmScheduler_ps_log=`mktemp /var/tmp/rmScheduler-ps.log.XXXXXX`
      ps aux --cols 1024 >"$rmScheduler_ps_log"
      rmScheduler_is_running="false"
      if grep com.ttv.rtm.scheduler.RMScheduler "$rmScheduler_ps_log" >/dev/null 2>/dev/null ; then
            rmScheduler_is_running="true"
      fi
      rm -f "$rmScheduler_ps_log"
      test "$rmScheduler_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"
}

ftpIsRunning()
{
      ftp_ps_log=`mktemp /var/tmp/ftp-ps.log.XXXXXX`
      ps aux --cols 1024 >"$ftp_ps_log"
      ftp_is_running="false"
      if grep [v]sftpd "$ftp_ps_log" >/dev/null 2>/dev/null ; then
            ftp_is_running="true"
      fi
      rm -f "$ftp_ps_log"
      test "$ftp_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

# First reset status of this service

# 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.

#define where RMScheduler is - this is the directory containing directories log, bin, conf etc
RMSCHEDULER_HOME=${RMSCHEDULER_HOME:-"/usr/local/tstv/RMScheduler"}

#define the script to use to start RMScheduler
RMSCHEDULER_START=${RMSCHEDULER_ST:-"startRMScheduler.sh"}

#define the script to stop the RMScheduler
RMSCHEDULER_STOP=${RMSCHEDULER_SP:-"stopRMScheduler.sh"}

if [ -n "$RMSCHEDULER_CONSOLE" -a ! -d "$RMSCHEDULER_CONSOLE" ]; then
  # ensure the file exists
  touch $RMSCHEDULER_CONSOLE
fi

if [ -n "$RMSCHEDULER_CONSOLE" -a ! -f "$RMSCHEDULER_CONSOLE" ]; then
  echo "WARNING: location for saving console log invalid: $RMSCHEDULER_CONSOLE"
  echo "WARNING: ignoring it and using /dev/null"
  RMSCHEDULER_CONSOLE="/dev/null"
fi

#define what will be done with the console log
RMSCHEDULER_CONSOLE=${RMSCHEDULER_CONSOLE:-"/usr/local/tstv/RMScheduler/logs/RMScheduler_Console.log"}

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

CMD_START="cd $RMSCHEDULER_HOME/bin; ./$RMSCHEDULER_START"
CMD_STOP="cd $RMSCHEDULER_HOME/bin; ./$RMSCHEDULER_STOP"

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

if [ ! -d "$RMSCHEDULER_HOME" ]; then
  echo RMSCHEDULER_HOME does not exist as a valid directory : $RMSCHEDULER_HOME
  exit 1
fi

case "$1" in
start)
        echo -n "Checking FTP server status: "
        if ftpIsRunning ; then
	  echo "FTP is Running"
        else
	  echo "Need service (FTP) is not running"
          exit 1
        fi

        echo -n "Starting RMScheduler: "
        if rmSchedulerIsRunning;
        then
	  echo "rmScheduler Service is already running"
        else
            $SUBIT "$CMD_START >${RMSCHEDULER_CONSOLE} 2>&1 &"
        fi

        # Remember status and be verbose
        ;; 
stop)
        echo -n "Shutting down RMScheduler: "
        if rmSchedulerIsRunning;
        then
            $SUBIT "$CMD_STOP"

            sleep 2
            while rmSchedulerIsRunning
            do
                echo "... waiting for shutdown ... "
                sleep 2
            done
        else
	  echo "rmScheduler Service is not Running"
        fi

        # Remember status and be verbose
        ;;
restart)
	    $0 stop
	    $0 start
	
	    # Remember status and be quiet
	    ;;
status)
        echo -n "Checking for RMScheduler 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 rmSchedulerIsRunning ; then
	  echo "rmScheduler service is running"
        else
	  echo "rmScheduler service is not running"
        fi

        # NOTE: rc_status knows that we called this init script with
        # "status" option and adapts its messages accordingly.
        ;;
*)
    	echo "usage: $0 (start|stop|restart|status|help)"
        exit 1
esac
exit 0
