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


#                                                                      #
#  EBM macro utility: check_bypass_step                                #
#                                                                      #
######################################################################## 


calling_macro=`basename $0`;export calling_macro

BYPASS=NO; export BYPASS
EXIT_NOW=NO; export EXIT_NOW
rm -f  bypassfile.${EBMSYS}.${JON}

#------------------------------------------------------------------------------#
# Check to see if we are in the middle of an MVS IF/THEN sequence, and
# if we have a file 'ifbypassnow.${PROC_LEVEL}.${EBMSYS}.${JON}' then 
# re-establish the file 'bypassfile.${EBMSYS}.${JON}', set BYPASS=YES, and exit.
# This is because we are here on behalf of a macro invocation, but are
# in a section of the IF/THEN/ELSE/ENDIF code that should NOT be executed
#------------------------------------------------------------------------------#

# ifbypassnow file is used only in MVS environment, so don't need
# separate mode check

if [ -f ifbypassnow.*.${EBMSYS}.${JON} ]
then
   touch bypassfile.${EBMSYS}.${JON}
   BYPASS="YES" ; export BYPASS
   EXIT_NOW=YES ; export EXIT_NOW
fi
#------------------------------------------------------------------------------#

if [ "$EXIT_NOW" != "YES" ]
then

#------------------------------------------------------------------------------#
# If caller is ONCONDCODE or ONRETCODE, 
# do not perfrom onretcode or oncondcode checks, only step restart
#------------------------------------------------------------------------------#

#-----------------------------------------------------------------------#
# CONDITION CODE TESTS
#                Action to take if previous step has abended
#-----------------------------------------------------------------------#
   if [ $calling_macro != ONCONDCODE -a $calling_macro != ONRETCODE ]
   then
   
      #-----------------------------------------------------------------------#
      # There can be only a cond code directive in effect.
      # A step defined directive overrides a job defined
      # for the duration of the step.
      #-----------------------------------------------------------------------#
      if [ "a$STEPNAME" != a ]
      then
         if [ -f ${STEPNAME}.oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} ] 
         then
            . ${STEPNAME}.oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
         else
            if [ -f oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} ]
            then
               . oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
            fi
         fi
      else 
         if [ -f oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} ]
         then
            . oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
         fi
      fi
   
      if [ "a$ONCONDCODE_BYPASS_CURRENT_STEP" != "a" ]
      then
         if [ "$ONCONDCODE_BYPASS_CURRENT_STEP" = "YES" ]
         then
            BYPASS=YES; export BYPASS
            EXIT_NOW="YES"; export EXIT_NOW
   	 touch bypassfile.${EBMSYS}.${JON}
         else 
            BYPASS=NO; export BYPASS
         fi
      fi   
   
      #-----------------------------------------------------------------------#
      # RETURN CODE TESTS
      #             Action to take based on previous step's return code
      #             or the maximum return code of all previous steps.
      #-----------------------------------------------------------------------#
   
      if [ "$EXIT_NOW" = "NO" ]
      then
      
      #-----------------------------------------------------------------------#
      # Job level return code checks override step level return code checks.
      # There can be both job and step defined return code directives.
      # The job directives are checked first.
      # If the job test results in a bypass state (BYPASS=YES)
      # then
      #    the step directives are ignored
      # else
      #    the step directives are tested.
      #-----------------------------------------------------------------------#
      # if the $STATE_THEN=ACTIVE, the current macro that invoked
      # check_bypass_step is the target action of a THEN statement.
      # The execution of this macro is forced, return code checks
      # are not applied.
      #-----------------------------------------------------------------------#
   
         if [ "$STATE_THEN" != "ACTIVE" ]
         then
            if [ -f onretcode.${EBMSYS}.${JON}.${PROC_LEVEL} ]
            then
               . onretcode.${EBMSYS}.${JON}.${PROC_LEVEL}
            fi
            
            if [ "a$ONRETCODE_BYPASS_CURRENT_STEP" = "aYES" ]
            then
               BYPASS=YES; export BYPASS
   	    touch bypassfile.${EBMSYS}.${JON}
            else
               BYPASS=NO; export BYPASS
            fi
   
            #-------------------------------------------------------------------#
            # if the job level return code test(s) are not satisfied,
            # only then execute the step level test(s)
            #-------------------------------------------------------------------#
            if [ $BYPASS = NO ]
            then      
               if [ "a$STEPNAME" != a ]
               then
                  if [ -f ${STEPNAME}.onretcode.${EBMSYS}.${JON}.${PROC_LEVEL} ]
                  then
                     . ${STEPNAME}.onretcode.${EBMSYS}.${JON}.${PROC_LEVEL}
                  fi
               fi
            fi
         fi  # if [ "$STATE_THEN" != "ACTIVE" ]
      
         if [ "a$ONRETCODE_BYPASS_CURRENT_STEP" = "aYES" ]
         then
            BYPASS=YES; export BYPASS
   	 touch bypassfile.${EBMSYS}.${JON}
         else
            BYPASS=NO; export BYPASS
         fi
   
      fi # if [ "$EXIT_NOW" = "NO" ]
   
   fi  #  if [ $calling_macro != ONCONDCODE -a $calling_macro != ONRETCODE ]
fi  #  if [ "$EXIT_NOW" != "YES" ]

#-----------------------------------------------------------------------------#
# RESTART TESTS
#          Check if in restart mode, (or a GOTO has been executed)
#-----------------------------------------------------------------------------#
   
if [ $BYPASS = NO -a $EXIT_NOW = NO ]
then
   RESTART=`cat restart.${EBMSYS}.${JON}` ; export RESTART
   PRESTART=`cat prestart.${EBMSYS}.${JON}` ; export PRESTART
   
#
# the checks for END_JOB have to do with MVS ONLY and the case
# of a default ONCONDCODE situation, but a COND=EVEN|ONLY, in
# which case we need to let the step execute.  Really need to
# separate out the condition code, return code, restart and goto
# checks to discrete routines.
#
   echo $RESTART | grep "END_JOB" > $DEVNULL 

   if [ $? -eq 0 ] 
   then
      BYPASS=NO
      EXIT_NOW="YES"; export EXIT_NOW
   fi

   if [ "$EXIT_NOW" = "NO" ]
   then
      echo $PRESTART | grep "END_JOB" > $DEVNULL

      if [ $? -eq 0 ] 
      then
         BYPASS=NO
         EXIT_NOW="YES"; export EXIT_NOW
      fi

      if [ "$EXIT_NOW" = "NO" ]
      then
         case $PROC_LEVEL in
            0)
               if [ "$RESTART" = "BeginJob" -a "$PRESTART" = "BeginProc" ]
               then
                  BYPASS=NO ; export BYPASS
		  rm -f bypassfile.${EBMSYS}.${JON}
		  rm -f restart.vse.${EBMSYS}.${JON}
               else 
                  if  [ "$RESTART" = "BeginJob" ]
                  then
                     BYPASS=NO ; export BYPASS
                     rm -f bypassfile.${EBMSYS}.${JON}
                     rm -f restart.vse.${EBMSYS}.${JON}
                  else
                     if [ "$RESTART" = "$STEPNAME" ]
                     then
                        cat <$DEVNULL > restart.${EBMSYS}.${JON}
                        echo "BeginJob" >> restart.${EBMSYS}.${JON}
                        RESTART=`cat restart.${EBMSYS}.${JON}` ; export RESTART
                        BYPASS=NO ; export BYPASS
                        rm -f bypassfile.${EBMSYS}.${JON}
                        rm -f restart.vse.${EBMSYS}.${JON}
                        if [ "$calling_macro" = "EXECPGM" ]
                        then
                           cat <$DEVNULL > prestart.${EBMSYS}.${JON}
                           echo "BeginProc" >> prestart.${EBMSYS}.${JON}
                        fi
                     else
                        BYPASS=YES; export BYPASS
                        touch bypassfile.${EBMSYS}.${JON}
                     fi
                  fi
               fi
            ;;
            *)
               if [ "$RESTART" = "BeginJob" -a "$PRESTART" = "BeginProc" ]
               then
                  BYPASS=NO ; export BYPASS
		  rm -f bypassfile.${EBMSYS}.${JON}
                  rm -f restart.vse.${EBMSYS}.${JON}
               else
                  if [ "$RESTART" = "BeginJob" -a "$PRESTART" = "$STEPNAME" ]
                  then
                     BYPASS=NO ; export BYPASS
		     rm -f bypassfile.${EBMSYS}.${JON}
                     rm -f restart.vse.${EBMSYS}.${JON}
                     cat <$DEVNULL > prestart.${EBMSYS}.${JON}
                     echo "BeginProc" >> prestart.${EBMSYS}.${JON}
                     cat <$DEVNULL > restart.${EBMSYS}.${JON}
                     echo "BeginJob" >> restart.${EBMSYS}.${JON}
                  else
                     BYPASS=YES; export BYPASS
		     touch bypassfile.${EBMSYS}.${JON}
                  fi
               fi
            ;;
         esac

      fi # if [ "$EXIT_NOW" = "NO" ]

   fi # if [ "$EXIT_NOW" = "NO" ]

fi # if [ #BYPASS = NO -a $EXIT_NOW = NO ]
