#!/bin/sh -x
##########################################################################
#
# routine: cdbdel.sh 
#
# purpose: Runs the pipeline process that deletes obsolete reference
#		files from disk (indicated by CDBS catalog entries). 
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 10/08/98 36608  MSwam     first version
# 04/09/99 38834  MSwam     add banner print
# 12/07/99 37784  MSwam     add age test for catalog before deletions
# 05/25/00 37784_03 MSwam   change perl routine calls
# 04/14/04 48141  MSwam     add check for catalog deletions
# 
##########################################################################
banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
date
#
quit_this_dataset=1
wait_some_more=2
no_deletions_needed=3
#
# see if catalog contains any deletions (if not, nothing else to check)
num_deletions=`grep -c ' D ' ${CDB_DELETION_DIR}/${OSF_DATASET}${CATALOG_FILE_EXT}`
if [ $num_deletions = 0 ]
then
  echo "INFO: No deletions called for in catalog ${OSF_DATASET}"
  exit $no_deletions_needed
else
  echo "INFO: $num_deletions deletions called for in catalog ${OSF_DATASET}"
fi
#
# test age of release catalog
#
file_age_days=`fileage.pl ${CDB_DELETION_DIR}/${OSF_DATASET}${CATALOG_FILE_EXT} | awk '{print $4}'`
if [ $? != 0 ]
then
   echo "ERROR: Failed to compute age of catalog $OSF_DATASET"
   echo $banner
   exit $quit_this_dataset
fi
#
# if file age is too young, then exit
#
compare=`num_compare.pl $file_age_days $MIN_DELETION_AGE`
echo "compare=$compare"
if [ $compare = -1 ]
then
   # file_age_days lt MIN_DELETION_AGE, timer not yet expired...
   exit $wait_some_more
fi
#
# release catalog is "old enough", 
# run process to delete any obsolete reference files
#
cdbdel ${OSF_DATASET}${CATALOG_FILE_EXT} $PATH_FILE
save_stat=$?
#
exit $save_stat
