#!/bin/csh
#
#--------------------------------------------------------------------
#
#       xpltst.csh
#
#       C shell script spawned by the XPOLL test driver, xpltst.
#
#--------------------------------------------------------------------
#                          REVISION HISTORY
#--------------------------------------------------------------------
#  MOD             PR
# LEVEL   DATE   NUMBER  User     Description
# ----- -------- ------  -------  ------------------------------------
#  001  03/16/99 38414   WMiller  Initial code
#  002  08/10/05 53803   MSwam  Change PATH_FILE_NAME to PATH_BASENAME
#
#--------------------------------------------------------------------
#
# Bail out if appears to not have been run by XPOLL
if ( ! $?EVENT_TYPE ) then
   echo ""
   echo "xpltst - EVENT_TYPE not defined in environment!"
   echo ""
   echo "This script is spawned by the xpoll test driver, xpltst,"
   echo "and is not meant to be run interactively."
   echo ""
   exit 100 # in XPOLL's fatal range, just in case
endif
# Check event type
if ( $EVENT_TYPE == "EVENT_TIME_EVENT" ) then
   # Check if test is complete
   set OHOMEDIR = `osfile_stretch_file OPUS_HOME_DIR:`
   if ( $status != 0 ) then
      echo ""
      echo "Failed to resolve OPUS_HOME_DIR:; bailing out."
      exit 100
   endif
   if ( -f $OHOMEDIR/xpltst_done ) then
      echo ""
      # verify OSF
      set osf = `osf_test -p $PATH_BASENAME -f xpltst -c IN -s c`
      if ( $status != 0 || $osf == "" ) then
         echo "OSF not found or incorrectly updated. TEST FAILED."
         exit 100
      endif
      # verify file names
      if ( ! -f $OUTPATH/xpltst_1.tst_proc ) then
         echo "$OUTPATH/xpltst_1.tst_proc not found. TEST FAILED."
         exit 100
      endif
      if ( ! -f $OUTPATH/xpltst_2.tst_done ) then
         echo "$OUTPATH/xpltst_2.tst_done not found. TEST FAILED."
         exit 100
      endif
      if ( ! -f $OUTPATH/xpltst_3.tst_done ) then
         echo "$OUTPATH/xpltst_3.tst_done not found. TEST FAILED."
         exit 100
      endif
      if ( ! -f $OUTPATH/xpltst_4.tst_done ) then
         echo "$OUTPATH/xpltst_4.tst_done not found. TEST FAILED."
         exit 100
      endif
      if ( ! -f $OUTPATH/xpltst_5.tst_bad ) then
         echo "$OUTPATH/xpltst_5.tst_bad not found. TEST FAILED."
         exit 100
      endif
      echo "xpltst - COMPLETE, TEST PASSED"
      # clean-up files
      /bin/rm -f $OHOMEDIR/xpltst_done
      /bin/rm -f $OUTPATH/xpltst*
      exit 100
   endif
   echo ""
   echo "EVENT TYPE: $EVENT_TYPE"
   echo ""
   # create OSF for testing OSF triggering
   echo "Creating OSF ..."
   osf_create -p $PATH_BASENAME -f xpltst -c IN -s i
   if ( $status != 0 ) then
      echo "... an ERROR occurred; bailing out."
      exit 100
   endif
   exit 0 # exit status not important for TIME events
else if ( $EVENT_TYPE == "EVENT_OSF_EVENT" ) then
   # create files for testing FILE triggering
   echo "Creating files $OUTPATH/xpltst_[1-5].txt"
   touch $OUTPATH/xpltst_1.tst
   touch $OUTPATH/xpltst_2.tst
   touch $OUTPATH/xpltst_3.tst
   touch $OUTPATH/xpltst_4.tst
   touch $OUTPATH/xpltst_5.tst
   if ( $status != 0 ) then
      echo ""
      echo "... an ERROR occurred; bailing out."
      exit 100
   endif
   echo ""
   echo "EVENT TYPE  : $EVENT_TYPE"
   echo "EVENT NUM   : $EVENT_NUM"
   if ( $EVENT_NUM != 1 ) then
      echo "...EVENT NUM is incorrect (should be 1); bailing out."
      exit 100
   endif
   echo "EVENT items :"
   echo "Dataset $OSF_DATASET"
   echo "DCF Num $OSF_DCF_NUM"
   echo "Time    $OSF_START_TIME"
   echo "ID      $OSF_DATA_ID"
   # return OSF_NORMAL
   exit 1
else if ( $EVENT_TYPE == "EVENT_FILE_EVENT" ) then
   set OHOMEDIR = `osfile_stretch_file OPUS_HOME_DIR:`
   if ( $status != 0 ) then
      echo ""
      echo "Failed to resolve OPUS_HOME_DIR:; bailing out."
      exit 100
   endif
   echo ""
   echo "EVENT TYPE  : $EVENT_TYPE"
   echo "EVENT NUM   : $EVENT_NUM"
   if ( $EVENT_NUM != 5 ) then
      echo "...EVENT NUM is incorrect (should be 5); bailing out."
      exit 100
   endif
   echo "EVENT items :"
   echo "Item #1 $EVENT_NAME"
   echo "Item #2 $EVENT_NAME1"
   echo "Item #3 $EVENT_NAME2"
   echo "Item #4 $EVENT_NAME3"
   echo "Item #5 $EVENT_NAME4"
   echo "Creating state file ..."
   # Indicate "NULL" state for first file
   echo "EVENT_STATUS  = 5" > $OHOMEDIR/$EVENT_STATE_FILE
   # Indicate FILE_SUCCESS state for second through fourth files
   echo "EVENT_STATUS1 = 3" >> $OHOMEDIR/$EVENT_STATE_FILE
   echo "EVENT_STATUS2 = 3" >> $OHOMEDIR/$EVENT_STATE_FILE
   echo "EVENT_STATUS3 = 3" >> $OHOMEDIR/$EVENT_STATE_FILE
   # Indicate FILE_ERROR state for fifth file
   echo "EVENT_STATUS4 = 4" >> $OHOMEDIR/$EVENT_STATE_FILE
   # Indicate that test is complete
   touch $OHOMEDIR/xpltst_done
   if ( $status != 0 ) then
      echo ""
      echo "Failed to create test done indicator $OHOMEDIR/xpltst_done; bailing out."
      exit 100
   endif
   # return "CHECK_FILE" status
   exit 6
endif
