#! /bin/ksh

#  Brent Gordon  09 August 2000
#
# 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 deleted and may contain wildcards.)
#
#

#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

#
# Now remove unwanted files
#
for dir in `cat dirlist`
do
  for fil in `cat $rmfile`
  do
    rm $dir/$fil
#    ls $dir/$fil
  done
done

exit

