#!/bin/csh -x
##########################################################################
#
# routine: retrigger.csh
#
# purpose: Script to perform retriggering of OSFs and files.
#
#       This script is activated by a time trigger.  It queries the
#	OSF blackboard for OSFs in need of retriggering as determined
#	by specific environment variables.  Any OSFs found in need
#	are retriggered as specified.
#
#	Currently an OSF can only be queried and retriggered in a single
#       pipeline stage.  For example, if these entries appeared in the
#       resource file for a retrigger process,
#          SEARCH_OSF.DE = z
#          SET_OSF.DE    = w
#
#       then the retrigger process would search for OSFs on the blackboard
#       with the DE column = "z" and alter them to "w".  Currently the
#       stage name appearing in SEARCH_OSF (.DE) must have a corresponding
#       SET_OSF.DE entry.
#
#	Similar retriggering is performed for files.  A file can be
#	retriggered through these resource file entries:
#          SEARCH_DIRECTORY.1 = /home/mswam/  ! directory in which to search
#	   SEARCH_EXT.1       = .pod_dup      ! file extension to search for
#          SET_EXT.1          = .pod          ! file extension to set to
#
#       Multiple sets of these three (3) file search variables can be supplied, 
#       with each set numbered from 1, 2, 3, etc.  All three variables must
#       be supplied for each set.
#
# required:
#	The following environment variables must be available to this
#	routine:
#		PATH_FILE - full path name to the OPUS path file
#
# returns:
#       102 if the path file or stage file cannot be found
#       0   if successful OR no OSFs found to update
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 12/07/99 37784  MSwam   first version
# 01/31/01 43165  MSwam   mods for OTFR
# 
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
/bin/date
#
set go_absent=102          #this exit status is in the FATAL range for XPOLL
set all_is_well=0
#
# locate the pipeline stage file for this path
#
set stagefile=`grep STAGE_FILE $PATH_FILE | awk '{print $3}'`
if ($stagefile == "") then
   echo "ERROR: Failed to find pipeline stage file for $PATH_FILE"
   exit $go_absent
endif
set fullstage=`osfile_stretch_file $stagefile`
if ($fullstage == "") then
   echo "ERROR: Failed to resolve pipeline stage file $stagefile"
   exit $go_absent"
endif
#
# loop through the set of pipeline stages in the stage file
#
foreach i (`egrep "TITLE =" $fullstage | awk '{print $3}'`)
   # search for retriggering information in the environment
   set stagevalue = `printenv SEARCH_OSF.$i`
   if ($stagevalue != "") then
      set newvalue = `printenv SET_OSF.$i`
      if ($newvalue != "") then
        #
        # a retrigger has been requested 
        #
        # search OSFs for any in need of retriggering and save their names
        #
        osf_test -p $PATH_FILE -c $i -s $stagevalue -pr dataset > tmp$$.notify
        if ($status != 0) then
          echo "ERROR testing for retriggering candidates."
          exit $go_absent
        endif
        # loop through qualifying datasets, and retrigger each
        #
        set numentries=`cat tmp$$.notify | wc -l`
        if ($numentries != 0) then
          foreach j (`cat tmp$$.notify`)
            osf_update -p $PATH_FILE -f $j -c $i -s $newvalue
            if ($status != 0) then
               echo "WARNING: failed to retrigger OSF for $j"
               # possible collision, just try the next one
            endif
          end
        endif
      endif
   endif
end
#
# loop through the possible FILE retriggering variables
#   (e.g. SEARCH_FILE.1,SET_FILE.1 , etc.)
#
@ i = 1;
TOPFILE:
   set search_ext = `printenv SEARCH_EXT.$i`
   if ($search_ext != "") then
      set set_ext = `printenv SET_EXT.$i`
      if ($set_ext != "") then
         set search_dir = `printenv SEARCH_DIRECTORY.$i`
         if ($search_dir != "") then
            #
            # rename all matching files
            foreach f (`ls -1 $search_dir/*$search_ext`)
                set newf = $f:r${set_ext}
                /bin/mv -i $f $newf
                if ($status != 0) then
                   # failures will try again later
                   echo "rename FAILED for $f to $newf; try later"
                endif
                if (-e $f) then
                   # failures will try again later
                   echo "rename FAILED for $f to $newf; try later"
                endif
                echo "renamed $f to $newf"
            end
         else
            echo "ERROR: Found SEARCH_FILE.$i,SET_FILE.$i, but no SEARCH_DIRECTORY.$i"
            goto DONE
         endif
      else
         echo "ERROR: Found SEARCH_FILE.$i, but no SET_FILE.$i"
         goto DONE
      endif
   else
      goto DONE
   endif
   @ i++
   goto TOPFILE
DONE:
#
# complete, exit success
rm tmp$$.notify
exit $all_is_well
