#! /bin/csh -f

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

#
# shadowroot [-R Prefix] [-d] [shadow-system-list]
#
# The -d option turns on debug messages.
#
# If the current system is not a sun (or some other 'master'
#	system), silently return nothing.
#
# If command line shadow-system-list is present, set the system-list to
# 	shadow-system-list,
# Otherwise if Prefix is defined (by command line flag)
#	and if ${Prefix}_SHADOW_SYSTEMS
# 	is present in the environment, set the system-list to
# 	( ${${Prefix}_SHADOW_SYSTEMS} ),
# Otherwise if DEPT_SHADOW_SYSTEMS is present in the environment,
#	set the system-list to ( ${DEPT_SHADOW_SYSTEMS} ),
# Otherwise set the system-list to "sc".
#
# For each system in system-list:
# 	If Prefix is defined (by command line flag)
#		and if ${Prefix}_SHADOW_ROOTS
#		exists in the environment and one of its
# 		entries has the form system:pathname, write
# 		pathname to stdout,
# 	Otherwise if DEPT_SHADOW_ROOTS exists
#		in the environment and one of its
# 		entries has the form system:pathname, write
# 		pathname to stdout,
# 	Otherwise invoke rsh (for the current user) on system
#		If Prefix was defined by command line flag
#			display the value of the environmental
#			variable ${Prefix}ROOT on shadow system
#		Otherwise display the value ov the environmental
#			variable DEPTROOT on shadow system.
# 		(The environmental value checked must have
#		been set in the user's .cshrc file on system
#		since rsh doesn't read .login.)
#
# Return 0.  (The only sensible error response occurs if the
# 'rsh' above fails; you will almost surely get an error message
# which will make an impossible path.)
#
# DEPT_SHADOW_SYSTEMS is set with
# 	setenv DEPT_SHADOW_SYSTEMS	"sc gpse" ... etc.
# DEPT_SHADOW_ROOTS is set with
# 	setenv DEPT_SHADOW_ROOTS	"sc:/m/t1/trcgp gpse:/home/gpse/trcgp"
# 					  ... etc

# 09/25/89 - Terri Fischer
#	Changed ${0} to ${ProgName:t} everywhere
#
set ProgName=$0
set Prefix="DEPT"
set ShadowMachines = ""
set debug = ""

while( $#argv )
	switch( "$1" )
		case -R:
			set Prefix=$2
			shift; shift
			breaksw
		case -d:
			set debug = 1
			shift
			breaksw
		default:
			set ShadowMachines = ( $ShadowMachines $1 )
			shift
			breaksw
	endsw
end
#
# If this is not a recognized master system, silently depart.
#
issystem master
if ( $status != 0 ) exit 0
#
# Set up the default shadow system list, if not set by command line.
#
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
		endif
	endif
endif
if ($debug) echo2 ${ProgName:t}: ShadowMachines is $ShadowMachines
#
# loop over all the target systems.
#
foreach target ( $ShadowMachines )
	#
	# See if we have a shadow root in the environment for the
	# target shadow system.
	#
	set ShadowRoots = `env_val $Prefix _SHADOW_ROOTS`
	if ($debug) echo2 ${ProgName:t}: 1st try at ShadowRoots: \>$ShadowRoots\<
	if ( "$ShadowRoots" == "" ) then
		if ( $?DEPT_SHADOW_ROOTS ) then
			set ShadowRoots = $DEPT_SHADOW_ROOTS
			if ($debug) then
				echo2 -n ${ProgName:t}: 2nd try at ShadowRoots: 
				echo2 \>$ShadowRoots\<
			endif
		endif
	endif

	set Result=`echo $ShadowRoots|sed -n "/.*${target}:\([^ ]*\).*/s//\1/p"`
	if ($debug) echo2 ${ProgName:t}: "after sed-ing, Result = >${Result}<"

	if ( "$Result" != "" ) then
		echo $Result
		if ($debug) echo2 ${ProgName:t}: ShadowRoots is $Result
	else
		#
		# shadow root not in the environment.
		# interrogate the shadow system.
		#
		set Result = `rsh -n $target env_val $Prefix ROOT`
		if ($debug) echo2 ${ProgName:t}: 3rd try at ShadowRoots: \>${Result}\<
		if ( "$Result" == "" ) then
			echo2 ${ProgName:t}: cannot find ShadowRoots for ${target}
			echo2 ${ProgName:t}: quitting now.
			exit 1
		else
			echo $Result
		endif
	endif
end
