
#  3 Mar 97   Author:  Hua-Lu Pan 457-5047
#  This script is to be used to manage scom subdirectories
#  which are in the form "$runid.yymmddtt".
#   To execute the script execute the command
#  sh /nwprod/util/scripts/scomkeep PDY compath ndays runid keepfile. 
#  PDY is the current date; compath is the path to the directories
#  to be managed; ndays is the number of days(to keep all files) which
#  includes the current day; runid is the runid or net name;
#  and keepfile is the name of the file containing the list of to_keep
#  files. All subdirectories beyond ndays will keep only the files in
#  the list.
#
#
# 1st argument is date stamp in form yymmdd.  This is normally
# the current date but it actually can be any valid date.
#
# This script uses the script /nw${envir}/util/ush/finddate.sh to
# build a list of dates.
#set -xua
set +x
pdy=$1
#  2nd argument is path to com directories to delete
compath=$2
#  3rd argument is total number of days to keep; includes today
numdays=$3
#   4th argument is the runid
runid=$4
#   5th arguement is the name of the file containing the keepfile
keepfile=$5

#
#  Check input arguement
#
numargs=$#
echo numargs=$numargs
if test "$numargs" -ne '5'
then 
echo Incorrect number of arguments passed to scomkeep. 
echo "$numargs arguments passed; 5 required."
exit 16
fi
 
num=`expr "$numdays" - 1`
if test "$num" -lt 0
then
  num=0
fi
echo num=$num

#
#  Make a variable that contains all the days that we do not want to
#  touch
#

date_string=`sh /nw${envir}/util/ush/finddate.sh $pdy s-${num}`
echo date_string="${date_string}"


#
# get current directory
#

curdir=`pwd`
echo $curdir

echo 'Current com directory is - '
echo "$compath"

#
# Make sure /com files are established
#

mkdir -p $compath

#
# get all directories in this com directory.
#

ls -p ${compath} | grep / > field
if  test "$runid"
then
grep ${runid} field | grep -v ${pdy} > field1
else
grep -v ${pdy} field > field1
fi

#
#  make a list of all directories beyond ndays
#

#set +xS
for day in ${date_string}
do
  cat field1>field
  grep -v $day field >field1
done

cat field1
#set -xS


#
#  For all directories beyond ndays, we move the files in keepfile
#    to $TMPDIR, clean up the directory, and move the to-keep files
#    back
#

if test -s field1
then                                                                            
  lisdir=`cat < field1 | cut -d/ -f1 `                                          
  if test "$lisdir"                                                             
  then                                                                          
    cd $compath                                                                 
    for dirname in $lisdir                                                      
    do                                                            
    delete=no
    if test $runid
    then
      id=`echo $dirname | cut -f1 -d '.'`
      adate=`echo $dirname | cut -f2 -d '.' `
      ts=`echo $adate | cut -c1-6`
      if test $pdy -gt $ts
      then
        delete=yes
      fi
    else
      ts=`echo $dirname | cut -c1-6`
      if test $pdy  -gt $ts
      then
        delete=yes
      fi
    fi
    if test $delete = 'yes'
    then
      cd $dirname

#
#  Make a list of the files in $dirname
#

      ls > $curdir/list1
      nf1=` cat $curdir/list1 | wc -l `
      echo " Number of files in $dirname =" $nf1
#
#  list all files in $keepfile, sort that list,
#  and diff the list with list1 to form the list
#  of files to delete
#
      sed 's/^.*/ls &/' $keepfile > $curdir/mklist.sh
      chmod 775 $curdir/mklist.sh
      sh $curdir/mklist.sh > $curdir/list2
      sort $curdir/list2 > $curdir/list3
      nf2=` cat $curdir/list3 | wc -l `
      echo " Number of files to keep in $dirname =" $nf2
      diff $curdir/list3 $curdir/list1 > $curdir/list4
      if test -s $curdir/list4
      then
#
#  Make the delete script
#
        grep '>' $curdir/list4 > $curdir/list5
        nf3=` cat $curdir/list5 | wc -l `
        echo " Number of files to delete in $dirname =" $nf3
        sed 's/>/rm -f/' $curdir/list5 > $curdir/rmlist.sh
        chmod 775 $curdir/rmlist.sh
        sh $curdir/rmlist.sh
      fi
#      ls -l
      cd ..
    fi
  done
  fi
fi
# return to working directory
cd $curdir

rm field field1 list1 list2 list3 list4 list5 mklist.sh rmlist.sh

exit
