#! /bin/ksh

#  Brent Gordon  09 August 2000
#  Luke lin      13 February 2006  for new gens/gefs
#  Luke lin      23 May 2006       for new gens/cmce
#
# This script removes selected data files from specified
# com directories on the IBM-SP.  It expects the following
# arguments and fails if it does not have them.
#
# Arguments
# $1 -> $PDY
# $2 -> $com (i.e. /com/nam/prod)
# $3 -> $NET (i.e nam, ndas, gfs, etc.)
# $4 -> $keep (# of most recent days data directories to ignore.)
# $5 -> $rmfile (must exist.  This file specifies all of the
#                data files to be kept or deleted and may contain wildcards.
#                If the filename has the extension keeplist the files are
#                kept, otherwise they are removed)
#
#

#set -x

if [ $# -ne 5 ] ; then
  echo "You need five arguments to run this script"
  exit 911
fi

PDY=$1
com=$2
NET=$3
keep=$4
rmfile=$5

#
# Determine which data directories to ignore
#
if [ $keep -eq 0 ] ; then
  ignoredir=""
else
  let nkeep=keep-1
  ignoredir=`$utilities/finddate.sh $PDY s-${nkeep}`
  ignoredir=`echo $PDY $ignoredir`
fi

#
# Make a list of direcories to purge unwanted data files
#
ls -1d $com/${NET}.* > dirlist
for dir in `echo $ignoredir`
do
  cat dirlist | grep -v $dir > tmp
  mv tmp dirlist
done

keep_flag=`echo $rmfile | grep keeplist`

if test "$keep_flag" = ""
then
   ######################################
   # Remove Files Listed in the PARM File
   ######################################
   for dir in `cat dirlist`
   do
      echo "Thinning Directory $dir"

      for fil in `cat $rmfile`
      do
         rm -rf $dir/$fil
         # ls $dir/$fil
      done
   done
elif test "$NET" = "gefs" -o "$NET" = "cmce"
then
   cd $com/$NET.$PDYm1/00
   ls > $DATA/tmp_gefsdir
   cd $DATA

   for deldir in `cat $rmfile`
   do
     cat tmp_gefsdir | grep -v $deldir > gefs_deldir_list
     cp  gefs_deldir_list tmp_gefsdir
   done

   echo "pgrbalr" >> gefs_deldir_list
   echo "pgrba_an" >> gefs_deldir_list
   echo "pgrba_bc" >> gefs_deldir_list
   echo "pgrba_wt" >> gefs_deldir_list

   #################################################
   # Remove GEFS directories Listed in the PARM File
   #################################################
   for dir in `cat dirlist`
   do
      for subdir in `cat gefs_deldir_list`
      do
         echo "Removing Directory $dir/??/$subdir"

         rm -rf $dir/00/$subdir
         rm -rf $dir/06/$subdir
         rm -rf $dir/12/$subdir
         rm -rf $dir/18/$subdir
      done
   done
else
   ######################################
   # Keep Files Listed in the PARM File
   ######################################
   for dir in `cat dirlist`
   do
      echo "Thinning Directory $dir"

      cat /dev/null > filelist

      for fil in `cat $rmfile`
      do
         ls $dir/$fil >> filelist
      done

      for fil in `ls $dir`
      do
         if test "`grep $fil filelist`" = ""
         then
            rm -rf $dir/$fil
         fi
      done
   done
fi

exit

