#!/bin/csh
#
#	Manage a Stage Manager
#
#	CVS ID: Manage_Stage_Manager,v 1.9 2011/04/06 20:22:51 castalia Exp


#	Initial environment setup:

#	Workaround obsolete cruft in the PIRL User Environment setups (.login).
setenv NO_TERM_MENU 1
set TERM = non_interactive
source ~/.cshrc
source ~/.login

#	Required STAGE_MANAGER.
if (! $?STAGE_MANAGER) then
	set STAGE_MANAGER	= /opt/local/sh/Stage_Manager
endif

#	Required STAGE_MANAGER_LOG_DIR.
if (! $?STAGE_MANAGER_LOG_DIR) then
	set STAGE_MANAGER_LOG_DIR = ~/.Stage_Manager
endif


#	Arguments parsing:

set Stage_Manager_Args = ()
while ($#argv)
	switch ($1)
		case [Ss][Tt][Aa]*:
			set op = "start"
			goto Set_Operation
		case [Ss][Tt][Oo]*:
			set op = "stop"
			goto Set_Operation
		case [Rr][Ee]*:
			set op = "restart"

			Set_Operation:
			if ($?Operation) then
				if ($Operation != $op) then
					echo "Multiple operations - $Operation and $op"
					echo
					goto Usage
				endif
			endif
			set Operation = $op
			breaksw

		case -[Hh]*:
			goto Usage

		default:
			set Stage_Manager_Args = ($Stage_Manager_Args $1)
			breaksw
	endsw
	shift
end


#	Help listing:

if (! $?Operation) then
	Usage:
	echo "Usage: $0:t <operation> [Stage_Manager arguments]"
	echo
	echo 'Manage a Stage_Manager on the local host system.'
	echo 'Operation is one of -'
	echo '  start'
	echo '  stop'
	echo '  restart'
	echo
	echo 'All other arguments are passed to the Stage_Manager.'
	echo
	echo 'Environment setup:'
	echo
	echo 'The ~/.cshrc and ~/.login files are sourced.'
	echo 'If a $STAGE_MANAGER_LOG_DIR/Environment'
	echo '  or $STAGE_MANAGER_LOG_DIR/.environment file exists it is sourced.'
	echo
	echo 'The $HOST.{pid,report,log} files are written to the $STAGE_MANAGER_LOG_DIR.'
	echo
	echo "STAGE_MANAGER_LOG_DIR is $STAGE_MANAGER_LOG_DIR"
	echo "The Stage_Manager procedure is in $STAGE_MANAGER"
	echo
	$STAGE_MANAGER -help
	exit 1
endif


#	Confirm or create the STAGE_MANAGER_LOG_DIR.
if (! -d $STAGE_MANAGER_LOG_DIR) then
	mkdir -p $STAGE_MANAGER_LOG_DIR
	if ($status) then
		echo "Unable to create the $STAGE_MANAGER_LOG_DIR directory."
		exit 1
	endif
endif


#	Secondary environment setup:

#	Avoid possible reset of  STAGE_MANAGER or STAGE_MANAGER_LOG_DIR.
set Stage_Manager				= $STAGE_MANAGER
set Stage_Manager_Log_File		= $STAGE_MANAGER_LOG_DIR/$HOST.log
set Stage_Manager_PID_File		= $STAGE_MANAGER_LOG_DIR/$HOST.pid
set Stage_Manager_Report_File	= $STAGE_MANAGER_LOG_DIR/$HOST.report

if (-f $STAGE_MANAGER_LOG_DIR/Environment) then
	source $STAGE_MANAGER_LOG_DIR/Environment
else if (-f $STAGE_MANAGER_LOG_DIR/.environment) then
	source $STAGE_MANAGER_LOG_DIR/.environment
endif


#	Initialize the PID; from an existing PID file if available.
if (-f $Stage_Manager_PID_File) then
	set PID = `cat $Stage_Manager_PID_File`

	#	Check if the process is running.
	set PIDs = (`/bin/ps auxwww \
		| grep -v grep \
		| awk '{print $2}' \
		| grep "$PID"`)
	while ($#PIDs)
		if ($PIDs[1] == $PID) then
			break
		endif
		shift PIDs
	end
	if (! $#PIDs) then
		#	The process was not found.
		unset PID
	endif
endif


if ($Operation == "stop" || $Operation == "restart") then
	if ($?PID) then
		echo  "Shutting down Stage_Manager $PID"
		/bin/kill -TERM $PID
		/bin/rm -f $Stage_Manager_PID_File
		unset PID
	else
		echo "No Stage_Manager to stop"
	endif
endif

if ($Operation == "start" || $Operation == "restart") then

	if ($?PID) then
		echo "Stage_Manager $PID is already running"
		exit 0
	endif

	#	Remove the LOG, PID and REPORT FILEs.
	/bin/rm -f $Stage_Manager_Log_File
	/bin/rm -f $Stage_Manager_PID_File
	/bin/rm -f $Stage_Manager_Report_File

	#	Run the Stage_Manager and capture its PID.
	echo "Starting Stage_Manager -log $Stage_Manager_Log_File $Stage_Manager_Args"
	/bin/date "+%e %B %G %T %z" > $Stage_Manager_Report_File
	/usr/bin/nohup $Stage_Manager -log $Stage_Manager_Log_File $Stage_Manager_Args:q >>& $Stage_Manager_Report_File &
	set PID = $!

	#	Confirm that the Stage_Manager started successfully.
	sleep 3
	set PIDs = (`/bin/ps auxwww \
		| grep -v grep \
		| awk '{print $2}' \
		| grep "$PID"`)
	while ($#PIDs)
		if ($PIDs[1] == $PID) then
			break
		endif
		shift PIDs
	end

	#	Update the PID file.
	if ($#PIDs) then
		echo $PID >! $Stage_Manager_PID_File
		echo "Started Stage_Manager $PID"
	else
		echo "The Stage_Manager did not start successfully"
		exit 1
	endif

endif

exit 0

end
