#!/bin/bash
#
# Start/Stop CRSAppServer
#
# copied from /etc/init.d/script.template
# adapted by Ian Moir
#
#

NNSTARTLOG="/var/log/nn_startup"

pidfile="/var/run/CCRSAppServer.pid"
lockfile="/tmp/scripts/CCRSAppServer.startscript.lock"

alreadyRunning=0

#
# This function is used to reliably create a pid file.
# First the pid of the process running this script is saved
# to the pid file. Then, the exec causes the starting app
# to use the same pid to run under. This function never
# returns since the app server exits with a ctrl-C signal.
#
doexec() {
    echo $$ > $pidfile
    exec /opt/sbin/CCRSAppServer /nn/sbin/CCRSAppServer.exe
}

start() {
    echo `date` $"CCRSAppServer: Checking if CCRSAppServer already running or being started ..." >> $NNSTARTLOG
    if [ -f $pidfile ]; then
        oldpid=`cat $pidfile`
        if [ -f /proc/$oldpid/cmdline ]; then
            isappsrv=`grep -a CCRSAppServer /proc/$oldpid/cmdline`
            if [ -z "$isappsrv" ]; then
                rm -f $pidfile
                echo `date` $"CCRSAppServer: Stale CCRSAppServer.pid file found ..." >> $NNSTARTLOG
            else
                echo `date` $"CCRSAppServer: CCRSAppServer is already running." >> $NNSTARTLOG
                alreadyRunning=1
            fi
        else
            rm -f $pidfile
            echo `date` $"CCRSAppServer: Stale CCRSAppServer pid file found ..." >> $NNSTARTLOG
        fi
    fi
    if [ $alreadyRunning -eq 0 ]; then
        # make exec call as a new process
        /etc/rc.d/init.d/CCRSAppServer exec &
        killcount=0
        while [ ! -f $pidfile ] && [ $killcount -lt 10 ]
        do
            sleep 2
            killcount=$((killcount+1))
        done
        if [ $killcount -lt 10 ]; then
            echo `date` $"CCRSAppServer: CCRSAppServer launched." >> $NNSTARTLOG
        else
            echo `date` $"CCRSAppServer: Error launching CCRSAppServer." >> $NNSTARTLOG
        fi
    fi
}

stop() {
    echo `date` $"CCRSAppServer: Stopping CCRSAppServer ..." >> $NNSTARTLOG
    if [ -f $pidfile ]; then
        pidtostop=`cat $pidfile`
        killcount=0
        while [ -d "/proc/$pidtostop" ] && [ $killcount -lt 10 ]
        do
            echo `date` $"CCRSAppServer: Signalling CCRSAppServer to exit ..." >> $NNSTARTLOG
            kill -2 $pidtostop
            sleep 2
            killcount=$((killcount+1))
        done
        if [ $killcount -lt 10 ]; then
            echo `date` $"CCRSAppServer: CCRSAppServer exited." >> $NNSTARTLOG
        else
            echo `date` $"CCRSAppServer: Could not signal CCRSAppServer to exit ... " >> $NNSTARTLOG
            echo `date` $"CCRSAppServer: Killing CCRSAppServer. " >> $NNSTARTLOG
            kill -9 $pidtostop
        fi
        rm -f $pidfile
    else
        echo `date` $"CCRSAppServer: CCRSAppServer pid file not found." >> $NNSTARTLOG
    fi
}

restart() {
        stop
        start
}

reload() {
        restart
}

#
# The following section is a secure way of creating a lock file
# set -C ensures that the redirection to the lock file will fail if
# the file already exists. This atomic operation ensures that only
# one script can successfully create a lockfile at a time.
#
(set -C; : > $lockfile) 2> /dev/null
if [ $? != "0" ]; then
    # Since the "exec" call is recursive, the lock file must exist
    if [ "$1" == "exec" ]; then
        doexec
        exit 0
    else
        echo `date` $"CCRSAppServer: Startup script already running - \"$1\" call exiting." >> $NNSTARTLOG
        exit 1
    fi
fi

# Ensure that the lock file is removed anytime the script exists
trap "rm -f $lockfile" EXIT

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart}"
        exit 1
esac

exit 0

