#! /bin/csh -f

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

#
# shadowpath [-R Prefix] [-d] [shadow-system-list]
#
# If the current system is not a sun system (or other 'master'
# 	system), return nothing.
#
# Otherwise try to map the present working directory, as a
# 	SubDirectory of the directory named in the
# 	environmental variable ${Prefix}ROOT or by default,
# 	DEPTROOT, onto a SubDirectory
# 	of the value returned by `shadowroot [shadow-system]`,
# 	and return the shadow path.
# Otherwise announce failure.
#
# Nota bene:  this script does not verify that the contructed
# path actually exists.
#
# The -d option turns on debug messages.
#

# 09/25/89 - Terri Fischer
#	Changed ${0} to ${ProgName:t} everywhere.
#	Changed sed script: deleted -n, added regexp stuff.
#

set ProgName=$0
set Prefix="DEPT"
set ShadowMachines = ""
set debug = ""
set debugflag  = ""

while ( $#argv )
	switch( "$1" )
		case -R:
			set Prefix=$2
			shift; shift
			breaksw
		case -d:
			set debug = 1
			set debugflag  = "$1"
			shift
			breaksw
		default:
			set ShadowMachines =  ( $ShadowMachines $1 )
			shift
			breaksw
	endsw
end

if ($debug) echo2 ${ProgName:t}: after parsing args

set RootToUse=`env_val $Prefix ROOT`

if ( "$RootToUse" == "" ) then
	echo2 ${ProgName:t}: could not find a value for ${Prefix}ROOT
	echo2 ${ProgName:t}: must have DEPTROOT defined or use -R and your own variable
	exit 1
endif
if ($debug) echo2 ${ProgName:t}: RootToUse is $RootToUse
#
# if the present system is not a master system, go home quietly.
#
issystem master
if ( $status != 0 ) exit 0
#
# Set up the default shadow system list.
#
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 are $ShadowMachines
#
# capture the list of shadow roots
#
if ($debug) echo2 ${ProgName:t}: calling: shadowroot -R $Prefix $debugflag $ShadowMachines

set ShadowRoots = `shadowroot -R $Prefix $debugflag $ShadowMachines`
if ( $status != 0 ) then
	echo2 ${ProgName:t} ShadowRoots is \>$ShadowRoots\<
	echo2 ${ProgName:t} quitting now
	exit 1
endif

if ($debug) echo2 ${ProgName:t}: ShadowRoots is $ShadowRoots
#
# get the current directory's ~trcgp path (if it exists)
#
set new_cwd= (`echo $cwd | sed -e "s,^/tmp.*/net/[^/]*\(.*\),\1,p"`)
if ($debug) echo2 change \$cwd = $cwd to \$new_cwd = $new_cwd

set SubDir = `symname $debugflag $new_cwd`
if ($debug) echo2 ${ProgName:t}: symname $debugflag $new_cwd = $SubDir

#
# for each shadowroot, emit the current path (SubDir) with
# the local RootToUse replaced by the shadow machine's root, $root.
#
foreach root ( $ShadowRoots )
	if ($debug) echo2 ${ProgName:t}: replacing $RootToUse by $root in $SubDir
#
# the p at the end of the sed command causes problems on solaris and I 
# fail to see its purpose - j.m.wade
#
#	set NewPath=`echo $SubDir|sed -e "s,^${RootToUse}\(.*\),${root}\1,p"`
	set NewPath=`echo $SubDir | sed -e "s,^${RootToUse}\(.*\),${root}\1,"`
#
# added the trailing "/" so if the dir doesn't exist, a file is not created
# by that name during shadowcopy - j.m.wade
#
	echo $NewPath/
	if ($debug) echo2 ${ProgName:t}: shadow path will be $NewPath
end
exit 0
