#! /bin/sh

#    Dec 03/08 - F.Majaess (Revised for sa/saiph, ib/dorval-ib, af/alef)
#    Oct 16/06 - F.Majaess (Revised for ma/maia, ns/naos)
#    Mar 01/06 - F.Majaess (Revised for rg/rigel)
#    Jun 20/03 - F.Majaess (Revised for az/azur)
#    Feb 25/02 - F.Majaess (Revised for ya/yata)
#    Jan 05/00 - F.Majaess (Added kz/kaze)
#    Jan 21/99 - F.Majaess (Added yo/yonaka)
#    Jul 14/97 - F.Majaess (Added sx/hiru)
#    Mar 18/96 - F.Majaess (revise for SX-4)
#    Jun 21/95 - F.Majaess (Added orion, gandalf & aragorn)
#    Nov 08/94 - F.Majaess (Modify for sx3r and new destination identifiers)
#    Jun 14/94 - E. Chan 
 
#id  getout   -  retrieves printout from old temporary directories
 
#    AUTHOR - E. Chan  
 
#hd  PURPOSE  -  "getout" submits a job to the destination machine and 
#hd              returns all printout in "dead" temporary directories 
#hd              belonging to the user.  Temporary directories are 
#hd              assumed "dead" if the printout has not been changed 
#hd              in the last 20 minutes.  Jobs aborting abnormally due 
#hd              to exceeding time/memory limits or system crashes will 
#hd              leave behind temporary directories. 
#hd 
#hd              Note that "getout" will not actually change anything 
#hd              in the temporary directories.  Even if it should for
#hd              some reason return partial printout from an "active" 
#hd              directory, it will not in any way affect the 
#hd              execution of the associated job. 

#pr  PARAMETERS: 
#pr
#pr   PRIMARY
#pr
#pr      Note: One of 'ha/sp/po/ca/mz'  in Dorval.
#pr            In Victoria, default destination: "$HOSTID"  for Linux.
#pr
#pr          ha = to target job to IBM (hadar).
#pr
#pr          sp = to target job to IBM (spica).
#pr
#pr          po = to target job to Linux (pollux).
#pr
#pr          ca = to target job to Linux (castor).
#pr
#pr          mz = to target job to Linux (mez).
#pr
#pr
#pr      time = time limit for job in seconds (=60)
#pr
#pr       mem = memory limit for job in megabytes (=25)
#pr
 
#ex  EXAMPLE: 
#ex  
#ex  getout ha
#ex
#ex     The above example submits a job to Hadar to retrieve printout 
#ex     from "dead" temporary directories belonging to the user who
#ex     invoked the script.


#  * Obtain the destination.

for arg in $@
do
  case $arg in
      -x) set $arg                     ;;
     *=*) eval $arg                    ;;
      ha) mdest=${mdest:='hadar'}       ;;
      sp) mdest=${mdest:='spica'}       ;;
      po) mdest=${mdest:='pollux'}     ;;
      ca) mdest=${mdest:='castor'}     ;;
      mz) mdest=${mdest:='mez'}        ;;
      ar) mdest=${mdest:='aragorn'}    ;;
      ga) mdest=${mdest:='gandalf'}    ;;
       *) echo " Argument $arg ignored"    
  esac
done

#  * Set the defaults.

time=${time:=60}
mem=${mem:=25}

#  * Prompt for a destination if none was specified.

while [ -z "$mdest" ]
do
 if [ "$SITE_ID" = 'Victoria' ] ; then
   mdest='vicsmall'
 elif [ "$SITE_ID" = 'Dorval' ] ; then
  echo "please enter a destination; ha/sp/po/ca/mz: > \\c"
  read tmdest
  case $tmdest in
      ha) mdest='hadar'                 ;;
      sp) mdest='spica'                 ;;
      po) mdest='pollux'                ;;
      ca) mdest='castor'                ;;
      mz) mdest='mez'                   ;;
       *) echo "illegal destination ! " ;;
  esac
 else
  echo "please enter a destination: > \\c"
  read mdest
 fi
done

#  * Construct and submit the job.

if [ "$mdest" = 'hadar' ] ; then
  outdest=$OUTPUTQ/getout.ha.$$
elif [ "$mdest" = 'spica' ] ; then
  outdest=$OUTPUTQ/getout.sp.$$
elif [ "$mdest" = 'pollux' ] ; then
  outdest=$OUTPUTQ/getout.po.$$
elif [ "$mdest" = 'castor' ] ; then
  outdest=$OUTPUTQ/getout.ca.$$
elif [ "$mdest" = 'mez' ] ; then
  outdest=$OUTPUTQ/getout.mz.$$
else
  outdest=$OUTPUTQ/getout.$$
fi

cd $HOME/tmp
qsub -q $mdest -lT $time -lM ${mem}mb -r getout \
               -eo -o $outdest  <<'endjob'
#!/bin/sh
#
cd $CCRNTMP
Stamp="${HOSTID}_"`date +%Y%j%H%M%S`
mkdir -m 755 tmp_getout_$$_$Stamp
AWK=${AWK:='awk'}
#
#  * Generate awk script to filter listing of files for only those files
#  * that have not been changed during the last 20 minutes.
#
cat > tmp_getout_$$_$Stamp/getout.$$ <<'endcat'
  BEGIN {
          # Get the current date/time stamp in the format required, minus
          # 20 minutes.
          #
          "date '+%e %H %M %m'" | getline $0
          dy = $1
          hr = $2 + 0
          mi = $3 - 20
          mn = $4 + 0
          if ( mi < 0 ) {
              mi = mi + 60
              hr = hr - 1
            if ( hr < 0 ) {
              hr = 23
              dy = dy - 1
              if ( dy == 0 ) {
                if ( mn == 5 || mn == 7 || mn == 10 || mn == 12 ) {
                  dy = 30
                }
                else if ( mn == 3 ) {
                  dy = 28
                }
                else {
                  dy = 31
                }
              }  
            }  
          }
        }  
        {
          # Extract the date/time info from an "ls -l" formated output line.
          # Then print out the filename only if the extracted date/time info
          # is older than the current date/time by 20 minutes.
          #
          day = $7
          hour = substr($8,1,2) + 0
          min = substr($8,4,5) + 0
          if ( day < dy || day == dy && hour < hr ||\
               day == dy && hour == hr && min < mi ) {
            print $9
          }
        }  
endcat
#
#  * Obtain the file list.
#
files=`ls -lR tmp*/*[._][0-9]* 2>/dev/null | grep $USER | $AWK -f tmp_getout_$$_$Stamp/getout.$$`
#
#  * Return printout only if there is anything to return.
#
if [ -n "$files" ] ; then
  #
  echo 'Files found:' ; echo ''
  #
  #  * Filter and concatenate error files to copies of the output files 
  #  * before bringing them to the back to the originating machine.
  #
  for file in $files
  do
    if [ "$flag" != on ] ; then
      outfil=`expr //$file : '.*/\(.*\)'`
      cp $file tmp_getout_$$_$Stamp/$outfil
      flag=on
    else
      : | paste "-d " - $file >> tmp_getout_$$_$Stamp/$outfil
      file_list="$file_list $outfil"
      echo $outfil
      flag=off
    fi
  done
  cd tmp_getout_$$_$Stamp
  rcp $file_list $QSUB_HOST:$QSUB_HOME/.queue 
  cd ..
else
  echo 'No files found'
fi
#
rm -r tmp_getout_$$_$Stamp
#
( rmllfls || : )
exit
endjob

exit



