#!/bin/sh
# RCSSchedulerService   RCS Scheduler Control Script
#
# Authors:              Matthew Kruer <matthew.kruer@ericsson.com>
#
# description:          Ericsson / SA Media RCS Scheduler Start Up Script
# processname:          RCSScheduler (java)
# pidfile:              /usr/local/tstv/RCSScheduler/config/RCSScheduler.pid

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

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

# Set-up Variables
APP_HOME=/usr/local/tstv/RCSScheduler
RCSSCHEDULER_PID=$APP_HOME/config/rcsScheduler.pid

CMD_START="cd $APP_HOME/bin; ./startRCSScheduler.sh"

# 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="$CMD_START"
else
  SUBIT="su - $RCSSCHEDULERUS -c \"$CMD_START\""
fi

#Check to see if the cms services are running
check_cms()
{
    adinameserver_pid=$(ps axww | grep "[o]rg.jacorb.naming.NameServer" | sed -e 's/^\s*//' | cut -d' ' -f1)
    cms_pid=$(ps axww | grep "[o]rg.jboss.Main" | sed -e 's/^\s*//' | cut -d' ' -f1)
    if [[ -z "${adinameserver_pid}" ]] || [[ -z "${cms_pid}" ]]; then
        printf "  The required service CMS has not started. Unable to start RCS Scheduler Service. \n"
        exit 1
    fi

    printf "  Starting RCSScheduler\n"
    printf "  Waiting for CMS Services to fully start"
    count=0
    delay=1
    timeout=600
    while [[ $count -le "$timeout" ]] ; do
        sleep $delay
        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 [[ $count -ge $timeout ]] ; then
                printf "\n  Failed to Start RCSSchedulerService after $count Seconds. Verify that CMS Service is running correctly. \n"
                exit 1
            else
                printf "."
            fi
        else
            if [[ "$CURL_OUT" == "200" ]] || [[ "$CURL_OUT" == "302" ]] ; then
                printf "\n  Done [Start Up Duration $count Seconds] \n"
                count=$(( $count + $timeout ))
            else
                printf "\n  Unknown exit code \n"
                printf "`curl -k -s --head --connect-timeout 1 https://localhost:8443/portal/`"
                count=$(( $count + $timeout ))
            fi
        fi
        count=$(( $count +1 ))
    done
}

rcs()
{
    pid=$(ps axww | grep "[R]CSScheduler/lib/RCS.jar" | grep -v grep | sed -e 's/^\s*//' | cut -d' ' -f1)
    if [[ -z "${pid}" ]] && [[ "start" = $1 ]] ; then
        printf "  Starting RCSScheduler "
        daemon --pidfile="$RCSSCHEDULER_PID" $SUBIT
    elif [[ -n "${pid}" ]] && [[ "stop" = $1 ]] ; then
        printf "  Stopping RCSScheduler "
        killproc -p "$RCSSCHEDULER_PID"
    elif [[ -n "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  RCSScheduler (pid "$pid") is running \n"
    elif [[ -z "${pid}" ]] && [[ "status" = $1 ]]; then
        printf "  RCSScheduler is stopped \n"
    else
        exit 1
    fi
}

case "$1" in
    start)
        printf "Starting RCSScheduler Service: \n"
        check_cms
        rcs $1
        $0 status
        ;;
    stop)
        printf "Stopping RCSScheduler Service: \n"
        rcs $1
        $0 status
        ;;
    status)
        printf " Status of RCSScheduler Service: \n"
        rcs $1
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        printf "Usage: $0 ( start | stop | status | restart )\n"
        exit 1
        ;;
esac
exit 0
