#! /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)
#    Feb 15/05 - F.Majaess (Revised for AIX/Linux setup in Victoria)
#    Jun 20/03 - F.Majaess (Revised for az/azur)
#    Feb 25/02 - F.Majaess (Revised for ya/yata)
#    Sep 14/00 - F.Majaess (Revised for tu/tsunami)
#    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 14/96 - F.Majaess (Added sx4)
#    Jan 09/96 - FM (Revise to use access)
#    Jun 21/95 - F.Majaess (Added orion, gandalf & aragorn)
#    Nov 08/94 - F.Majaess (Modify for new destination identifiers)
#    Nov 25/92 - E. Chan (Modify to use links on the official CCRD/N data
#                         directory to move into directory where the file
#                         actually resides)
#    Nov 13/92 - E. Chan   
 
#id  inventory - obtains a ggstat of a file residing on another machine.
 
#    AUTHOR - E. Chan  
 
#hd  PURPOSE  -  "inventory" submits a job to the specified machine and runs 
#hd              the program ggstat on the specified file. The file may reside
#hd              in any of the official CCRD/N file systems as long as a 
#hd              symbolic link to that file exists in the official CCRD/N
#hd              data directory. 
#hd 
#hd              The latest edition is used by default. This may be overidden
#hd              using the "ed" pararmeter described below.
 
#pr  PARAMETERS:
#pr  
#pr    POSITIONAL     
#pr  
#pr      file   = name of permanent file 
#pr  
#pr    PRIMARY
#pr   
#pr      Note: One of 'hr/br/dy/ba/p1/p2/p3/p4'  in Dorval.
#pr            In Victoria, default destination: "$HOSTID"  for Linux.
#pr
#pr          hr = to target job to Linux (hare).
#pr
#pr          br = to target job to Linux (brooks).
#pr
#pr          dy = to target job to Linux (daley).
#pr
#pr          ba = to target job to Linux (banting).
#pr
#pr          p1 = to target job to Linux (ppp1).
#pr
#pr          p2 = to target job to Linux (ppp2).
#pr
#pr          p3 = to target job to Linux (ppp3).
#pr
#pr          p4 = to target job to Linux (ppp4).
#pr
#pr  
#pr      time   = time limit for job on the back end in seconds (=60)
#pr  
#pr       mem   = memory limit for job on the back end in megabytes (=150)
#pr  
#pr        ed   = edition number of file (defaults to the latest edition)
#pr  
#pr      path   = path of ggstat executables (='$RTEXTBLS/lopgm')
 
#ex  EXAMPLE: 
#ex  
#ex  inventory filex br ed=3
#ex  
#ex  The above example generates a ggstat of the file "filex.003" on 
#ex  the official data directory on Brooks.
#ex  
#ex  inventory filey p1 path='\$RTEXTBLS/lopgmhr'
#ex  
#ex  The above example generates a ggstat of the highest edition of file 
#ex  "filey" on the official data directory using the ggstat executable
#ex   in $RTEXTBLS/lopgmhr on ppp1.

#  * Obtain any specified option. 

for arg in $@
do
  case $arg in
      -x) set $arg                           ;;
     *=*) eval $arg                          ;;
    ed=*) ed=`expr $arg : 'ed=\([0-9]*\)'`   ;;
      hr) mdest=${mdest:='hare'}       ;;
      br) mdest=${mdest:='brooks'}       ;;
      dy) mdest=${mdest:='daley'}       ;;
      ba) mdest=${mdest:='banting'}       ;;
      p1) mdest=${mdest:='ppp1'}       ;;
      p2) mdest=${mdest:='ppp2'}       ;;
      p3) mdest=${mdest:='ppp3'}       ;;
      p4) mdest=${mdest:='ppp4'}       ;;
    mamccc) mdest=${mdest:='mamccc'}         ;;
    odin) mdest=${mdest:='odin'}             ;;
    climate) mdest=${mdest:='climate'}       ;;
       *) file=${file:=$arg}
  esac
done

#  * Set the defaults.

time=${time:=60}
mem=${mem:=250}
# set -x
path=${path:='$RTEXTBLS/lopgm'}
# set +x
nonqs=${nonqs:=$NONQS}
nonqs=${nonqs:='no'}
 
#  * Prompt for a filename if none was specified.
 
while [ -z "$file" ] 
do
  echo "please enter file name: > \\c"
  read file
done
 
#  * Prompt for a destination if none was specified.
 
while [ -z "$mdest" ]
do
 if [ "$SITE_ID" = 'Victoria' ] ; then
    mdest="$HOSTID"
 elif [ "$SITE_ID" = 'York' ] ; then
   mdest="$HOSTID"
 elif [ "$SITE_ID" = 'Utor1' -o "$SITE_ID" = 'UOFT' ] ; then
   mdest="$HOSTID"
 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
 elif [ "$SITE_ID" = 'Drvlsc' ] ; then
  echo "please enter a destination; hr/br/dy/ba/p1/p2/p3/p4: > \\c"
  read tmdest
  case $tmdest in
      hr) mdest='hare'                 ;;
      br) mdest='brooks'                 ;;
      dy) mdest='daley'                 ;;
      ba) mdest='banting'                 ;;
      p1) mdest='ppp1'                ;;
      p2) mdest='ppp2'                ;;
      p3) mdest='ppp3'                ;;
      p4) mdest='ppp4'                ;;
       *) echo "illegal destination ! " ;;
  esac
 elif [ "$SITE_ID" = 'Downsview' ] ; then
    mdest="$HOSTID"
 else
  echo "please enter a destination: > \\c"
  read mdest
 fi
done
 
#  * Remove leading zeros in the edition number and ensure that  
#  * three digits are used if it is specified.
 
if [ -n "$ed" ] ; then
  ed=`expr $ed + 0`
  if [ "$ed" -lt 100 ] ; then
    ed=0$ed
  fi
  if [ "$ed" -lt 10 ] ; then
    ed=0$ed
  fi
  Ted="ed=$ed"
  DspTed=".$ed"
fi

#  * Construct and submit the job.

if [ "$mdest" = 'hare' ] ; then
  outdest=$OUTPUTQ/inv_${file}.hr.$$
elif [ "$mdest" = 'brooks' ] ; then
  outdest=$OUTPUTQ/inv_${file}.br.$$
elif [ "$mdest" = 'banting' ] ; then
  outdest=$OUTPUTQ/inv_${file}.ba.$$
elif [ "$mdest" = 'daley' ] ; then
  outdest=$OUTPUTQ/inv_${file}.dy.$$
elif [ "$mdest" = 'ppp1' ] ; then
  outdest=$OUTPUTQ/inv_${file}.p1.$$
elif [ "$mdest" = 'ppp2' ] ; then
  outdest=$OUTPUTQ/inv_${file}.p2.$$
elif [ "$mdest" = 'ppp3' ] ; then
  outdest=$OUTPUTQ/inv_${file}.p3.$$
elif [ "$mdest" = 'ppp4' ] ; then
  outdest=$OUTPUTQ/inv_${file}.p4.$$
else
  outdest=$OUTPUTQ/inv_${file}.$$
fi

if [ "$SITE_ID" = 'Victoria' ] ; then
 unset NOCP
else
 NOCP=${NOCP:-'nocp'}
fi
if [ "$nonqs" = 'yes' ] ; then
 if [ "$OS" = 'Linux' ] ; then
  Batch_job="/tmp/tmp_inventory_job$$"
  RM_Batch_job="rm -f $Batch_job"
  line="cat > $Batch_job ; chmod u+x $Batch_job ; nohup $Batch_job 1>>$outdest 2>>$outdest & " 
 else
  RM_Batch_job=':'
  line="nohup sh -s - 1>>$outdest 2>>$outdest & "
 fi
else
 line="$CCCQSUB -q $mdest -lT $time -lM ${mem}mb -r inv -eo -o $outdest"
fi

RM_Batch_job=${RM_Batch_job:-':'}
cd $HOME/tmp
# qsub -q $mdest -lT $time -lM ${mem}mb -r inv.$file \
# qsub -q $mdest -lT $time -lM ${mem}mb -r inv \
#                -eo -o $outdest <<endjob
(cat <<endjob) | eval "$line"
#!/bin/sh
# set -x
echo " " ; echo "                  Job executing on HOSTID=\${HOSTID}: \`date\` " ; echo " "
echo " " ; echo "                  Job executing on hostname=\`hostname\`: \`date\` " ; echo " "
#
# F_PROGINF=NO
# export F_PROGINF
Stamp="\${HOSTID}_"\`date +%Y%j%H%M%S\`
mkdir -m 755 \$CCRNTMP/tmp_inventory_\$\$_\$Stamp
cd \$CCRNTMP/tmp_inventory_\$\$_\$Stamp
echo " " ; echo "Directory: \`pwd\`" ; echo " "
#
#  * Access the file and process it's contents.
#  * The latest edition is used if the edition number
#  * is not specified.
#
access ${file}${DspTed} ${file} $Ted $NOCP &&
${path}/ggstat ${file}${DspTed}  &&
cd \$CCRNTMP &&
\rm -r -f \$CCRNTMP/tmp_inventory_\$\$_\$Stamp &&
( rmllfls || : ) && $RM_Batch_job &&
exit 0 ||
echo "Error: file ${file}${DspTed} does not exist or problem in doing ggstat" &&
cd \$CCRNTMP &&
\rm -r -f \$CCRNTMP/tmp_inventory_\$\$_\$Stamp &&
( rmllfls || : ) &&
$RM_Batch_job &&
exit 1 || ( rmllfls || : ) && $RM_Batch_job && exit 2
endjob

exit
