#!/bin/csh
#
# ##########################################################################
# script file to move files to remote nodes - assumes parellel directory
# structures exist on the machines and that they are trusted hosts.
#
# defaults setup to move idl source code to the ucon trees - 
# if the output directory is under the ys tree, the local version of
# the log file: /ys/site/logs/sw2tree.log is updated
#
#
# slf, 16-feb-1993
# slf, 18-feb-1993	# auto-backup stuff in atest
#
# Calling Sequence: (assume alias online executes this script)
#
#   online p1 p2		# p1 is file or directory to move
#				# p2 is output path 
#				# p2 may be abbreviated as:
#				#    ucon  (users /ys ucon tree)
#				#    atest (/ys/atest/soft)
#
#   online newpro [ucon]	# copies newpro.pro to users /ys ucon tree
#   online newpro atest		# copies newpro.pro to /ys/atest/soft
#   online oldpro atest		# copies oldpro.pro to /ys/atest/soft
#				# (existing file moved to /ys/atest_back/soft)
#   online userpro home		# copies userpro.pro to users home directory
############################################################################
# 1st argument is local file or directory to transfer
if (-d $1) then 
   set rfiles=`ls -a $1/*.pro`
else
   set rfiles=$1
   if (!(-e $rfiles)) then 		# user specified routine name only?
      set rfiles=`ls -a $1.pro`
   endif
endif

# second optional arg is  remote path
set backup=0				# default is no backup
if ($#argv == 2) then
   switch ($2)
   case ucon:				# also the default
      set outpath=/ys/ucon/soft/$user
      breaksw
   case atest:
      set outpath=/ys/atest/soft
      set backup=1
      set backup_dir=/ys/atest_back/soft
      echo atest update, auto-backup enabled
      breaksw
   case backup:
      set outpath=/ys/atest_back/soft
      breaksw
   case home:
   case ~:
      set outpath=/0p/$user
      breaksw
   default:
      set outpath=$2
   endsw
else
   set outpath=/ys/ucon/soft/$user	# default to ucon tree
endif

# check to see if this is a ys update (only log ys updates)
set logging=0
set ophead=`echo "$outpath:h"`
set optail=`echo "$ophead:t"`
while (($ophead != "") & ($ophead != $optail))
   if ( $ophead == "/ys" ) then 
      echo "/ys update - logging enabled"
      set logging=1
   endif
   set ophead=`echo "$ophead:h"`
   set optail=`echo "$ophead:t"`
end
#
# define some log information and log it
set date=`date`
set year=`date +%y`
set ysdate  = $date[3]-$date[2]-$year" $date[4]  ($date[5])"
set hostchk=`grep "/bin/hostname " /etc/rc.local`
if ($#hostchk > 1) then
   set host=$hostchk[2]
else
   set host=host???
endif
if ($logging == 1) then 
   echo $ysdate " - " $user " - " $host >> /ys/site/logs/sw2atest.log
endif
# define output host list
set hosts=( sxt.space.lockheed.com isass0.solar.isas.ac.jp )

# now do the remote copying
while ($#rfiles >= 1)				# for each file
   set rhosts=($hosts)			           # init host list
   while ($#rhosts >= 1)			   # for each host
      set outfile=$rhosts[1]":'$outpath'"	   # rcp output name
      set cmd="rcp -p $rfiles[1] $outfile"	   # form rcp command
      handle auto-backups ###############################################
      if ($backup == 1) then
         set fname=`echo "$outfile[1]:t"`
         set rname=$outpath/$rfiles[1]
         set etest=`rsh $rhosts[1] "if (-e $rname) echo exist"`
         if ($etest !=) then
            echo backing up existing file $rname...
            set bcmd=`rsh $rhosts[1] mv -f $rname $backup_dir`
            echo $bcmd >> /ys/site/logs/sw2atest.log
            $bcmd
         endif
      endif
      ####################################################################
      echo $cmd 				   # execute & log success
      $cmd 
      if (($status == 0) & ($logging == 1)) then 
         echo $cmd >> /ys/site/logs/sw2atest.log	   
      endif
      shift rhosts      			   # next host
   end
   shift rfiles					   # next file
end
# delimit this run
if ($logging == 1) then 
   echo  ------------------------------------------------------------------------- >> /ys/site/logs/sw2atest.log
   echo  " " >> /ys/site/logs/sw2atest.log  
endif

exit
