#!/bin/csh
# CVS: $Id$

# Name:   copy-snapshots_exp
# Description:
#   Copy snapshots VAR# and TAVG# from /scratch to the current data directory.
#   Should better be a Perl script.
#
#   This version uses the lists move-me.list and moved-files.list that are
#   written by the code with lcopysnapshots_exp=T (in start.in:&init_pars).
#
#   It currently runs fine with local_disc=1, one_local_disc=0.
#   For one_local_disc=1, howerver, pc_run currently resets the node list
#   to just the first node, which means that only data files under proc0/
#   are copied.

set debug = 0
foreach argv_scalar ($argv)
  if (($argv_scalar == "-v") || ($argv_scalar == "--verbose")) then
    set debug = 1          # for debugging output (recommended)
  else if (($argv_scalar == "-1")) then
    set run_only_once = 1  # check only once for files to copy
  else
    set varfile = $argv_scalar
    if ($debug) echo "varfile = <$varfile>"
  endif
end
##
## Set some necessary filesystem variables...
##
set pwd = `pwd`
set targetdir = $pwd/data
set nodelist = (`echo $NODELIST | sed 's/:/ /g'`) # unpack NODELIST
##
## ... and print out their values.
##
if ($debug) then
  echo "SCRATCH_DIR = <$SCRATCH_DIR>"
  echo "targetdir   = <$targetdir>"
  echo "nodelist    = <$nodelist>"
endif
##
## Produce debug output file in data directory
##
if($debug) then
  echo "copy-snapshots: pid=$$ starting" >> $targetdir/copy-snaps_was_here.log
  echo "copy-snapshots: pid=$$ args=<$*>" >> $targetdir/copy-snaps_was_here.log
endif
##
## If explicit filename is given, copy that file from local scratch discs
##
if ($?varfile) then
  echo "======================== $varfile ========================"
  foreach node ($nodelist)
    echo "------------------ working on node: $node ----------------------"
##
## Output directories and whereabouts of $varfile at the root processor's
## scratch disc
##
    if ($debug) then
      printf "\n  ##Checking if scratch directory is accessible\n"
      printf "\n$SSH $node \\ls -ltd $SCRATCH_DIR :\n"
      $SSH $node "\ls -ltd $SCRATCH_DIR"
      printf "\n\n  ##Checking for presence of $varfile on scratch disc\n"
      printf "\n$SSH $node \\ls -ltd $SCRATCH_DIR/proc*/$varfile $SCRATCH_DIR/allprocs/$varfile :\n"
      $SSH $node "\ls -ltd $SCRATCH_DIR/proc*/$varfile $SCRATCH_DIR/allprocs/$varfile"
      printf "\n  ##Checking for presence of $varfile in data directory\n"
      printf "\n$SSH $node \\ls -ltd $targetdir/proc*/$varfile $targetdir/allprocs/$varfile :\n"
      $SSH $node "\ls -ltd $targetdir/proc*/$varfile $targetdir/allprocs/$varfile"
      echo
    endif
##
## Copy all $varfile you find (on all nodes) to the data directory
## (not efficient on dual-CPU systems, unless we would delete files
## after copying..)
##
    if ($debug) printf "  ##Copying proc*/$varfile and allprocs/$varfile to data directory\n\n"

    set remcmd = 'cd '"$SCRATCH_DIR"'; for f in `\\ls proc*/'"$varfile"' allprocs/'"$varfile"' 2> /dev/null`; do echo "Copying '"$SCRATCH_DIR"'/$f to '"$targetdir"'/$f"; cp '"$SCRATCH_DIR"'/$f '"$targetdir"'/$f; done'

    if ($debug) echo "Now running <$SSH $node sh -c $remcmd>"

    $SSH $node sh -c "'$remcmd'"

    if ($debug) then
      printf "\n  ##Checking for changes to $varfile in data directory\n"
      printf "\n$SSH $node \\ls -ltd $targetdir/proc*/$varfile $targetdir/allprocs/$varfile :\n"
      $SSH $node "\ls -ltd $targetdir/proc*/$varfile $targetdir/allprocs/$varfile"
    endif

  end  # loop over nodes
  echo "========================================================"
else
##
## If no explicit filename is given, copy VARN, TIMEAVGN
##
  echo
  echo "=================== VARN, TIMEAVGN ====================="
##
## Check for relevant files at regular intervals
##
  while (1)
    echo
    echo "=========  "`date`"  ========="
##
## Check for files to copy by diffing the files moved-files.list
## and move-me.list in the data directory
##
    set move_me=$targetdir/move-me.list
    set eval_me=$targetdir/copy-snapshots-eval-me.csh
    set moved_files=$targetdir/moved-files.list
##
## Must make sure that both move-me.list and moved-files.list exist
##
    if (! -e $move_me) touch $move_me
    if (! -e $moved_files) touch $moved_files
    echo " ## Comparing $moved_files with $move_me"
    echo
    echo "cat $moved_files $move_me | sort | uniq -u"
##
## Only do the diff once (there could be changes in the mean time)
##
    set lines=`cat $moved_files $move_me | sort | uniq -u`
    echo
    echo $lines
    echo
##
## Consider all lines as filenames and move them from relevant node
##
    foreach file ($lines)
      set i=0            # Node number
      foreach node ($nodelist)
        echo "--------------- Moving file $file from node $node ---------------"
        echo $SSH $node \mv -f $SCRATCH_DIR/proc$i/$file $targetdir/proc$i/
        $SSH $node \mv -f $SCRATCH_DIR/proc$i/$file $targetdir/proc$i/
##
## Check exit status and output whether move was succesful or not
##
        if (! $status) then
          echo " copy-snapshots: Move successful (exit status 0)"
        else
          echo " copy-snapshots: Could not move file (exit status 1)"
          echo " copy-snapshots: Will not attempt to move file again"
        endif
        set i=`expr $i + 1`
      end
##
## Inform moved-files.list that the file has been (attempted) moved
##
      echo $file >> $moved_files
    end
##
## Execute user supplied script copy-snapshots-eval-me.csh in data directory
##
    if (-e $eval_me) then
      echo
      echo cat $eval_me
      cat $eval_me
      echo
##
## The eval command does not stop the program when encountering an error in
## the script, as opposed to source and exec.
##
      eval $eval_me >> $targetdir/copy-snapshots-eval-me.log
      \mv -f $eval_me ${eval_me}.used
    endif
##
## Break infinite while loop if set to check only once for files to copy
##
    if ($?run_only_once) break
##
## Check if parent process still alive
##
    if (`ps -p $PARENT_PID | fgrep -c $PARENT_PID` <= 0) then
      echo " copy-snapshots: No parent process (pid $PARENT_PID) -- exiting"
      exit 1
    endif
##
## Check only every 60 seconds
##
    sleep 60

  end # while(1)

endif # no input filename given
##
## Finishing output
##
if ($debug) echo " copy-snapshots: done"
echo "------------------------------------------------------ "

#TEST-BEGIN
if($debug) then
  echo "copy-snapshots: pid=$$ finished" >> $targetdir/copy-snaps_was_here.log
endif
#TEST-END

# End of file copy-snapshots
