#!/bin/sh
#
# Routine: clear_otfr
#
# Description:
#  This script is used to clear out the OTFR working directories
#  when a complete shutdown and cleaning of OTFR is needed.
#
#  It removes all traces of all OTFR requests that were being
#  processed from both the OTFR and OTFR-Science pipelines.
#
#  It is assumed that the all processes have already been halted via
#  the PMG, and that all OSFs have been deleted via the OMG, in both
#  the OTFR and OTFR-science paths.
#
#  This script is hard-coded with the base names of specific path file
#  entries in the OTFR and OTFR-Science paths.  If the conventions that
#  these mnemonics follow gets changed, the script needs a tweak.
#
#  Here are the current hard-coded OTFR path strings searched for:
#      "_recalib" not including "base_"
#      "local_otfr_request_"
#
#  and the OTFSCI path strings searched for:
#      "CAL_DIR"
#      "SIS_DIR"
#      "POD_"
#
# modification history:
#
#   date    opr     who      reason
# -------- -----    -------- --------------------------------------
# 01/15/01 43165    MSwam    first version
# 03/14/01 43165_06 MSwam    use rm -f
# 03/29/01 43165_10 MSwam    add OSF deletes
# 01/07/02 44965    MSwam    fix path entry names
# 08/21/03 48370    Sherbert Add missing /bin/ qualifier to awk, rm, tr, wc
# 08/21/03 48370    Sherbert Add missing "-1" param to ls 
#
#-------------------------------------------------------------------------
#
go_absent=1
#
# verify usage
if [ $# != 3 ]
then
    echo "usage: clear_otfr otfr_pathname otfsci_pathname reserv_pathname"
    exit 1
fi
otfr_pathname=$1  # the path to delete requests from
otfsci_pathname=$2 # the path to delete science datasets from
reserv_pathname=$3 # the path to delete reservation osfs from
#
# resolve the full path file names
otfr_fullpath=`osfile_stretch_file OPUS_DEFINITIONS_DIR:${otfr_pathname}.path`
if [ $? != 0 ]
then
   echo "Failed to resolve filename to path: $otfr_pathname"
   exit 1
fi
otfsci_fullpath=`osfile_stretch_file OPUS_DEFINITIONS_DIR:${otfsci_pathname}.path`
if [ $? != 0 ]
then
   echo "Failed to resolve filename to path: $otfsci_pathname"
   exit 1
fi
reserv_fullpath=`osfile_stretch_file OPUS_DEFINITIONS_DIR:${reserv_pathname}.path`
if [ $? != 0 ]
then
   echo "Failed to resolve filename to path: $reserv_pathname"
   exit 1
fi
#-------------------------------------------------------------
# START OTFR directories
#
# locate the calibration root directories for each instrument in the path
for target_dir in `grep _recalib $otfr_fullpath | grep -v '^!' | grep -v base_ | awk '{print $3}'`
do
 echo
 echo "checking $target_dir"
 if [ ! -d $target_dir ]
 then
   echo "ERROR: target_dir $target_dir directory does not exist"
   continue
 fi
#
# move to the WF2 directory and delete all files
 cd $target_dir
 if [ $? != 0 ]
 then
   echo "ERROR: Failed to set directory to $target_dir"
   continue
 fi
#
# test for files
 anyfiles=`ls -1 $target_dir/* | wc -l`

 if [ $anyfiles != 0 ]
 then
    #prompt for deletion
    echo " "
    echo "HEY! $anyfiles Files were found in directory $target_dir"
    echo " "
    echo -n "Do you REALLY WANT THESE DELETED!! (y or n)? "
    read response
    ans=`echo $response | tr '[A-Z]' '[a-z]'`
    echo
    if [ $ans = 'y' ]
    then
       echo
       echo "FILE DELETION NOW OCCURRING..."
       rm -Rf *
       if [ $? != 0 ]
       then
           echo "ERROR: File deletion failed in $target_dir"
           exit 1
       fi
       echo
    else
       echo
       echo "OK. Deletions will be skipped for $target_dir."
       echo
    fi
 fi
done
#-------------------------------------------------------------
# START REQUEST FILES
#
# locate the incoming request directory
for target_dir in `egrep "local_otfr_request_" $otfr_fullpath | grep -v '^!' | awk '{print $3}'`
do
 if [ $? != 0 ]
 then
   echo "Failed to find local_otfr_request_ in path file $otfr_fullpath"
   exit 1
 fi
 echo
 echo "checking $target_dir"
 if [ ! -d $target_dir ]
 then
   echo "ERROR: $target_dir directory does not exist"
   continue
 fi
#
# move to the directory and delete all files
 cd $target_dir
 if [ $? != 0 ]
 then
   echo "ERROR: Failed to set directory to $target_dir"
   continue
 fi
 anyfiles=`ls -1 ?????????????_* | wc -l`
 if [ $anyfiles != 0 ]
 then
    #prompt for deletion
    echo " "
    echo "HEY! $anyfiles Files were found in directory $target_dir"
    echo " "
    echo -n "Do you REALLY WANT THESE DELETED!! (y or n)? "
    read response
    ans=`echo $response | tr '[A-Z]' '[a-z]'`
    echo
    if [ $ans = 'y' ]
    then
       echo
       echo "FILE DELETION NOW OCCURRING..."
       rm -Rf ?????????????_*
       if [ $? != 0 ]
       then
           echo "ERROR: File deletion failed in $target_dir"
           exit 1
       fi
       echo
    else
       echo
       echo "OK. Deletions will be skipped in $target_dir."
       echo
    fi
 fi
done
#
#-------------------------------------------------------------
# START SCIENCE FILES
#
for target_dir in `egrep "CAL_DIR|SIS_DIR|POD_" $otfsci_fullpath | grep -v '^!' | awk '{print $3}'`
do
 if [ $? != 0 ]
 then
   echo "ERROR: Failed to find SCIENCE dirs in path file $otfsci_fullpath"
   exit 1
 fi
 fullpath=`osfile_stretch_file ${target_dir}`
 if [ $? != 0 ]
 then
   echo "ERROR: Failed to resolve filename for $target_dir"
   continue
 fi
 target_dir=$fullpath
 echo
 echo "checking $target_dir"
 if [ ! -d $target_dir ]
 then
   echo "ERROR: $target_dir directory does not exist"
   continue
 else
#
# move to the directory and delete all files
 cd $target_dir
 if [ $? != 0 ]
 then
   echo "ERROR: Failed to set directory to $target_dir"
   continue
 fi
 anyfiles=`ls -1 ?????????* | wc -l`
 if [ $anyfiles != 0 ]
 then
    #prompt for deletion
    echo " "
    echo "HEY! $anyfiles Files were found in directory $target_dir"
    echo " "
    echo -n "Do you REALLY WANT THESE DELETED!! (y or n)? "
    read response
    ans=`echo $response | tr '[A-Z]' '[a-z]'`
    echo
    if [ $ans = 'y' ]
    then
       echo
       echo "FILE DELETION NOW OCCURRING..."
       rm -Rf ?????????*
       if [ $? != 0 ]
       then
           echo "ERROR: File deletion failed in $target_dir"
           exit 1
       fi
       echo
    else
       echo
       echo "OK. Deletions will be skipped in $target_dir."
       echo
    fi
  fi
 fi
done
#
# now delete osfs in each path that has some
count=`osf_test -p $otfr_pathname | wc -l | awk '{print $1}'`
if [ $count != '0' ]
then
echo " "
echo "HEY! $count OSFs in path $otfr_pathname"
echo " "
echo -n "Do you REALLY WANT THESE DELETED!! (y or n)? "
read response
ans=`echo $response | tr '[A-Z]' '[a-z]'`
echo
if [ $ans = 'y' ]
then
       echo
       echo "OSF DELETION NOW OCCURRING..."
       for i in `osf_test -p $otfr_pathname -pr dataset`
       do
          # first delete any duplicate OSF
          `osf_delete -p $otfr_pathname -f $i -t dup 2> /dev/null`
          `osf_delete -p $otfr_pathname -f $i`
          if [ $? != 0 ]
          then
              echo "ERROR: OSF deletion failed for $otfr_pathname, $i"
          fi
       done
else
       echo
       echo "OK. OSF deletions will be skipped in $otfr_pathname."
       echo
fi
fi
count=`osf_test -p $otfsci_pathname | wc -l | awk '{print $1}'`
if [ $count != '0' ]
then
echo " "
echo "HEY! $count OSFs in path $otfsci_pathname"
echo " "
echo -n "Do you REALLY WANT THESE DELETED!! (y or n)? "
read response
ans=`echo $response | tr '[A-Z]' '[a-z]'`
echo
if [ $ans = 'y' ]
then
       echo
       echo "OSF DELETION NOW OCCURRING..."
       for i in `osf_test -p $otfsci_pathname -pr dataset`
       do
          `osf_delete -p $otfsci_pathname -f $i -t dup 2> /dev/null`
          `osf_delete -p $otfsci_pathname -f $i`
          if [ $? != 0 ]
          then
              echo "ERROR: OSF deletion failed for $otfsci_pathname, $i"
          fi
       done
else
       echo
       echo "OK. OSF deletions will be skipped in $otfsci_pathname."
       echo
fi
fi
count=`osf_test -p $reserv_pathname | wc -l | awk '{print $1}'`
if [ $count != '0' ]
then
echo " "
echo "HEY! $count OSFs in path $reserv_pathname"
echo " "
echo -n "Do you REALLY WANT THESE DELETED!! (y or n)? "
read response
ans=`echo $response | tr '[A-Z]' '[a-z]'`
echo
if [ $ans = 'y' ]
then
       echo
       echo "OSF DELETION NOW OCCURRING..."
       for i in `osf_test -p $reserv_pathname -pr dataset`
       do
          `osf_delete -p $reserv_pathname -f $i -t dup 2> /dev/null`
          `osf_delete -p $reserv_pathname -f $i`
          if [ $? != 0 ]
          then
              echo "ERROR: OSF deletion failed for $reserv_pathname, $i"
          fi
       done
else
       echo
       echo "OK. OSF deletions will be skipped in $reserv_pathname."
       echo
fi
fi
# done
exit 0
