#!/bin/tcsh -fx
#------------------------------------------------------------------------
#                          REVISION HISTORY                             -
#                                                                       -
#------------------------------------------------------------------------
#  MOD            PR                                                    -
# LEVEL   DATE   NUMBER  USER     DESCRIPTION
# ----- -------- ------  -------  ------------------------------------  -
#  000  10/26/01 44694   J.Baum  Initial version 
#       09/10/12 70213   MSwam   use tcsh
#------------------------------------------------------------------------
#
# Module:	delete_save.csh
#
#------------------------------------------------------------------------
# DESCRIPTION:
#
#  This script deletes a file based on three variables $INPATH, $OSF_DATASET,
#  and $EXTENSION. These are required variables. If the INPATH is invalid, the
#  process will go absent. If no files exist a warning message is generated but
#  the process returns a success status. If any file exists that cannot be
#  deleted a failure status is returned. The non-existence, failure of success
#  is logged.
#
#*********************************************************************
#
set go_absent=102          #this exit status is in the FATAL range for XPOLL
set return_success=0       #completed or no file
set return_failure=1       #cannot delete file
#
# Validate INPATH
if (!(-d $INPATH)) then
   echo -n `\date +"%Y%j%H%M%S"`
   echo "-F-FATAL ERROR: invalid directory - $INPATH"
   exit $go_absent
endif
#
# Construct file mask from input variables
set noglob
set spec_mask = \
  ${INPATH}`echo $OSF_DATASET | sed "s/^...\(.*\)/\*\1/"`
set spec_mask = $spec_mask$EXTENSION  
#
unset noglob
set file_list = `echo $spec_mask`   # translate wildcards in spec
if ($#file_list != 0) then
  foreach i ($file_list)
    rm $i
    if (-e $i) then
      echo -n `\date +"%Y%j%H%M%S"`
      echo "-E-ERROR: cannot delete $i"
      exit $return_failure
    else
      echo -n `\date +"%Y%j%H%M%S"`
      echo "-I-SUCCESS: deleted $i"
    endif
  end
else
   echo -n `\date +"%Y%j%H%M%S"`
   echo "-W-WARNING: no files found for $spec_mask"
endif
exit $return_success
