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


########################################################################
#                                                                      #
# EBM BATCH JOB MACRO :     EXECPGM                                    #
# Purpose             :     Execute a program                          #
#                                                                      #
########################################################################

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

#
# source the label file so we can get our
# running step name - RUNNINGSN globally available
# this is needed in case the program about to be
# executed is used to do dynamic allocation of
# sysout files (Affina support).
#
if [ -f "label.${EBMSYS}.${JON}.${PROC_LEVEL}" ]
then
   . label.${EBMSYS}.${JON}.${PROC_LEVEL}
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 [ -f setpgmsw.${EBMSYS}.${JON} ]
then
   . setpgmsw.${EBMSYS}.${JON}
fi
# Preserve the current stepname from the LABEL file if it exists
if [ ! -z $STEPNAME ]
then
   jon_proc_stepname=$STEPNAME
fi
MACRO_NAME=EXECPGM; export MACRO_NAME

EXECPGM_STEPNAME=''; export EXECPGM_STEPNAME
FILLER="                               "; export FILLER
for argument in $@
do
  i=`eval echo "$argument"`
  case $i in
    stepname=*|STEPNAME=*)
       STEPNAME="`eval echo $i|cut -f2 -d'='`"
       export STEPNAME
       EXECPGM_STEPNAME=$STEPNAME;export EXECPGM_STEPNAME
       ;;
    pgmname=*|PGMNAME=*)
       PGMNAME="`eval echo $i|cut -f2 -d'='`"
       export PGMNAME
       ;;
    parm=*|PARM=*)
       tmp_parm="`eval echo $i|cut -c6-`"; export tmp_parm
       PARM="$PARM $tmp_parm"; export PARM
       ;;
    verbose|VERBOSE)
      VERBOSE=YES ; export VERBOSE
       ;;
    *)
      print_message EXECPGM "ERROR   Invalid parameter specified :   $i" 
      . set_jobstatus_abort
      ABORT=YES; export ABORT
      ;;
  esac
done

if [ "a$PGMNAME" = "a" ]
then
    print_message EXECPGM "ERROR   Invalid syntax: pgmname is required" 
    . set_jobstatus_abort
    ABORT=YES; export ABORT
else
   if [ "a$EXECPGM_STEPNAME" = "a" ]
   then
      STEPNAME=$PGMNAME; export STEPNAME
   fi 
fi
# Preserve the current stepname from the LABEL file if it exists
if [ ! -z $jon_proc_stepname ]
then
   jon_proc_stepname=$STEPNAME
fi

# in MVS mode, a user may code an EXECPGM macro without
# a prior LABEL statment.  In this case, use the stepname
# from the EXECPGM statement as our current job stepname.
#
# The assgndd workfile may contain sysout file references
# to RUNNINGSN.  But, if no labels are in the JCL, we will
# not have a RUNNINGSN, but we will have a default STEPNAME
# from the EXECPGM card.  So, if RUNNINGSN is null, use the
# STEPNAME so will create the proper sysout file paths if
# that code is in the assgndd workfile
#
if [ -z "$RUNNINGSN" ]
then
   if [ $PROC_LEVEL -eq 1 ]
   then
      if [ ! -z $CALLERS_STEPNAME ]
      then
         RUNNINGSN=${CALLERS_STEPNAME}_${STEPNAME} ; export RUNNINGSN
      else
         RUNNINGSN=$STEPNAME; export RUNNINGSN
      fi
   else
      RUNNINGSN=$STEPNAME; export RUNNINGSN
   fi
fi

if [ "a$PROCNAME" != "a" ]
then
   CURRENT_STEP_PROCNAME=$PROCNAME; export CURRENT_STEP_PROCNAME
fi

print_message_STEPNAME=$STEPNAME; export print_message_STEPNAME

. check_bypass_step

case $BATCH_MODE in
   VSE)
      if [ -f assgndd.${EBMSYS}.${JON} ]
      then
         . assgndd.${EBMSYS}.${JON}
      fi
   ;;

   MVS)
      prior_proc_level=`expr $PROC_LEVEL - 1`

      #-----------------------------------------------------------------------
      # 1.  Source the ASSGNDD generated in the current execution level
      #
      #     The current level of execution, whether at job level(0)  or 
      #     procedure level(1-n), will  have  an ASSGNDD workfile  if one 
      #     or more ASSGNDD  macros were present. It is in the format:
      #
      #      assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}
      #
      # 2. Source the calling level's ASSGNDD override workfile 
      #
      #    If we are at a procedure level,  (PROC_LEVEL > 0),
      #    then there MAY be a file in the format:
      #
      #    ${CURRENT_STEP_PROCNAME}.assgndd.${EBMSYS}.${JON}.{prior_proc_level}
      #
      #    containing DD definitions to OVERRIDE the current ones.
      #    source this file NEXT.
      #-----------------------------------------------------------------------
   
      if [ -f "assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}" ]
      then
         . assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}
      fi

      if [ -f "${EXECPGM_STEPNAME}.assgndd.${EBMSYS}.${JON}.${prior_proc_level}" ]
      then
         . ${EXECPGM_STEPNAME}.assgndd.${EBMSYS}.${JON}.${prior_proc_level}
      fi
   
      #
      if [ -f first_step.assgndd.${EBMSYS}.${JON}.${prior_proc_level} ]
      then
         . first_step.assgndd.${EBMSYS}.${JON}.${prior_proc_level}
      fi
   ;;
esac
if [ "a$ABORT" != "aYES" ]
then
   cat < $DEVNULL >  ${JON}_RET
   echo "_${RUNNINGSN}_ 0" >> ${JON}_RET
fi

if [ "$BYPASS" = "NO" -a "a$ABORT" != "aYES" ]
then
  if [ "a$EXECPGM_EXPAND_PARM" != "aN" ]
  then
   if [ "a$PARM" != a ]
   then
      PARM=`eval echo $PARM`; export PARM
   fi
  else
   if [ "a$PARM" != a ]
   then
      PARM=`ebmsed EBM_DELETE_BACK_SLASH $PARM`; export PARM
   fi
  fi

   if [ -f first_step.${EBMSYS}.${JON} ]
   then
      rm -f first_step.${EBMSYS}.${JON}
   fi

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

   STEP_START_TIME="`ebmtime`" ;  export STEP_START_TIME
   SAVE_COBPATH="$COBPATH"; export SAVE_COBPATH
   SAVE_PATH="$PATH"; export SAVE_PATH
   COBPATH_ORIG="$COBPATH"; export COBPATH_ORIG
   if [ -f libdef_pgm_step.${EBMSYS}.${JON} ]
   then
      .  libdef_pgm_step.${EBMSYS}.${JON}
      if [ "a$STEPLIB" != "a" ]
      then
         case $EBM_OS_NAME in
            NT)
               COBPATH="$STEPLIB;$COBPATH"; export COBPATH
               PATH="$STEPLIB;$PATH"; export PATH
            ;;
         
            *)
               COBPATH="$STEPLIB:$COBPATH"; export COBPATH
               PATH="$STEPLIB:$PATH"; export PATH
            ;;
         esac
      fi   
   fi
    
   if [ "a$STEPLIB" = "a" ]
   then
      if [ -f libdef_pgm_job.${EBMSYS}.${JON} ]
      then
        .  libdef_pgm_job.${EBMSYS}.${JON}
        if [ "a$JOBLIB" != "a" ]
        then
           case $EBM_OS_NAME in
              NT)
                 COBPATH="$JOBLIB;$COBPATH"; export COBPATH
                 PATH="$JOBLIB;$PATH"; export PATH
              ;;
              *)
                 COBPATH="$JOBLIB:$COBPATH"; export COBPATH
                 PATH="$JOBLIB:$PATH"; export PATH
              ;;
           esac
        fi
      fi
   fi
   if [ "a$PROCNAME" != a ]
   then
      STEPNAME=${PROCNAME}_${STEPNAME}
   fi
   #-----------------------------------------------------------------------
   # Determine if job override causes all but PARM on first step
   # to be nullified.
   #-----------------------------------------------------------------------

   if [ ! -f beginproc_first_step_in_proc.${EBMSYS}.${JON}.${PROC_LEVEL} ]
   then
      if [ -f execproc_nullify_pgmprms.${EBMSYS}.${JON}.${PROC_LEVEL} ]
      then 
         PARM=''; export PARM
      fi
   fi

   if [ "$VERBOSE" = "YES" ]
   then
      echo " "
      print_message EXECPGM "-----Starting in verbose mode-----"
      print_message EXECPGM "stepname=$STEPNAME"
      print_message EXECPGM "pgmname=$PGMNAME"
      if [ "a$PARM" != "a" ]
      then
         print_message EXECPGM "parm=$PARM"
      fi
      if [ "a$COBPATH" != "a" ]
      then
         print_message EXECPGM "Lib Path=$COBPATH"
      else     
         print_message EXECPGM "Lib Path="
      fi
      if [ "a$PATH" != "a" ]
      then
         print_message EXECPGM "PATH=`ebmsed $PATH`"
      else
         print_message EXECPGM "PATH="
      fi
   fi
   #-----------------------------------------------------------------------
   # Determine if PARM passed to program is overridden by caller
   #-----------------------------------------------------------------------
   if [ "$PROC_LEVEL" -gt 0 ]
   then
   #-----------------------------------------------------------------------
   # If there is an explicit override PARM.procstepname, use it
   # else
   #    if first step in proc and first step override exists, use it
   #    else
   #       if not in first step and nullify parm exists, nullify (~)
   #       the ~ is reset to null in the cobwrap process.
   #-----------------------------------------------------------------------
      if [ -f execproc_pgmparm.${EBMSYS}.${JON}.${PROC_LEVEL} ]
      then
         #-----------------------------------
         # EXECPROC is passing parm overrides
         #-----------------------------------
         pgmp_stepname=`echo $STEPNAME | cut -f2 -d'_'`; export pgmp_stepname
         envpgmparm="`eval echo PGMPARM_${pgmp_stepname}`"; export envpgmparm

         grep $envpgmparm execproc_pgmparm.${EBMSYS}.${JON}.${PROC_LEVEL}>$DEVNULL 2>&1
         if [ $? -eq 0 ]
         then
            value=`grep $envpgmparm execproc_pgmparm.${EBMSYS}.${JON}.${PROC_LEVEL} | cut -f2 -d'='`
            PARM=$value; export PARM
         else 
            #---------------------------
            #no explicit reference found
            #---------------------------
            if [ -f beginproc_first_step_in_proc.${EBMSYS}.${JON}.${PROC_LEVEL} ]
            then
               #---------------------------
               # current step is first step
               #---------------------------
               pgmp_stepname=first_step; export pgmp_stepname
               envpgmparm="`eval echo PGMPARM_${pgmp_stepname}`"; export envpgmparm
               grep $envpgmparm execproc_pgmparm.${EBMSYS}.${JON}.${PROC_LEVEL} > $DEVNULL 2>&1
               if [ $? -eq 0 ]
               then
                  value=`grep $envpgmparm execproc_pgmparm.${EBMSYS}.${JON}.${PROC_LEVEL} | cut -f2 -d'='`; export value
                  PARM=$value; export PARM
               fi    
            else
               #-------------------------------
               # current step is not first step
               #-------------------------------
               if [ -f execproc_nullify_pgmprms.${EBMSYS}.${JON}.${PROC_LEVEL} ]
               then 
                      PARM='~'; export PARM
               fi
            fi   
         fi   
      fi
   fi
   # 
   # look for accept file - backward compatibility
   if [ -f accept.${EBMSYS}.${JON} ]
   then
       . accept.${EBMSYS}.${JON}
   fi

   if [ "$BATCH_MODE" = "VSE" ]
   then
      grep "^$PGMNAME" $PUBLIC/dos.conf 1> $DEVNULL 2>&1
   else
      grep "^$PGMNAME" $PUBLIC/mvs.conf 1> $DEVNULL 2>&1
   fi

   if [ $? -eq 0 ]
   then
      UTILITY=$PGMNAME ;  export UTILITY
      echo 255 > status.${JON}
      case $UTILITY in
        IDCAMS)

           if [ "a$LN_SYSIN" = a ]
           then
              LN_SYSIN="null"; export LN_SYSIN
           else 
              DD_SYSIN=$LN_SYSIN; export DD_SYSIN
           fi
        ;;

        SORT|ICEMAN|SYNCSORT)
           if [ "a$LN_SYSIN" = a ]
           then
              LN_SYSIN="null"; export LN_SYSIN
           else 
              DD_SYSIN=$LN_SYSIN; export DD_SYSIN
           fi

           if [ "a$LRECL_SORTOUT" = "a" ]
           then 
              LRECL_SORTOUT=80; export LRECL_SORTOUT
           fi

           if [ "a$LRECL_SORTIN" = "a" ]
           then 
              LRECL_SORTIN=$LRECL_SORTOUT; export LRECL_SORTIN
           fi
        ;;
      esac
   fi

   if [ "a$LN_SYSIN" = "a" ]
   then
      LN_SYSIN="null"; export LN_SYSIN
   fi

   echo " "
   print_message EXECPGM "starting"

   whence pre_exec_pgm 2>&1 > $DEVNULL
   whenceret=$?
   if [ "$whenceret" -eq 0 ]
   then 
      pre_exec_pgm
   fi 
   
#-----------------------------------------------------------------------------
# The application program about to be invoked may execute macro 
# statements internally. These can create independent assgndd workfiles
# that are unknown to EBM.  
#
# Save the current assgndd workfile by moving it to a temp
# file in the form <assgndd workfile>.sav. If an assgndd workfile
# exists on return from the execpgm.btsh, it was not created by execution of 
# an ASSGNDD macro in the job script. 
#
# . Source it to inherit its content (sets environment
#   variables and may perform file initialization)
# . append it to the saved file
# . move the combined workfiles back to the original workfile
#-----------------------------------------------------------------------------
   case $BATCH_MODE in
      VSE)
         if [ -f assgndd.${EBMSYS}.${JON} ]
         then
            mv assgndd.${EBMSYS}.${JON} assgndd.${EBMSYS}.${JON}.sav
         fi
      ;;
   
      MVS)
         if [ -f "assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}" ]
         then
            mv assgndd.${EBMSYS}.${JON}.${PROC_LEVEL} assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}.sav
         fi
      ;;
   esac

   if [ "a$UTILITY" != "a" ]
   then
      PATH=`ebmsed EBM_CONVERT_PATH_VARIABLE "$PATH"`; export PATH
      if [ "a$SUSPEND" != "anull" ]
      then
       tmprunsn=`echo $RUNNINGSN|tr '_' '.'`
         if [ "$SUSPEND" = "$tmprunsn" ]
         then
            $PACK/bin/btsh $PUBLIC/bin/utility.btsh
         fi
      fi
      $UTILITY 
      CONDCODE=`cat status.${JON}` ; export CONDCODE
   else 
      $PACK/bin/btsh $PUBLIC/bin/execpgm.btsh
      CONDCODE=$?
   fi

   case $BATCH_MODE in
      VSE)
         if [ -f assgndd.${EBMSYS}.${JON} ]
         then
            . assgndd.${EBMSYS}.${JON}
            cat assgndd.${EBMSYS}.${JON} >> assgndd.${EBMSYS}.${JON}.sav
         fi
         if [ -f assgndd.${EBMSYS}.${JON}.sav ]
         then
            mv assgndd.${EBMSYS}.${JON}.sav assgndd.${EBMSYS}.${JON}
         fi
      ;;
   
      MVS)
         if [ -f "assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}" ]
         then
            . assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}
            cat assgndd.${EBMSYS}.${JON}.${PROC_LEVEL} >> assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}.sav
         fi
         if [ -f "assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}.sav" ]
         then
            mv assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}.sav assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}
         fi
      ;;
   esac

   if [ $CONDCODE = "0" ]
   then
      . ${JON}_WK2n
   else
      . ${JON}_WK2a
      rm -f jobstatus.${EBMSYS}.${JON}
      cat <$DEVNULL                                 > jobstatus.${EBMSYS}.${JON}
      echo "JOBSTATUS=$CONDCODE ; export JOBSTATUS" >>jobstatus.${EBMSYS}.${JON}
      echo "echo $CONDCODE"                         > ${JON}_JCOND
      . jobstatus.${EBMSYS}.${JON}
      JOB_COND=`${JON}_JCOND` ; export JOB_COND
      CONDCODE=$JOB_COND      ; export CONDCODE
   fi
   PATH="$SAVE_PATH"; export PATH

   if [ -f ${JON}_RET ]
   then
      cat ${JON}_RET >> ${JON}_RETCOD
   fi
#8179 begin
   proc_retcod_status=0
   if [ $PROC_LEVEL -gt 0 ]
   then
      if [ ! -f ${JON}_PROC_RETCOD_${PROC_LEVEL} ]
      then
         #---------------------------------------------------------------------
         # BEGINPROC unconditionally initializes the file 
         # ${JON}_PROC_RETCOD_${PROC_LEVEL}
         # If it does not exist at this point, there may have been work files
         # removed while job is executing, issue warning.  
         #---------------------------------------------------------------------
         print_message EXECPGM "(W)  ${JON}_PROC_RETCOD_${PROC_LEVEL} does not exist" 
         print_message EXECPGM "(W)  creating ${JON}_PROC_RETCOD_${PROC_LEVEL} " 
         cat $DEVNULL >> ${JON}_PROC_RETCOD_${PROC_LEVEL}
         if [ $? -ne 0 ]
         then
            proc_retcod_status=2
         fi
         chmod 755 ${JON}_PROC_RETCOD_${PROC_LEVEL}
         if [ $? -ne 0 ]
         then
            proc_retcod_status=2
         fi
      fi
      #-----------------------------------------------------------------------
      # extract the return code
      #-----------------------------------------------------------------------
      tmp_retcode=`cat ${JON}_RET | cut -f2 -d' '` 
      #-----------------------------------------------------------------------
      # The entry for ${JON}_PROC_RETCOD_${PROC_LEVEL} is _stepname_ n
      # not RUNNINGSN _jstp_pstp_ n 
      #-----------------------------------------------------------------------
      echo "_${jon_proc_stepname}_ $tmp_retcode" >> ${JON}_PROC_RETCOD_${PROC_LEVEL}
      if [ $? -ne 0 ]
      then
         proc_retcod_status=1
      fi
   fi
   if [ $proc_retcod_status -ne 0 ]
   then
      if [ $proc_retcod_status -eq 1 ]
      then
         print_message EXECPGM "(S) unable to write to  ./${JON}_PROC_RETCOD_${PROC_LEVEL}"
	 else
         print_message EXECPGM "(S) unable to create ./${JON}_PROC_RETCOD_${PROC_LEVEL}"
      fi
      . set_jobstatus_abort
      ABORT=YES 
   fi
#8179 end
   RETURN_CODE=`LASTRC` ; export RETURN_CODE

case $BATCH_MODE in
   MVS)
      whence post_exec_pgm 2>&1 > $DEVNULL
      whenceret=$?
      if [ "$whenceret" -eq 0 ]
      then 
         post_exec_pgm
      fi 
   ;;
   VSE)
      whence post_exec_pgm.vse 2>&1 > $DEVNULL
      whenceret=$?
      if [ "$whenceret" -eq 0 ]
      then 
         post_exec_pgm.vse
      fi 
   ;;
esac

   if [ "a$VALIDATE" = "ay" ]
   then
      echo " "
      print_message EXECPGM "was executed in validation mode  COND-CODE=$CONDCODE"
   else
      echo " "
      print_message EXECPGM "was executed  RETURN-CODE=$RETURN_CODE, COND-CODE=$CONDCODE"
      echo " "
   fi

   print_message EXECPGM "start at TIME=$STEP_START_TIME" 
   STEP_STOP_TIME="`ebmtime`" ; export STEP_STOP_TIME
   print_message EXECPGM "stop  at TIME=$STEP_STOP_TIME"
   echo " "
else
   echo " "
   print_message EXECPGM "bypassed"
   echo " "
fi
   
if [ "a$BATCH_MODE" = "aMVS" ]
then
   if [ -f "assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}" ]
   then
      rm -f assgndd.${EBMSYS}.${JON}.${PROC_LEVEL}
   fi

   rm -f  *.setprint.DD.*.${EBMSYS}.${JON}.${PROC_LEVEL}

   if [ -f "${EXECPGM_STEPNAME}.assgndd.${EBMSYS}.${JON}.${prior_proc_level}" ]
   then
      rm -f ${EXECPGM_STEPNAME}.assgndd.${EBMSYS}.${JON}.${prior_proc_level}
   fi

   if [ -f "first_step.assgndd.${EBMSYS}.${JON}.${prior_proc_level}" ]
   then
      rm -f first_step.assgndd.${EBMSYS}.${JON}.${prior_proc_level}
   fi

fi
if [ "a$BATCH_MODE" = "aVSE" ]
then
   if [ -f assgndd.${EBMSYS}.${JON} ]
   then
      if [ ! -z $LN_SYSIN ]
      then
         echo "unset LN_SYSIN" >> assgndd.${EBMSYS}.${JON}
      fi
      if [ ! -z $DD_SYSIN ]
      then
         echo "unset DD_SYSIN" >> assgndd.${EBMSYS}.${JON}
      fi
      if [ ! -z $SYSIN_FILETYPE ]
      then
         if [ $SYSIN_FILETYPE = "INSTREAM" ]
         then
            echo "unset SYSIN_FILETYPE" >> assgndd.${EBMSYS}.${JON}
         fi
      fi
   fi
fi

rm -f pstepname.${EBMSYS}.${JON}.${PROC_LEVEL}
rm -f libdef_pgm_step.${EBMSYS}.${JON} 
rm -f libdef_cat_step.${EBMSYS}.${JON} 
rm -f libdef_proc_step.${EBMSYS}.${JON} 
rm -f label.${EBMSYS}.${JON}.${PROC_LEVEL}

rm -f accept.${EBMSYS}.${JON}
rm -f beginproc_first_step_in_proc.${EBMSYS}.${JON}.${PROC_LEVEL}
rm -f current_ddname.${EBMSYS}.${JON}
rm -f *.onretcode.${EBMSYS}.${JON}.${PROC_LEVEL}
cat <$DEVNULL > ${JON}_WK2a
cat <$DEVNULL > ${JON}_WK2n
if [ "$BATCH_MODE" = "VSE" ]
then
   if [ -f oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} ]
   then
      . oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} >$DEVNULL
   fi
   if [ ! -f bypassfile.${EBMSYS}.${JON} -a -f onretcode.${EBMSYS}.${JON}.${PROC_LEVEL} ]
   then
      . onretcode.${EBMSYS}.${JON}.${PROC_LEVEL} >$DEVNULL
   fi
fi
exit 0
