#! /bin/csh -f

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

#
# shadowcopy [-R Prefix] [-v] [-s system-name] [-d] list-of-files
#
# If this is not a master system, do nothing (successfully).
#
# If list-of-files is empty, do nothing (successfully).
#
# If system-name == `hostname`, do nothing (successfully).
#
# Copy each entry in the list of files to each of the shadow
# directories.
#
# 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 any of the file names.
#
# The flag "-v" turns on a verbose mode which echoes each remote
# copy (rcp(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.
#
# 06/27/89 - Terri Fischer
#	Added findpath stuff to keep rcp from happening when
#	on the same filesystem.
#
# 07/10/89 - Terri Fischer
#	Added $target == `hostname` stuff to keep rcp from
#	happening if we're on the same machine.
#
# 09/25/89 - Terri Fischer
#	Changed ${0} to ${ProgName:t} everywhere
#	Added some error processing to rcp call.
#
# 05/26/92 - Joe Wade
#	Added two "set DoRcp" lines to get copies to happen on machines
#	other than cray sc.  Missing lines was a bug.
#
# 06/22/92 - Terri Fischer
#	Added abbreviation pn for ProgName to shorten line lengths.
#	Added kludge to get copy to happen only once if trying to go
#	to both crays, sc and sf.  This should be redone more cleanly.
#
# 07/27/93 - Joe Wade
#	Duplicated Terri's kludge for sk (the Cray C90)
#
# 11/01/94 - Joe Wade
#	Total revamping to check for mounted systems by touching a file
#       and then running a test to see if it is already there. This 
#       also eliminates the need for the MSC kludge. Many of the above
#       mods may be meaningless at this point since I removed massive
#       amounts of code to keep things clean.
#
# 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 ( ! $?DEPT_SHADOW_SYSTEMS ) then
#         echo " "
#         echo "shadowcopy:   FreeUSP bin/script remote copy facility:"
#         echo " "
#         echo "Usage:"
#         echo " shadowcopy [-R Prefix] [-v] [-s system-name] [-d] list-of-files"
#         echo " "
#         echo "Please set the env var DEPT_SHADOW_SYSTEMS to proper machines"
#         echo "and locations."
#         echo " "
#         exit 0
# endif


# If we're not on a master system, do nothing successfully.
#
# - commented out two lines - 11/02/00 - joe m. wade
# - uncommented those 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 EchoRcp = $no
set KeepRcp = ""
set exit_status=0

set ProgName=$0

# abbreviation for ProgName, to keep line lengths down
set pn = ${ProgName:t}:

set Prefix = DEPT
set ShadowMachines = ""
set filelist = ""
set debugflag  = ""
set debug = $no
set index = 1

while( $#argv > 0 )
    switch( "$1" )
	case "-R":
	    set Prefix = $2
	    shift; shift
	    breaksw
	case "-p":
	    set KeepRcp = "-p"
	    shift
	    breaksw
	case "-v":
	    set EchoRcp = $yes
	    shift
	    breaksw
	case "-s":
	    set ShadowMachines = ($ShadowMachines $2)
	    shift; shift
	    breaksw
	case "-d":
	    set debugflag="$1"
	    set debug = $yes
	    shift
	    breaksw
	default:
	    set filelist = ($filelist $1)
	    shift
	    breaksw
    endsw
end
if ($debug) echo2 $pn "The env prefix being used is >>$Prefix<<"
#
# if there's nothing to do, do it now
#
if ( $#filelist == "1" )  then
    if ( "$filelist" == "" ) exit 0
endif
#
# 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 $pn ShadowMachines are $ShadowMachines
#
# find the root paths for the shadow machines
#
if ($debug) echo2 $pn run: shadowpath -R $Prefix $debugflag $ShadowMachines

set DidRcp = `echo $ShadowMachines | sed 's/[0-z][0-z]*/FALSE/g'`
set ShadowRoots = `shadowpath -R $Prefix $debugflag $ShadowMachines`

if ( $status != 0 ) then
    echo2 ${ProgName:t} shadowpath returned with status != 0
    echo2 ${ProgName:t} quitting now.
    exit 1
endif

if ($debug) echo2 $pn the ShadowRoots are $ShadowRoots
#
# Build a test file to be checked for on remote systems
#
 	    set TestFile = "ShdCpy`date +%y%j`$$"
	    touch ${TestFile}
#
# Now send the files to the shadow machines
#
foreach target ( $ShadowMachines )
    if ( $debug ) echo2 $pn \>starting rcp query for $target\<
    set DoRcp = $no
    if ( "$ShadowRoots" == "" ) then
	echo2 $pn shadowpath returned invalid ShadowRoot for ${target}
	echo2 $pn ShadowRoot = \>"$ShadowRoot"\<
	echo2 $pn quitting now.
	exit 1
    endif

    if ( $target == `hostname` ) then
	if ( $debug ) then
	    echo2 $pn target = hostname
	    echo2 $pn do nothing on $target
	endif
	set DoRcp=$no

    else
# 
# build the file to be tested on the remote machine
#
	set NFS = `rsh -n $target '"sh"' -c '"if [ -f '${ShadowRoots[${index}]}/${TestFile} ']; then echo TRUE; fi"' `
#
# do this so that we flag the error and don't have to wait on the rcp to timeout
#
	if ( $status != 0 ) then 
	  set exit_status=1
	  set NFS = TRUE
	endif
	if ( $debug ) echo2 NFS = $NFS
	if ( "$NFS" == "TRUE" ) then
	      set DoRcp = $no
	      if ( $EchoRcp || $debug ) then
	        echo2 $pn do nothing on $target
	      endif
        else
	      set DoRcp = $yes
	endif
    endif 

    if ( $DoRcp ) then
	if ( $debug ) echo2 $pn beginning  for $target

	if ( $EchoRcp || $debug ) then
	   echo $pn run: rcp $KeepRcp $filelist ${target}:$ShadowRoots[${index}]
	endif
	rcp $KeepRcp $filelist ${TestFile} ${target}:$ShadowRoots[${index}]
	if ( $status != 0 ) then 
	  set exit_status=1
	else
	  set DidRcp[${index}] = TRUE
	endif
    endif   # $DoRcp
    set index = `expr ${index} + 1`
end

"rm" ${TestFile}

foreach target ( $ShadowMachines )
    if ( $DidRcp[1] == TRUE ) then
	rsh -n $target '"rm" '$ShadowRoots[1]/${TestFile}
    endif
    shift DidRcp
    shift ShadowRoots
end

exit $exit_status
