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


########################################################################
#                                                                      #
# EBM BATCH JOB MACRO :     BEGINPROC                                  #
# Purpose             :     Start a procedure                          #
#                                                                      #
########################################################################
. verify_jobstatus
. current_proc_level.${EBMSYS}.${JON}
. beginjob.$EBMSYS.$JON


#execute the callers setparm variables
if [ -f setparm.${EBMSYS}.${JON}.${PROC_LEVEL} ]
then
  . setparm.${EBMSYS}.${JON}.${PROC_LEVEL}
fi

#-----------------------------------------------------------------------
# Increment and set new procedure nesting level
#-----------------------------------------------------------------------
PROC_LEVEL=`expr $PROC_LEVEL + 1` ;  export PROC_LEVEL
CALLERS_PROC_LEVEL=`expr $PROC_LEVEL - 1` ; export CALLERS_PROC_LEVEL

. beginproc.inherit_callers_parms

#-----------------------------------------------------------------------
# Current nesting level inherits caller's condition code and return code
# directives. It can be modified at this level, but caller does not
# inherit from this level.       
#-----------------------------------------------------------------------
if [ -f oncondcode.${EBMSYS}.${JON}.${CALLERS_PROC_LEVEL} ]
then
   cat <$DEVNULL > oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
   cp oncondcode.${EBMSYS}.${JON}.${CALLERS_PROC_LEVEL} oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} 
   chmod 755 oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
fi

if [ "a$STEPNAME" != "a" ]
then
   if [ -f ${STEPNAME}.oncondcode.${EBMSYS}.${JON}.${CALLERS_PROC_LEVEL} ]
   then
      cat <$DEVNULL > ${STEPNAME}.oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
      cp ${STEPNAME}.oncondcode.${EBMSYS}.${JON}.${CALLERS_PROC_LEVEL} ${STEPNAME}.oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL} 
      chmod 755 ${STEPNAME}.oncondcode.${EBMSYS}.${JON}.${PROC_LEVEL}
   fi
fi

#-----------------------------------------------------------------------
# Inititalize the file that will contain a local history of the current 
# procedure's step return codes. Format: _procstepname_ rc
#-----------------------------------------------------------------------
cat <$DEVNULL > ./${JON}_PROC_RETCOD_${PROC_LEVEL} 
echo "rm -f ./${JON}_PROC_RETCOD_${PROC_LEVEL}" >> ${JON}_WK4n
echo "rm -f ./${JON}_PROC_RETCOD_${PROC_LEVEL}" >> ${JON}_WK4a
chmod 775 ./${JON}_PROC_RETCOD_${PROC_LEVEL}  
echo "rm -f ./${JON}_PROC_RETCOD_${PROC_LEVEL}.back" >> ${JON}_WK4n
echo "rm -f ./${JON}_PROC_RETCOD_${PROC_LEVEL}.back" >> ${JON}_WK4a

cat <$DEVNULL > onretcode.${EBMSYS}.${JON}.${PROC_LEVEL}
if [ -f onretcode.${EBMSYS}.${JON}.${CALLERS_PROC_LEVEL} ]
then
   cp onretcode.${EBMSYS}.${JON}.${CALLERS_PROC_LEVEL} onretcode.${EBMSYS}.${JON}.${PROC_LEVEL} 
   chmod 755 onretcode.${EBMSYS}.${JON}.${PROC_LEVEL}
fi

print_message_STEPNAME=''    ; export print_message_STEPNAME
print_message_PROCNAME=''    ; export print_message_PROCNAME

echo "_${STEPNAME}_ 0" > ${JON}_RET
cat <$DEVNULL > current_proc_level.${EBMSYS}.${JON}
echo "PROC_LEVEL=$PROC_LEVEL ;export PROC_LEVEL" >> current_proc_level.${EBMSYS}.${JON}
cat <$DEVNULL > beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}
chmod 755 beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}

echo "CALLERS_STEPNAME=$STEPNAME ; export CALLERS_STEPNAME" >> beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}

PARMS='';export PARMS

for argument in $*
do
  i=`eval echo $argument`
  case $i in
    procname=*)
       PROCNAME=`eval echo $i|cut -f2 -d'='` 
       export PROCNAME
       if [ "a$PROCNAME" != "a" ]
       then
          print_message_PROCNAME=$PROCNAME ; export print_message_PROCNAME

          echo "PROCNAME=$PROCNAME ; export PROCNAME" >> beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}

          echo "print_message_PROCNAME=$PROCNAME ; export print_message_PROCNAME" >> beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}
       else
          print_message BEGINPROC "procedure name is required." 
          . set_jobstatus_abort
       fi
    ;;
    parms=*)
       tmp_parms="`eval echo $i|cut -c7-`"
       export tmp_parms

       PARMS="$PARMS $tmp_parms"
       export PARMS
    ;;
    verbose*)
       VERBOSE=YES
    ;;
    *)
      print_message BEGINPROC "Invalid parameter specified :   $i" 
      . set_jobstatus_abort
      break
       ;;
  esac
done

if [ "$VERBOSE" = "YES" ]
then
   echo " "  
   print_message BEGINPROC "-----Starting in verbose mode-----" 
fi

if [ "$VERBOSE" = "YES" ]
then
   if [ "a$PARMS" != "a" ]
   then
      print_message BEGINPROC "default parameter values:"
      for p in `echo $PARMS|sed 's/,/ /g'`
      do
         print_message BEGINPROC "   `echo $p`"
      done
   fi
fi

if [ "a$PARMS" != "a" ]
then
   first_time=yes; export first_time
   for p in `echo $PARMS|sed 's/,/ /g'`
   do
      parm_name=`echo $p|cut -f1 -d'='`
      default_value=`echo $p|cut -f2 -d'='`

      env|grep "^${parm_name}=" > $DEVNULL 2>&1
      if [ $? -eq 0 ]
      then
         override_value=`env | grep "^${parm_name}=" | cut -f2 -d'='`
         echo "$parm_name=$override_value ; export $parm_name"  >> beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}
         if [ "$VERBOSE" = "YES" ]
         then
            if [ $first_time = yes ]
            then 
               first_time=no; export first_time
               print_message BEGINPROC "parameters overridden:"
            fi
            print_message BEGINPROC "   $parm_name=$override_value"
         fi
      else
         echo "$parm_name=$default_value" >> beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}
         echo "export $parm_name" >> beginproc.setparms.${EBMSYS}.${JON}.${PROC_LEVEL}
      fi
   done
fi

touch beginproc_first_step_in_proc.${EBMSYS}.${JON}.${PROC_LEVEL}
exit 0
