#! /bin/sh
 
#    May 19/06 - F.Majaess (Revise to do multiple scans if necessary)
#    Jul 07/00 - F.Majaess (Revise to use "-mtime [+]$ndays" for SX)
#    Mar 28/96 - F.Majaess (Add cleanup of "/tmp/sh*' on orion/pollux)
#    Nov 07/91 - F.Majaess
 
#id  clntmp  - Used to clean old "tmp" file(s)/subdirectory(ies)
 
#    AUTHOR  - F.Majaess

#hd  PURPOSE - "clntmp" script is used to clean up "target" files/
#hd            subdirectories which are more than "ndays" old in
#hd            "tarpath" directory belonging to "usr". 
 
#pr  PARAMETERS:
#pr
#pr    PRIMARY
#pr
#pr      target = file/subdiretory name identifier (='tmp*')
#pr      tarpath= subdirectory in which "target" data is residing 
#pr               and is accessible from current machine (=$CCRNTMP).
#pr      ndays  = number of days on which elimination of "target" is 
#pr               based (=3)
#pr      usr    = username code of "target" owner (=$USER)
 
#ex  EXAMPLE: 
#ex
#ex          clntmp ndays=2 target='scratch*' 
#ex
 
 
#   * code used to set switches and deal with parameter=value arguments 
#   * as well as setting parameters to their primary/secondary defaults.
#   * The list of other arguments (if any) is returned in "prmtrl" var...
 
#  *  check if switches/parameters specified on script call
 
if [ $# -gt 0 ] ; then
  
#  * put argument list in "prmtrl" ...
   
  prmtrl=$@
  prmtrl=`echo $prmtrl | tr '[A-Z]' '[a-z]'`
  
#  * for each argument specified do...
    
  for prmtr in $prmtrl
  do
    case $prmtr in
 
#   * check for standard shell switches and set them accordingly
 
     --|-|[-+][aefhkntuvx]) set `echo $prmtr ` ;;
 
#   * case where a parameter is to be assigned a value
 
    *=*   ) eval $prmtr ;;
 
#   * anything else case skip...
 
    *  )  tprmtr="$tprmtr $prmtr";;
 
    esac
  done
  prmtrl=$tprmtr
  tprmtr=
 
fi
prmtrl=${prmtrl=}

#   * make sure arguments list variable "prmtrl" is initialized...
 
prmtrl=${prmtrl=}
  
#   * Skip extra parameters...
 
if [ -n "$prmtrl" ] ; then
 eval "echo '$0 : Skipped $prmtrl parameters !' "
fi
 
#   * code used to set hard coded primary defaults...
 
eval "target=${target='tmp*'}"
eval "tarpath=${tarpath=$CCRNTMP}"
eval "ndays=${ndays=3}"
eval "usr=${usr=$USER}"
 
#   * Make sure file/subdirectory to be transfered does not exist 
#   *                                                on front-end,
#   * otherwise, issue an abort message and exit.

if [ ! -d  "$tarpath/." ] ; then
 eval "0 clntmp : not able to access $tarpath subdirectory !"
 exit 0
else
  :
fi
HOSTHW=${HOSTHW:=`uname -m`}
# ntimes=10
ntimes=20
 
#set -x
#set -v
#
# `eval " find ${tarpath}/. -user $usr -name '$target' \( -mtime +$ndays -o -mtime $ndays \) -print 2> /dev/null  "  | grep -v 'failed' | grep -v 'cannot chdir to' | sed -e 's/^/\/bin\/rm -r -f /' `
## if [ "$HOSTID" = 'kaze' -o "$HOSTID" = 'asa' -o "$HOSTID" = 'hiru' -o "$HOSTID" = 'yonaka' -o "$HOSTID" = 'yata' -o "$HOSTHW" = 'SX-6' ] ; then
##  LiSt=`eval " find ${tarpath}/. -user $usr -name '$target' \( -mtime +$ndays -o -mtime $ndays \) -print 2> /dev/null  "  | grep -v 'failed' | grep -v 'cannot chdir to' | grep -v 'Permission denied' | head -6 | tr '\\012' '\\040' `
## else
 # LiSt=`eval " find ${tarpath}/. -user $usr -name '$target' -mtime +$ndays -print 2> /dev/null  "  | grep -v 'failed' | grep -v 'cannot chdir to' | grep -v 'Permission denied' | head -6 | tr '\\012' '\\040' `
 # LiSt=`eval " find ${tarpath}/. -user $usr -name '$target' -mtime +$ndays -print 2> /dev/null  "  | egrep -v 'failed|cannot chdir to|Permission denied|/JHOME_' | head -6 | tr '\\012' '\\040' `
 LiSt=`eval " find ${tarpath}/. -mindepth 1 -maxdepth 1 -user $usr -name '$target' -mtime +$ndays -print 2> /dev/null  "  | egrep -v 'failed|cannot chdir to|Permission denied|/JHOME_' | head -6 | tr '\\012' '\\040' `
## fi
while [ "$ntimes" -gt 0 -a -n "$LiSt" ] 
 do
  eval "/bin/chmod -R u+w $LiSt"
 #eval "echo /bin/chmod -R u+w $LiSt"
 #eval "/bin/ls -ld $LiSt"
 #eval "/bin/cksum $LiSt"
  eval "/bin/rm -r -f $LiSt"
 #eval "echo /bin/rm -r -f $LiSt"
  # LiSt=`eval " find ${tarpath}/. -user $usr -name '$target' -mtime +$ndays -print 2> /dev/null  "  | grep -v 'failed' | grep -v 'cannot chdir to' | grep -v 'Permission denied' | head -6 | tr '\\012' '\\040' `
  # LiSt=`eval " find ${tarpath}/. -user $usr -name '$target' -mtime +$ndays -print 2> /dev/null  "  | egrep -v 'failed|cannot chdir to|Permission denied|/JHOME_' | head -6 | tr '\\012' '\\040' `
  LiSt=`eval " find ${tarpath}/. -mindepth 1 -maxdepth 1 -user $usr -name '$target' -mtime +$ndays -print 2> /dev/null  "  | egrep -v 'failed|cannot chdir to|Permission denied|/JHOME_' | head -6 | tr '\\012' '\\040' `
  if [ -n "$LiSt" ] ; then
   ntimes=`expr $ntimes - 1 `
  else
   ntimes=0
  fi
 done
 
# If running on Linux platform, cleanup "sh*" files in "/tmp"

if [ "$OS" = 'Linux' -a "$SITE_ID" = 'Dorval' ] ; then
 cd $HOME
 \rm -f gcmsave.o* tdumper.o* tloader.o* rtdwork.o* core.*
 \rm -f subpakj.o* sxf90cl.o* sxmpif9.o* plottr.o*
 \rm -f gcmsave.l* tdumper.l* tloader.l* rtdwork.l* 
 \rm -f subpakj.l* sxf90cl.l* sxmpif9.l* plottr.l*
 \rm -f lltmpjbcmd_*.o* 
 \rm -f lltmpjbcmd_*.e*
fi
exit
