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


########################################################################
#                                                                      #
# JOB MACRO           :     ONCONDCODE                                 #
# Purpose             :     Specifies the action to be taken based on  #
#                           the condition code issued within a step.   #
#                                                                      #
########################################################################

check_bypass_func()
{

BYPASS=NO; export BYPASS

#-----------------------------------------------------------------------#
# There can be only one 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
      touch bypassfile.${EBMSYS}.${JON}
   else
      BYPASS=NO; export BYPASS
      rm -f bypassfile.${EBMSYS}.${JON}
   fi
fi
return
}

########################################################################

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

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

if [ "a$STEPNAME" != "a" ]
then
   print_message_STEPNAME=$STEPNAME; export print_message_STEPNAME
fi


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

if [ -f setparm.${EBMSYS}.${JON}.${PROC_LEVEL} ]
then
   . setparm.${EBMSYS}.${JON}.${PROC_LEVEL}
fi
#
# if ifbypassnow.level.ebmsys.jon exists we must have been
# called from witin an IF/THEN/ELSE sequence, and due to 
# the IF check, we should NOT execute this particular instance
# of ONCONDCODE.
#
if [ -f "currentiflevel.${EBMSYS}.${JON}" ]
then
   ls ifbypassnow.*.${EBMSYS}.${JON} 1>$DEVNULL 2>&1
   stat=$?
   if [  "$stat" = "0" ]
   then
      exit 0
   fi
fi

#-----------------------------------------------------------------------------
# Initialize variables
#-----------------------------------------------------------------------------
comparator1=''; export comparator1
comparator2=''; export comparator2
INTEGER1='';    export INTEGER1 
INTEGER2='';    export INTEGER2 
LABEL='';       export LABEL
OPERATOR='';    export OPERATOR
ACTION='';      export ACTION 


if [ $# -lt 3 ]
then
   print_message ONCONDCODE "ERROR   invalid syntax: macro requires a minimum of 3 parameters" 
   . set_jobstatus_abort 
   ABORT=YES; export ABORT
fi

comparator1=$1 ; export comparator1
INTEGER1=$2 ; export INTEGER1
arg3=$3  ; export arg3

case $comparator1 in 
   EQ|NE|GT|LT|GE|LE|eq|ne|gt|lt|ge|le)
      COMPARATOR1=-`eval echo $comparator1 | tr '[A-Z]' '[a-z]'`  ; export COMPARATOR1
   ;;
   *)
      print_message ONCONDCODE "ERROR   invalid syntax:  $comparator1"
      . set_jobstatus_abort 
      ABORT=YES; export ABORT
   ;;
esac

tmp=`eval echo $INTEGER1 | tr -d '[0-9]'`
if [ "a$tmp" != "a" ]
then
   print_message ONCONDCODE "invalid syntax:  value must be an integer"
   . set_jobstatus_abort 
   ABORT=YES; export ABORT
fi

#------------------------------------------------------------------------------#
#  Valid syntax:
#------------------------------------------------------------------------------#
#1           2        3         4           5        6         7       8
#COMPARATOR1 INTEGER1 BYPASS    [verbose]
#COMPARATOR1 INTEGER1 CONTINUE  [verbose]
#COMPARATOR1 INTEGER1 GOTO      LABEL       [verbose]
#COMPARATOR1 INTEGER1 AND|OR    COMPARATOR2 INTEGER2 GOTO      LABEL   [verbose]
#------------------------------------------------------------------------------#
 
case $arg3 in
   BYPASS|bypass)
      ACTION=BYPASS; export ACTION 
   ;;
   CONTINUE|continue)
      ACTION=CONTINUE; export ACTION 
   ;;
   GOTO|goto)
      arg4=$4  ; export arg4
      if [ "a$arg4" = "a" ]
      then  print_message ONCONDCODE "ERROR   invalid syntax: GOTO requires a label or stepname"
         . set_jobstatus_abort 
         ABORT=YES; export ABORT
      else
         case $arg4 in 
            # Cannot be a reserved keyword
            VERBOSE|verbose|AND|and|OR|or|GOTO) 
               print_message ONCONDCODE "ERROR   invalid syntax: GOTO requires a label or stepname"
               . set_jobstatus_abort 
               ABORT=YES; export ABORT
            ;; 
            *)
               LABEL=$arg4; export LABEL
               ACTION=GOTO; export ACTION
            ;;
         esac
      fi
   ;;

   AND|and|OR|or)
      OPERATOR=$arg3; export OPERATOR
   ;;
   *)
      print_message ONCONDCODE "ERROR   invalid syntax: $arg3"
      . set_jobstatus_abort 
      ABORT=YES; export ABORT
   ;;
esac


for argument in $*
do
   i=`eval echo $argument`
   case $i in
      verbose)
         VERBOSE=YES  ; export VERBOSE 
      ;;
      scope=*|SCOPE=*)
        SCOPE=`eval echo $i|cut -f2 -d'='`; export SCOPE
      ;;
   esac
done

if [ "a$VERBOSE" = "aYES" ]
then
   echo " "
   print_message ONCONDCODE "----------Starting in verbose mode----------"
fi

if [ "a$SCOPE" != "a" ]
then
   SCOPE="`echo $SCOPE | tr '[a-z]' '[A-Z]'`"  ; export SCOPE
else
   SCOPE=JOB; export SCOPE
fi

case $SCOPE in
   JOB)
      COND_STEP_ID=''; export COND_STEP
   ;;

   STEP)
      if [ "a$STEPNAME" != "a" ]
      then
         COND_STEP_ID="$STEPNAME."; export COND_STEP_ID
      else
         print_message ONCONDCODE "ERROR   invalid syntax: scope=STEP, macro is not defined inside a step"
      . set_jobstatus_abort
      ABORT=YES; export ABORT
fi
   ;;

   *)
      print_message ONCONDCODE "Invalid syntax: scope=$SCOPE"
      . set_jobstatus_abort
      ABORT=YES; export ABORT
   ;;
esac

#------------------------------------------------------------------------------
#  If a logical operator was entered, AND/OR, test compound condition 
#------------------------------------------------------------------------------
if [ "a$OPERATOR" != "a" ]
then
   case $OPERATOR in
      AND|and)
         OPERATOR="-a" ; export OPERATOR
      ;;
      OR|or)
         OPERATOR="-o" ; export OPERATOR
      ;;
      *)
         print_message ONCONDCODE "ERROR   invalid syntax" 
         . set_jobstatus_abort 
         ABORT=YES; export ABORT
      ;;
   esac
 
# A valid logical operator was entered

   comparator2=$4; export comparator2
   INTEGER2=$5;    export INTEGER2
   ACTION=$6;      export ACTION

   if [ "a$comparator2" = "a" -o "a$INTEGER2" = "a" -o "a$ACTION" = "a" ]
   then
      print_message ONCONDCODE "ERROR   invalid syntax" 
      . set_jobstatus_abort 
      ABORT=YES; export ABORT
   fi


   case $ACTION in 
      GOTO|goto)
         LABEL=$7;    export LABEL

         if [ "a$LABEL" = "a" ]
         then
            print_message ONCONDCODE "ERROR   invalid syntax: GOTO requires a label or stepname"
         . set_jobstatus_abort 
         ABORT=YES; export ABORT
         fi
      ;;
      *)
         print_message ONCONDCODE "ERROR   invalid syntax"
      ;;
   esac

 
   case $comparator2 in 
      EQ|NE|GT|LT|GE|LE|eq|ne|gt|lt|ge|le)
         COMPARATOR2=-`eval echo $comparator2 | tr '[A-Z]' '[a-z]'`  ; export COMPARATOR1
      ;;
      *)
         print_message ONCONDCODE "ERROR   invalid syntax, value must be an integer"
         . set_jobstatus_abort 
         ABORT=YES; export ABORT
      ;;
   esac
fi   

if [ "a$ABORT" = "aYES" ]
then
   exit 0
fi

if [ "a$VALIDATE" != "ay" -o "a$VERBOSE" = "aYES" ]
then
   print_message ONCONDCODE "$comparator1 $INTEGER1 $OPERATOR $comparator2 $INTEGER2 $ACTION $LABEL scope=$SCOPE"
fi
#------------------------------------------------------------------------------#
# Build the file
#------------------------------------------------------------------------------#

if [ "a$VALIDATE" != "ay" ]
then
   cat <$DEVNULL > ${COND_STEP_ID}oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
   chmod 775 ${COND_STEP_ID}oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
   echo "CONDCODE=\`get_condcode\`  ; export CONDCODE" >> ${COND_STEP_ID}oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
   
   if [ "a$OPERATOR" = "a" ]
   then
   
      echo "if [ \"\$CONDCODE\" $COMPARATOR1 $INTEGER1 ]" >> ${COND_STEP_ID}oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} 
   
   else
   
      echo "if [ \"\$CONDCODE\" $COMPARATOR1 $INTEGER1 $OPERATOR \"\$CONDCODE\" $COMPARATOR2 $INTEGER2  ]" >> ${COND_STEP_ID}oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
   
   fi
   #------------------------------------------------------------------------------#
   # Define the action to be performed based on BATCH_MODE
   #------------------------------------------------------------------------------#
   if [ "$BATCH_MODE" = MVS ]
   then
      . oncondcode.mvs
      #
      # use the check_bypass_func function to source the oncondcode
      # file just created and see if it will take immediate effect
      # this is done to create the 'bypassfile' for performance
      #
      check_bypass_func
   else
      . oncondcode.vse
   fi 
fi   


exit 0
