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

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

#define the user under which RCSScheduler will run, or use RUNASIS to run as the current user
. /etc/init.d/functions
prog="RCSScheduler"
RCSSCHEDULERUS=${RCSSCHEDULERUS:-"tstv"}
APP_HOME=/usr/local/tstv/RCSScheduler
RCSSCHEDULER_PID=$APP_HOME/config/rcsScheduler.pid
CMD_START="cd $APP_HOME/bin; ./startRCSScheduler.sh"

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

check_cms_licensekey() {
if [[ ! -f "/opt/tandbergtv/cms/conf/workflow/licensekey.lic" ]] ; then
    printf "\nCMS license key not found; Aborting startup of RCSSchedulerService\n"
    exit 1
fi
}

check_cms() {
printf "\nWaiting for CMS Service to Start "
n=1
timeout=600
while [ $n -le "$timeout" ] ; do
    CURL_OUT=`curl -k -s --head --connect-timeout 1 https://localhost:8443/portal/ | head -n 1 | awk -F" " '{print $2}'`
    if [[ -z "$CURL_OUT" ]] ; then 
        if [[ $n -ge $timeout ]] ; then
            printf "\nFailed to Start RCSSchedulerService after $n Seconds. Verify that CMS Service is running correctly\n" 
            exit 1
        else
            printf "."
            sleep 1  
        fi
    else
        if [[ "$CURL_OUT" == "200" ]] ; then 
            printf "\nDone [Start Up Duration $n Seconds]\n"
            n=$timeout
        else
            printf "\nUnkown exit code\n"
            printf "`curl -k -s --head --connect-timeout 1 https://localhost:8443/portal/`"
            n=$timeout
        fi
    fi
    n=$(( $n +1 ))
done
}

############ VCS Check Logic [START] ############
VCSBIN="/opt/VRTSvcs/bin/"
HA_CURRENT_SYSTEM=`hostname -s`
SERVICE=`echo $0 | awk -F"/" '{print $NF}'`

if [[ $2 == "--VCSOverride" ]] || [[ -z `/etc/rc.d/init.d/vcs status | grep running...` ]] ; then
############# VCS Check Logic [END] #############
#
########## Original Case Logic [START] ##########

    case $1 in
    start)
        echo -n $"Starting $prog service: "
        check_cms_licensekey
        check_cms
        daemon --pidfile="$RCSSCHEDULER_PID" $SUBIT
        echo
        ;;
    stop)
        echo -n $"Stopping $prog service: "
        killproc -p "$RCSSCHEDULER_PID" 
        echo
        ;;
    status)
        status -p "$RCSSCHEDULER_PID" $prog
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
    esac
    exit 0

########### Original Case Logic [END] ###########
#
############# VCS Case Logic [START] ############
else
    case "$1" in
        start)
            echo "VCS is currently managing the $SERVICE service, Attempting to $1 $SERVICE on $HA_CURRENT_SYSTEM."
            `$VCSBIN/hares -online $SERVICE -sys $HA_CURRENT_SYSTEM &`
            ;;
        stop)
            echo "VCS is currently managing the $SERVICE service, Attempting to $1 $SERVICE on $HA_CURRENT_SYSTEM."
            `$VCSBIN/hares -offline -ignoreparent $SERVICE -sys $HA_CURRENT_SYSTEM &`
            ;;
        status)
            service $SERVICE status --VCSOverride
            ;;
        restart)
            echo "VCS is currently managing the $SERVICE service and restart is not available, please stop and start the service manually."
    esac
fi
############## VCS Case Logic [END] #############
