#!/bin/ksh
#**********************************************************************#
#*                                                                    *#
#* Copyright (c) 2001 by Sun Microsystems, Inc.                       *#
#* All rights reserved.                                               *#
#*                                                                    *#
#**********************************************************************#


########################################################################
#                                                                      #
# EBM BATCH JOB MACRO :     ENDJOB                                     #
# Purpose             :     Define the end of a jobstream              # 
#                                                                      #
########################################################################

. verify_jobstatus
. beginjob.${EBMSYS}.${JON}
. current_proc_level.${EBMSYS}.${JON}

if [ -f setparm.${EBMSYS}.${JON}.${PROC_LEVEL} ]
then
   . setparm.${EBMSYS}.${JON}.${PROC_LEVEL}
fi


for argument in $*
do
  i=`eval echo $argument`
  case $i in
    verbose*)
      VERBOSE=YES
      export VERBOSE
      ;;
    *)
      print_message ENDJOB "Invalid parameter specified :   $i" 
      . set_jobstatus_abort
      break
      ;;
  esac
done

if [ "$BATCH_MODE" = "MVS" ]
then
#
# verify that if this was a restart job, we
# actually found the step(s) of restart
# if END_JOB is in either file, we have an
# internal macro abort and must bypass the
# restart message check.
#
restart=`cat restart.${EBMSYS}.${JON}`
prestart=`cat prestart.${EBMSYS}.${JON}`
grep "END_JOB" restart.${EBMSYS}.${JON} > $DEVNULL
rstat1=$?
grep "END_JOB" prestart.${EBMSYS}.${JON} > $DEVNULL
prstat1=$?

if [ "$rstat1" != 0 -a "$prstat1" != 0 ]
then
   if [ "$restart" != "BeginJob" -o "$prestart" != "BeginProc" ]
   then
      echo "----------------------------------------------"
      echo "--  RESTART OR GOTO LABEL NOT FOUND"
      echo "--     Job stepname  = $RESTART"
      if [ "$prestart" != "BeginProc" ]
      then
         echo "--    Proc stepname = $PRESTART"
      fi
      echo "----------------------------------------------"
      . set_jobstatus_abort
   fi
fi
fi
if [ "$VERBOSE" = "YES" ]
then
   echo " "
   print_message ENDJOB "-----Starting in verbose mode-----"
fi
. verify_jobstatus
. jobstatus.${EBMSYS}.${JON}

if [ -f onretcode_status.${EBMSYS}.${JON} ]
then 
   onretcode_status=`cat onretcode_status.${EBMSYS}.${JON}` ; export onretcode_status
else
   onretcode_status='' ; export onretcode_status
fi

if [ -f oncondcode_status.${EBMSYS}.${JON} ]
then 
   oncondcode_status=`cat oncondcode_status.${EBMSYS}.${JON}` ; export oncondcode_status
else
   oncondcode_status='' ; export oncondcode_status
fi

if [ "$onretcode_status" != "GOTO_END_JOB" -o "$oncondcode_status" != "GOTO_END_JOB" ] 
then
   if [ -d ${SYSOUTDIR}/${JOBNAME} ]
   then 
      OW ${SYSOUTDIR}/${JOBNAME} ]
   fi
   if [ "a$FORMS" != "a" ]
   then
      if [ -d ${FORMS}/${JOBNAME} ]
      then 
         OW ${FORMS}/${JOBNAME} 
      fi
   fi
   . ${JON}_WK4n 
else 
   . ${JON}_WK4a 
fi

#------------------------------------------------------------------------------
# BEGINJOB generates files to force cleanup of workfiles
# If ENDJOB is executed, the following lines ensure the cleanup
# of  any residual workfiles if the WK4 files did not execute properly.
#------------------------------------------------------------------------------

# Macro workfiles
rm -f *.${EBMSYS}.${JON}
rm -f *.${EBMSYS}.${JON}.*

# SYSIN/Instream, 6398
rm -f LN_SYSIN.${JON}
rm -f LN_*.${JON}.*

rm -f SYSIN.${JON}
rm -f SYSIN.${JON}.*

# Batch shell workfiles
rm -f /tmp/btsh_env${JON}*
rm -f status.${JON}
rm -f ${JON}_WK2a
rm -f ${JON}_WK2n
rm -f ${JON}_WK3a
rm -f ${JON}_WK3n 
rm -f ${JON}_WK4a
rm -f ${JON}_WK4n
rm -f ${JON}_RET*
rm -f ${JON}_PROC_RETCOD_*
rm -f ${JON}_PROC_RETCOD_*.back
rm -f ${JON}_JCOND

# bypassfile and restart.vse for performance
rm -f bypassfile.${EBMSYS}.${JON}*
rm -f restart.vse.${EBMSYS}.${JON}*

if [ "$BATCH_MODE" = "VSE" ]
then
   rm -f unset.*.${JON}.${EBMSYS}
fi

echo " "
print_message ENDJOB "-------------------------------------------------------"
print_message ENDJOB "$JOBNAME start at TIME=$START_TIME"
STOP_TIME="`ebmtime`";export STOP_TIME
print_message ENDJOB "$JOBNAME stop  at TIME=$STOP_TIME"
print_message ENDJOB "-------------------------------------------------------"
exit $JOBSTATUS
