#!/bin/sh
# This script manages the execution of the Video Surveillance Server

Name="Arcus";
Application="/apps/milestone-arcus/Milestone/Arcus/Arcus.exe"
CmdParameters="-x"; # Cmd parameters are seperated from binary because of grep problem
Log="/dev/null";

# Sets additional paths where the shared libraries for the application can be found
export PYTHONPATH=$PYTHONPATH
export LD_LIBRARY_PATH=/apps/milestone-arcus/Milestone/Arcus:/apps/milestone-arcus/Milestone/DevicePack
export MILESTONE_DEVICEPACK=/apps/milestone-arcus/Milestone/DevicePack

Alive=0; # Variable to keep track whether the application breing run by the script is alive or not
KeepAlive=1; # Flag for determining whether the script should respawn the application when it dies unexpectedly
XPPID=0; # Process ID for the application to be run by the script

# Checks if the application run by this script is alive
isApplicationAlive() {
	# Check if the application is running and set the Alive flag
	HAS_PID=$(pidof $Name.exe)
	if [ -z $HAS_PID ]; then
		Alive=0;
	else
		Alive=1;
	fi
}

# Terminates the application run by this script
terminateApplication() {
	echo "Terminating $Name (PID=$XPPID)..." >> $Log;

	KeepAlive=0; #Tell the script not to respawn the application
  
        # Request the application to terminate
        isApplicationAlive;
        if [ $Alive -eq 1 ]; then
                kill -15 $XPPID; # Send terminate signal to the application
        else
                # The application has already been terminated
                echo "$Name (PID=$XPPID) already terminated!" >> $Log;
                exit;
        fi

	# Wait 120 seconds for application to terminate, checking every second if it has
	tick=0
	while [ $tick -lt 120 ];
	do
	       tick=`expr $tick + 1`
	       sleep 1;
	       isApplicationAlive; # Check if the application is still alive
	       if [ $Alive -eq 0 ]; then
        	       echo "$Name (PID=$XPPID) terminated gracefully. ($tick sec)" >> $Log;
	               exit 0;
	       fi
	done
	
	# The application is non-responsive, kill application
	kill -9 $XPPID;
	echo "$Name (PID=$XPPID) did not terminate gracefully. The application was killed." >> $Log;
	exit;
}

# Check if the application is already running. If it is, halt the execution of this script.
isApplicationAlive;
if [ $Alive -eq 1 ]; then
	echo "$Name is already running! Please close the other instance of $Name before running this script." >> $Log;
	exit;
fi
Alive=0; #Make sure the alive flag is reset, it should be 0 already

# Remove Previous Log if it exists
if [ -f $Log ]
then
	rm $Log
fi

# Trapping the signal to kill this script which will come from the app manager or user ctrl-c.
# If the application receives a terminate signal this script traps it and tries to terminate the 
# application gracefully otherwise after a timeout the application is killed.
trap terminateApplication 15
trap terminateApplication INT

# Loop while the application should be kept alive
while [ $KeepAlive -eq 1 ];
do
	# Starting the Application in a new process
	echo "Starting $Name..." >> $Log
	$Application $CmdParameters >> $Log 2>&1 &
	XPPID=$! #Save the applications process ID
	echo "$Name running in the background with PID=$XPPID." >> $Log

	# Let the application start and check that it is alive
	sleep 5;
	isApplicationAlive;

	# Loop while application is alive
	while [ $Alive -eq 1 ];
	do
		isApplicationAlive; # Check if the application is still alive
		sleep 5;
	done
  
	# The Application died unexpectedly
	echo "$Name (PID=$XPPID) terminated unexpectedly, the application probably crashed while executing!" >> $Log;
done

