#!/bin/tcsh

#	Manage Stage_Managers on remote systems.
#
#	Author:	Bradford Castalia, UA/HiROC
#
#	CVS ID: Manage_Remote,v 1.5 2009/06/16 02:00:35 castalia Exp

set User = $USER
if ($?STAGE_MANAGER_LOG_DIR) then
	set Working_Dir = $STAGE_MANAGER_LOG_DIR
else
	set Working_Dir = "/HiRISE/Users/$USER/Stage_Manager"
endif
set Active_Nodes = (`list_active_nodes`)

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

			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 -[Dd]*:
			shift
			if (! $#argv) then
				echo 'Missing directory pathname.'
				echo
				goto Usage
			endif
			set Working_Dir = $1
			breaksw

		case -[Ss]*:
			shift
			if (! $#argv) then
				echo 'Missing system name(s).'
				echo
				goto Usage
			endif
			set Sys_List = ($1)
			breaksw

		case -[Rr]*:
			set User = "root"
			breaksw

		case -[Vv]*:
			set Verbose = 1
			breaksw

		case help:
		case -[Hh]*:
			Usage:
			echo 'Usage:' $0:t '[<operation>] [-Working_Directory <pathname>] [-root]'
			echo
			echo 'Manage remote System_Managers.'
			echo
			echo 'Operation is one of -'
			echo '  start'
			echo '  stop'
			echo '  restart'
			echo '    Runs the Manage_Stage_Manager <operation> on the target systems.'
			echo '  clear'
			echo '    Removes the root Stage_Manager log files on the target systems.'
			echo '  check'
			echo '    Checks for running Stage_Managers on the target systems.'
			echo 'The default operation is check.'
			echo
			echo '-Directory <pathname>'
			echo '  The pathname is set as the current working directory'
			echo '  and the Manage_Stage_Manager STAGE_MANAGER_LOG_DIR.'
			echo '  This option is ignored when -Root is specified.'
			echo '  The default may be set by $STAGE_MANAGER_LOG_DIR.'
			echo "  Default: $Working_Dir"
			echo
			echo '-System "<hostname> [...]"'
			echo '  A list of one or more system hostnames where the Stage_Manager operation will be done.'
			echo '  Multiple names must be space separated and the entire list enclosed in quotes.'
			echo "  Default (from list_active_nodes): $Active_Nodes"
			echo
			echo '-Root'
			echo '  The /etc/init.d/Stage_Manager procedure is used to start root Stage_Managers.'
			echo "  Default: /opt/local/sh/Manage_Stage_Manager is used to start $USER Stage_Managers."
			echo
			echo '-Verbose'
			echo '  List each remote command that is excecuted.'
			echo '  Default: List only each remote system used.'
			echo
			echo 'See also Manage_Stage_Manager -help'
			echo
			exit 1
			breaksw

		default:
			echo "Unknown command line argument: $1"
			echo
			goto Usage
	endsw
	shift
end

if (! $?Sys_List) then
	set Sys_List = ($Active_Nodes)
endif

if (! $?Operation) set Operation = check
if ($Operation == "check") then
	set Command = "ps auxwww | grep $User | grep PIRL.Conductor.Maestro.Stage_Manager | grep -v grep"
else if ($Operation == "clear") then
	if ($User != "root") then
		echo 'The clear operation can only be used by root.'
		echo 'The restart operation will automatically clear private logs.'
		exit 1
	endif
	set Command = "rm -f /var/log/Stage_Manager/Stage_Manager.log /var/log/Stage_Manager/Stage_Manager_Watchdog.log"
else
	if ($User == "root") then
		if ($Operation == "start") then
			set Operation = "start_now"
		endif
		set Command = "/etc/init.d/Stage_Manager $Operation"
	else
		set Command = "cd $Working_Dir; setenv STAGE_MANAGER_LOG_DIR $Working_Dir; /opt/local/sh/Manage_Stage_Manager $Operation"
	endif
endif

foreach node ($Sys_List)
	if ($?Verbose) then
		echo ssh $User@$node $Command
	else
		echo "$node -"
	endif
	ssh $User@$node "$Command"
	echo
end

exit 0

