#! /bin/csh -f

########################################################################
#                 copyright 2001, Amoco Production Company             #
#                             All Rights Reserved                      #
#                     an affiliate of BP America Inc.                  #
########################################################################

#
# shadowexec [-R Prefix] [-v] [-s system-name] [-d] command-line
#
# If this is not a master system, do nothing (successfully).
#
# If command-line is empty, do nothing (successfully).
#
# Execute command-line in the shadow directory on each of the 
# shadow systems.
#
# One or more flags of the form "-s system-name" (with a space
# after "-s" as in "-s sc" or "-s gpse") can be used to replace
# the default system list (see shadowroot(1) and shadowpath(1))
# with user specified shadow systems.  All of the system specifiers
# must precede the command-line.
#
# The flag "-v" turns on a verbose mode which echoes each remote
# shell (rsh(1)) command to stdout as it's executed.
#
# A flag of the form "-R Prefix" may
# be used to tell the shadow programs to use the
# given prefix instead of DEPT for environment variables.
#
# The flag "-d" turns on debug mode for all shadow programs.
	
# 09/25/89 - Terri Fischer
#	Changed ${0} to ${ProgName:t} everywhere
#
# 11/02/00 - Joe Wade
#       Took out this upcoming check for DEPT_SHADOW_SYSTEMS. There are
#       many other XXXX_SHADOW_SYSTEMS that could be getting used and
#       if none are set, we should exit gracefully anyway.
#
# 11/02/00 - Joe Wade
#       Took out "issystem master" check
#	Changed exit code for "No Shadow Machines found" to be 0, although
#	the message will still be printed
#
# 07/12/01 - Joe Wade
#	Put the "issystem master" check back in. The "issystem" program 
#	has now been changed to reference the environment variable
#       FreeUSP_SystemType

# If we're not on a master system, do nothing successfully.

# if ( ! $?DEPT_SHADOW_SYSTEMS ) then
# 	echo " "
#       echo "shadowexec:   FreeUSP bin/script remote execution facility:"
# 	echo " "
# 	echo "Usage:"
# 	echo "	shadowexec [-R Prefix] [-v] [-s system-name] [-d] command-line"
# 	echo " "
# 	echo "Please set the env var DEPT_SHADOW_SYSTEMS to proper machines"
# 	echo "and locations."
# 	echo " "
# 	exit 0
# endif

# - commented out two lines - 11/02/00 - joe m. wade
# - uncommented these same two lines - 07/12/01 - joe m. wade
issystem master
if ( $status != 0 ) exit 0
#
# Parse the arguments
#
set no=0
set yes=1
set exit_status=0

set ProgName=$0
set Prefix = DEPT
set EchoRsh = $no
set ShadowMachines = ""
set commandline = ""
set debugflag  = ""
set debug = $no

while( $#argv > 0 )
	switch( "$1" )
		case "-R":
			set Prefix = $2
			shift; shift
			breaksw
		case "-v":
			set EchoRsh = $yes
			shift
			breaksw
		case "-s":
			set ShadowMachines = "$ShadowMachines $2"
			shift; shift
			breaksw
		case "-d":
			set debugflag="$1"
			set debug = $yes
			shift
			breaksw
		default:
			set commandline = "$commandline $1"
			shift
			breaksw
	endsw
end
if ($debug) echo2 ${0}: "The env prefix being used is >>$Prefix<<"
#
# if there's nothing to do, do it now
#
if ( "$commandline" == "" ) exit 0 
#
# if there's no shadow machine list, find the default one.
#
if ( "$ShadowMachines" == "" ) then
	set ShadowMachines = `env_val $Prefix _SHADOW_SYSTEMS`
	if ( "$ShadowMachines" == "" ) then
		if ( $?DEPT_SHADOW_SYSTEMS ) then
			set ShadowMachines = $DEPT_SHADOW_SYSTEMS
		endif
#		if ( "$ShadowMachines" == "" ) set ShadowMachines = "sc"
		if ( "$ShadowMachines" == "" ) then
			echo2 ${ProgName:t} No Shadow Machines found
			echo2 ${ProgName:t} quitting now
#			exit 1
			exit 0
		endif
	endif
endif
if ($debug) echo2 ${ProgName:t}: ShadowMachines are $ShadowMachines
#
# find the root paths for the shadow machines
#
if ($debug) echo2 ${ProgName:t}: calling: shadowpath -R $Prefix $debugflag $ShadowMachines

set ShadowRoots = `shadowpath -R $Prefix $debugflag $ShadowMachines`

if ($debug) echo2 ${ProgName:t}: the ShadowRoots are \>"$ShadowRoots"\<
#
# Now execute the commands(s) on the shadow machines.
#
foreach target ( $ShadowMachines )
	if ( $debug ) echo2 ${ProgName:t}: \>\>starting rsh for $target\<\<
	if ( "$ShadowRoots" == "" ) then
		echo2 ${ProgName:t}: shadowpath returned invalid ShadowRoot for ${target}
		echo2 ${ProgName:t}: quitting now.
		exit 1
	endif
	if ( $EchoRsh  || $debug ) then
		echo2 -n ${ProgName:t}: run: rsh -n ${target}\ 
		echo2 "cd $ShadowRoots[1]; $commandline"
	endif
	rsh -n ${target} "cd $ShadowRoots[1]; $commandline"
	shift ShadowRoots
end	# foreach
exit $exit_status
