#  2 Feb 96   Author:  Bill Facey 457-5047
#  This script is to be used to manage (delete) com and scom subdirectories
#  which are in the form "$runid.yymmddtt" where runid and tt
#  are optional.  To execute the script execute the command
#  sh /nwprod/util/scripts/rmcomdir PDY compath ndays runid. 
#  PDY is the current date; compath is the path to the directories
#  to be managed; ndays is the number of days to keep which
#  includes the current day; and, runid is the runid or net name.
#
#  This script will not delete directories containing either todays
#  date or some future date.
#
# 1st argument is date stamp in form yyyymmdd.  This is normally
# the current date but it actually can be any valid date.                
#
# This script uses the script /nwprod/util/scripts/lookitup to 
# build a list of dates.
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 and is required to be passed if the 
#   directory contains the runid in its name.  Otherwise do not include it.

numargs=$#
echo numargs=$numargs
if test "$numargs" -eq '3'
then 
unset runid
elif test "$numargs" -eq '4'
then 
runid=$4
else
echo Incorrect number of arguments passed to rmcomdir. 
echo "$numargs arguments passed; 3 or 4 required."
exit 16
fi
if test $runid
then 
echo runid=$runid
else 
echo The variable runid does not exist
fi
 
num=`expr "$numdays" - 1`
if test "$num" -lt 0
then
  num=0
fi
echo num=$num

date_string="`sh /nwprod/util/ush/finddate.sh $pdy s-${num}` $PDYp1"
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

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

cat field1

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
      echo "Delete directory ${dirname} in the path $compath."           
      rm -rf $dirname
    fi                                                              
    done                                                                        
  fi                                                                            
fi                                                                              
# return to working directory                                                   
cd $curdir                                                               

exit
