#!/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 master version of
# the log file: isass0: /ys/site/logs/online.log is updated
#
#
# slf, 16-feb-1993
# slf, 18-feb-1993	# auto-backup stuff in atest, force GMT while logging
# slf, 15-mar-1993	# added comment characters to non-unix command lines
#			# (so the log file is actually an executable script)
# slf,  9-apr-1993      # remove non-backups before rcp (permission prob)
#			# turn on backup if atest explicitly stated in arg[2]
# slf, 19-apr-1993	# for security, reduced scope of 9-apr changes
#			  (only atest clobbering allowed)
# slf,  9-may-1993      # add sxt4.oscs.montana.edu to distribution
# slf, 11-aug-1993	# use hostlist= environ <online_hosts> if defined
# slf,  4-nov-1993 	# changes for atest breakup
# slf, 19-Jan-1994 	# only backup atest stuff
# slf,  5-May-1994 	# add FID to backup copy
# slf, 30-aug-1994	# use hostname for non SGI (fix OSF failure)
#
# Calling Sequence: (assume alias online executes this script)
#
#   online p1 p2		# p1 is local 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 ($#argv == 0) then 
      echo "ONLINE places idl routines or data files in the ys online area"
      echo ""
      echo "use command: online <routine> [/gen, /sxt, /hxt, /bcs, /wbs, /ucon]"
      echo ""
      echo "where: Routine = name of file (default is routine.pro)"
      echo "       Instrument switches are mutually exclusive and designate target"
      echo '       path (default is /gen or current value of environ online_def)'
      exit
endif
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 arg is  remote path
if ($#argv == 2) then
   switch ($2)
   case /bcs:
   case bcs:
      set outpath=/ys/atest/soft/bcs
      breaksw
   case /gen:
   case gen:
      set outpath=/ys/atest/soft/gen
      breaksw
   case /hxt:
   case hxt:
      set outpath=/ys/atest/soft/hxt
      breaksw
   case /sxt:
   case sxt:
      set outpath=/ys/atest/soft/sxt
      breaksw
   case /wbs:
   case wbs:
      set outpath=/ys/atest/soft/wbs
      breaksw
   case /ucon:
   case ucon:				# also the default
      if ("$user" == sxt_co) then
         set outpath=/ys/site/soft/rel/sxt_co
      else
         set outpath=/ys/ucon/soft/$user
      endif
      breaksw
   case /ys/atest/soft:
   case '/ys/atest/soft':
   case atest:
      if ($?online_def) then
         set outpath=/ys/atest/soft/$online_def
      else
	 set outpath=/ys/atest/soft/gen
      endif
      breaksw
   case backup:
      set outpath=/ys/atest_back/soft
      breaksw
   case home:
   case ~:
      set outpath=/0p/$user
      breaksw
   case help:
   case /help:
       $ys/gen/script/online.src 
       exit
   default:
      set outpath=$2
      breaksw
   endsw
else
   if ($?online_def) then
      set outpath=/ys/atest/soft/$online_def
   else
      echo WARNING: Default removed  \(use /bcs, /sxt, /gen, /ucon, etc or pathname\)
     exit
   endif				### unstructured exit
#   set outpath=/ys/ucon/soft/$user	# default to ucon tree
endif

set backup=0				# default is no backup
set chkback=`echo $outpath | grep /atest/`
if ($chkback != "") then
   set backup_dir=/ys/atest_back/soft
   echo atest update, auto-backup enabled
   set backup=1
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
if (!($?HOST)) set HOST=""
switch ($HOST)
   case sxt:					#sgi kludge 
        set date=`date`
        set host="sxt.space.lockheed.com"
        breaksw
   default: 
        set host=`hostname`
        set date=`date -u`				# force GMT
endsw

set year=`date +%y`
set ysdate  = $date[3]-$date[2]-$year" $date[4]  ($date[5])"
#
######################## remote host setup #################################
# define output host list: hosts
# add to this list to export to other trusted hosts

if $?online_hosts then
  set hosts=`echo $online_hosts`
else
 set hosts=( isass0.solar.isas.ac.jp sxt.space.lockheed.com )
endif
# the first node in hosts is defined as the master for loggin purposes
set master_log=$hosts[1]
set logfile=/ys/site/logs/online.log
############################################################################
#
if ($logging == 1) then 
   rsh $master_log "echo  '# $ysdate'  -  $user  -  $host >> $logfile"
endif

# now do the remote copying
set fid=`/ys/gen/script/date2fid gmt`
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
      set fname=`echo "$rfiles[1]:t"`
      set rname=$outpath/$fname[1]
#     handle auto-backups ###############################################
      if ($backup == 1) then
         set etest=`rsh $rhosts[1] "if (-e $rname) echo exist"`
         if ($etest != "") then
            set bcmd="rsh $rhosts[1] mv -f $rname $backup_dir/$fname.$fid"
            echo backing up existing file $rname...
            echo $bcmd
            rsh $master_log "echo $bcmd >> $logfile"
            $bcmd
         endif
      else					# else, no bac
#         rsh $rhosts[1] rm -f $rname		# remove file first
      endif
      ####################################################################
      echo $cmd 				   # execute & log success
      $cmd 
      if (($status == 0) & ($logging == 1)) then 
         rsh $master_log "echo $cmd >> $logfile"
      endif
      shift rhosts      			   # next host
   end
   shift rfiles					   # next file
end
# delimit this run
if ($logging == 1) then 
   rsh $master_log "echo  '# -------------------------------------------------------------------------' >> $logfile"
   rsh $master_log "echo  " " >> $logfile"
endif

exit
