#! /bin/sh

#    Dec 03/2008 - F.Majaess (Revised for sa/saiph, ib/dorval-ib, af/alef)
#    Oct 16/06 - F.Majaess (Revised for ma/maia, ns/naos)
#    Jun 07/06 - F.Majaess (Added "xdays" switch support)
#    Mar 01/06 - F.Majaess (Revised for rg/rigel)
#    Feb 15/05 - F.Majaess (Revised for AIX/Linux setup in Victoria)
#    Jun 22/04 - F.Majaess (Added "expand" switch support)
#    Sep 02/03 - F.Majaess (Revise for "short term" dataclass use)
#    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)
#    Jun 21/95 - F.Majaess (Added orion, gandalf & aragorn)
#    Nov 08/94 - F.Majaess (Modify for new destination identifiers)
#    May 27/93 - E. Chan (Increase default time limit to 200 sec)
#    Dec 31/92 - F. Majaess (Add disk free reporting on SX3)
#    Dec 04/92 - E. Chan (Add option to set desired directory for audit)
#    Nov 26/92 - E. Chan (Add option to do audit on the symbolic links rather
#                         than on the files to which they point)
#    Nov 13/92 - E. Chan   
 
#id audit   - Submits a job to generate an audit for files residing on 
#id            the official CCRD/N file systems
 
#    AUTHOR - E. Chan  
 
#hd PURPOSE - "audit" sends an NQS job over to the appropriate machine 
#hd           and runs the script "auditor" in the official data 
#hd           directory.
#hd           By default, if the entry in the data directory is a 
#hd           symbolic link, the audit is done on the files to which the
#hd           links point rather than on the links themselves. The option 
#hd           "links" may be used to do the reverse. This is useful for
#hd           obtaining absolute path information for the file.
#hd           
#hd           Options to the "auditor" script may be placed on the "audit"
#hd           command line. These will be passed properly to "auditor". 
#hd           "Auditor" options are included in list of parameters in the
#hd           following section. For further information, use "aid" on 
#hd           "auditor".
 
#pr  PARAMETERS:
#pr  
#pr   POSITIONAL
#pr 
#pr            dir = list of files/directories - metacharacters (wildcards) 
#pr                  may be used, but they must be quoted on the command line 
#pr                  (Limited to a single target when "xdays" is set) 
#pr  
#pr    PRIMARY
#pr   
#pr
#pr      Note: One of 'ha/sp/po/ca/mz/cfs' 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         cfs = to target job to alef/pollux for cfs.
#pr
#pr          time = time limit for job in seconds (=600)
#pr  
#pr           mem = memory limit for job in megabytes (=50)
#pr  
#pr          user = username - used only in conjunction with the "own"
#pr                 parameter below (=$USER)
#pr   
#pr      datapath = path of data directory (='$DATAPATH') 
#pr   
#pr       shorterm= switch used to target the short term dataclass
#pr                 subdirectory on the file server.
#pr                 This switch is valid only in conjunction with 
#pr                 "cfs" switch.
#pr                 (=no/yes)
#pr       expand  = switch used to expand "_arc" file entries in the produced
#pr                 output by listing the contained individual files based on
#pr                 the "_arc" file log under "$CCRNSRC/arc_dir/active" 
#pr                 subdirectory.
#pr                 This switch is valid only in conjunction with "cfs" switch
#pr                 and provided the script is running on Linux.
#pr                 (=no/yes)
#pr   
#pr    SECONDARY
#pr  
#pr           own = switch to list user's own files 
#pr   
#pr          full = switch to do a recursive audit of all files in all 
#pr                 subdirectories below the directory from which "auditor"
#pr                 is invoked 
#pr  
#pr         links = switch to do the audit on the symbolic links rather than 
#pr                 on the files to which they point
#pr   
#pr       timesort= switch to allow sorting the list of files by time stamp
#pr                 (ie. invoking "-t" switch on the "ls" command).
#pr   
#pr         xdays = Cutoff number of days to base the list on. Mainly used
#pr                 to produce a list of files older than "xdays" in the
#pr                 short term dataclass on the archive server.
#pr                 (If set; it overrides "timesort" setting also no more than
#pr                          a single target subdirectory will be permitted).
#pr   
#pr        errfil = switch to include error output listing of files/directories
#pr                 that are not permitted to the user 
 
#ex EXAMPLES:
#ex   
#ex     audit po own
#ex   
#ex     The above example generates an audit of the user's own files in the
#ex     official CCRD/N data directory on the Linux (pollux).
#ex    
#ex     audit ha full
#ex    
#ex     The above example generates a recursive audit of all files residing 
#ex     in the official CCRD/N data directory on the IBM (Hadar).
#ex     The listing is sorted alphabetically by username and by filename.
#ex   
#ex     audit own po user=acrnusr subdir
#ex   
#ex     The above example generates an audit of the user acrnusr's files in the
#ex     subdirectory "subdir" of the official CCRD/N data directory on pollux.
#ex   
#ex     audit 'd* m*' ma   or:   audit 'd*' 'm*' sp
#ex   
#ex     The above example generates a sorted audit (by username and by filename)
#ex     of all files beginning with "d" and "m" in the official CCRD/N data 
#ex     directory on Spica.
#ex   
#ex     audit full cfs shorterm timesort
#ex   
#ex     The above example generates a timestamp sorted audit of all files 
#ex     archived under the invoker username's associated short term dataclass 
#ex     subdirectory on the archive server.
#ex     
#ex     audit full cfs shorterm expand xdays=75
#ex
#ex     The above will result in generating a list of files which are at least
#ex     75 days old, (ie. due to expire within 15 days), and archived under
#ex     the invoker username's associated short term dataclass subdirectory
#ex     on the archive server. The "_arc" entries are expanded to include
#ex     underneath the list of contained individual files.
#ex  
#ex     audit own cfs expand
#ex  
#ex     The above will result in generating a list of all files owned by the
#ex     invoker's username and archived under the invoker username's associated 
#ex     default dataclass subdirectory on the archive server with any "_arc" 
#ex     entries expanded to include underneath the list of contained individual 
#ex     files.
#ex  
#ex     audit full cfs expand dir='/home/cfs_ccrd/ccrd_user_archive/xxx  \
#ex                                /home/cfs_ccrd/ccrd_short_term_archive/xxx'
#ex  
#ex     The above will result in generating a list of all files archived under
#ex       "/home/cfs_ccrd/ccrd_user_archive/xxx" 
#ex     and 
#ex       "/home/cfs_ccrd/ccrd_short_term_archive/xxx"
#ex     subdirectories on the archive server with any "_arc" entries expanded 
#ex     to include underneath the list of contained individual files.

#  * Obtain any specified option. 
# set -x
for arg in "$@"
do
  case $arg in
   dir=*) dir=`echo $arg | sed -e 's/dir=//'` ;;
     own=*|full=*|links=*|timesort=*|xdays=*|cfs=*|shorterm=*|expand=*) options="$options $arg" ;;
     *=*) eval $arg                 ;;
      ha) mdest=${mdest:='hadar'}     ;;
      sp) mdest=${mdest:='spica'}     ;;
      po) mdest=${mdest:='pollux'}  ;;
      ca) mdest=${mdest:='castor'}  ;;
      mz) mdest=${mdest:='mez'}  ;;
     cfs) mdest=${mdest:='cfs'}     ;;
    mamccc) mdest=${mdest:='mamccc'}            ;;
    odin) mdest=${mdest:='odin'}            ;;
    climate) mdest=${mdest:='climate'}            ;;
    errfil) errfil=yes              ;;
         *) options="$options $arg"
  esac
done
if [ -n "$dir" ] ; then
 options="$options dir='""$dir""'"
fi

#  * Set the defaults.

user=${user:-$USER}
time=${time:=600}
mem=${mem:=50}
datapath=${datapath:='$DATAPATH'}
nonqs=${nonqs:=$NONQS}
nonqs=${nonqs:='no'}

#  * Prompt for a destination if none was specified.

while [ -z "$mdest" ]
do
 if [ "$SITE_ID" = 'Victoria' ] ; then
  #if [ "$OS" = 'Linux' ] ; then
    mdest="$HOSTID"
  #else
  # mdest='vicsmall'
  #fi
 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/cfs: > \\c"
  read tmdest
  case $tmdest in
      ha) mdest='hadar'                 ;;
      sp) mdest='spica'                 ;;
      po) mdest='pollux'                ;;
      ca) mdest='castor'                ;;
      mz) mdest='mez'                ;;
     cfs) mdest='cfs'                   ;;
       *) echo "illegal destination ! " ;;
  esac
 elif [ "$SITE_ID" = 'Downsview'  ] ; then
   mdest="$HOSTID"
 else
  echo "please enter a destination: > \\c"
  read mdest
 fi
done

#  * Construct and submit the job.

dfcmd=' '
if [ "$mdest" = 'hadar' ] ; then
  outdest=$OUTPUTQ/audit.ha.$$
  dfcmd='df -c'
elif [ "$mdest" = 'spica' ] ; then
  outdest=$OUTPUTQ/audit.sp.$$
  dfcmd='df -c'
elif [ "$mdest" = 'pollux' ] ; then
  outdest=$OUTPUTQ/audit.po.$$
elif [ "$mdest" = 'castor' ] ; then
  outdest=$OUTPUTQ/audit.ca.$$
elif [ "$mdest" = 'mez' ] ; then
  outdest=$OUTPUTQ/audit.mz.$$
elif [ "$mdest" = 'cfs' ] ; then
 #if [ "$OSbin" = '_linux64' ] ; then
   mdest='pollux'
 #else
 # mdest='alef'
 #fi
  outdest=$OUTPUTQ/audit.cfs.$$
  options="$options cfs"
else
  outdest=$OUTPUTQ/audit.$$
fi

if [ -n "$errfil" ] ; then

  #  * Include error file output. This includes a listing of 
  #  * files/directories for which permissions have not been given to 
  #  * the user running the audit.

 
 if [ "$nonqs" = 'yes' ] ; then
  if [ "$OS" = 'Linux' ] ; then
   Batch_job="/tmp/tmp_audit_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="qsub -q $mdest -lT $time -lM ${mem}mb -r audit -eo -o $outdest"
 fi

else

  #  * Do not include the error file output. This is the default.

 if [ "$nonqs" = 'yes' ] ; then
  if [ "$OS" = 'Linux' ] ; then
   Batch_job="/tmp/tmp_audit_job$$"
   RM_Batch_job="rm -f $Batch_job"
   line="cat > $Batch_job ; chmod u+x $Batch_job ; nohup $Batch_job 1>>$outdest 2>>/dev/null & " 
  else
   RM_Batch_job=':'
   line="nohup sh -s - 1>>$outdest 2>>/dev/null & "
  fi
 else
  if [ "$SITE_ID" = 'Dorval' ] ; then
    line="qsub -q $mdest -lT $time -lM ${mem}mb -r audit -o $outdest -e $mdest:/dev/null "
   #line="qsub -q $mdest -lT $time -lM ${mem}mb -r audit -o $outdest -e $outdest "
  else
    line="qsub -q $mdest -lT $time -lM ${mem}mb -r audit -o $outdest -e /dev/null "
  fi
 fi

fi
cd $HOME/tmp
# echo "auditor $options user=$user"
# (cat <<..endqsub) 
# set -x
(cat <<..endqsub) | eval "$line"
#   set -x
    cd $datapath
    auditor $options user=$user
    echo 
    $dfcmd
    ( rmllfls || : )
    $RM_Batch_job
..endqsub
exit
