################################################################################
#
#                        PROCEDURE: "XSUBMIT"
#
# Input parameters:
# XPRIORITY (def=9, from 0 to 9) specifies the priority the step will submitted)
# XCLASS (def=a, from a to z) specifies the class the step will execute
# XTYPE (def=STD, UNIKIX or STD) specifies the type of batch subsystem used
#
# Action:
# XSUBMIT will execute the step specified by the SYSIN data set in parallel
# to the calling job or procedure stream.
# 
# Requires:
#                    MVS/JCL Translator "mvstrans" ( Version 9.0 or later )
#                    PPF Handler                   ( Version 4.0 or later )
#
################################################################################
# The begin of the XSUBMIT procedure
start msg="start=init,XPRIORITY=9,XCLASS=a,XTYPE=STD"
setenv PROCSTATUS 0
setenv PLN_SYSIN LN_first_step.SYSIN.$JON.0
setenv PROCNAME $STEPNAME

# check if the job is running in validation mode
if ( "a$VALIDATE" == "ay" ) then
  setenv VALOPT "-v"
else
  setenv VALOPT " "
endif

# check if the job is running in animation mode
if ( "$EBMANIM" != "0" ) then
  setenv ANMOPT "-a"
else
  setenv ANMOPT " "
endif

# check if the XTYPE parameter is invalid
switch($XTYPE)
case STD:
  setenv SUBCMD subjob
  setenv PRINTTYPE std
  breaksw
case UNIKIX:
  setenv SUBCMD unikixjob
  setenv PRINTTYPE unikix
  breaksw
default:
  echo "XSUBMIT: invalid TYPE parameter, use STD\/UNIKIX types"
  echo 1 > status.${JON}
  exit 1
endsw

# the following lines are to create temporary jmvs/ish directories
#setenv JMVSDIR `pwd`/.xsubmit.$JON
# back up one level for this because new scheme for cwd will
# create unique directory as part of cwd for each separate job
# so they won't be looking at the same directories for these files
#setenv JMVSDIR `pwd`/../.xsubmit.$JON
setenv JMVSDIR $HOME/.xsubmit.$JON
echo "JMVSDIR=$JMVSDIR"
if ( ! -d $JMVSDIR ) then
  mkdir $JMVSDIR
  if ( $status != 0 ) then
    echo "XSUBMIT: unable to create directory $JMVSDIR"
    echo 1 > status.${JON}
    exit 1
  endif
endif

if ( ! -d $JMVSDIR/jmvs ) then
  mkdir $JMVSDIR/jmvs
  if ( $status != 0 ) then
    echo "XSUBMIT: unable to create directory $JMVSDIR/jmvs"
    echo 1 > status.${JON}
    exit 1
  endif
endif

if ( ! -d $JMVSDIR/ish ) then
  mkdir $JMVSDIR/ish
  if ( $status != 0 ) then
    echo "XSUBMIT: unable to create directory $JMVSDIR/ish"
    echo 1 > status.${JON}
    exit 1
  endif
endif

# the following lines are to run XSUBMIT as a MVS nested procedure !!!
set PRNAME=(`echo $STEPNAME | sed -e 's/_/\ /'`)
echo "PROCNAME=$PRNAME[1]; export PROCNAME" >> ${JON}_WK3a
echo "PROCNAME=$PRNAME[1]; export PROCNAME" >> ${JON}_WK3n

# the following lines are to create the step file
set EXECSTEP=(`grep \" EXEC \" $PLN_SYSIN`)
setenv EXECNAME `echo $EXECSTEP[1] | sed -e 's/+\\\///'`
setenv TEMPJOBNAME $JOBNAME
echo "//$EXECNAME JOB" > $JMVSDIR/jmvs/$TEMPJOBNAME
cat $PLN_SYSIN | sed -e 's/+/\\\//' >> $JMVSDIR/jmvs/$TEMPJOBNAME
echo '//*EBM_BTSH*cp \${JON}_JCOND $HOME/$PROCNAME.$JON.condcod' >> $JMVSDIR/jmvs/$TEMPJOBNAME
echo '//*EBM_BTSH*cp \${JON}_RET $HOME/$PROCNAME.$JON.retcod' >> $JMVSDIR/jmvs/$TEMPJOBNAME
# the following lines are to translate the step
echo "XSUBMIT: translating step $EXECNAME"
setenv CURRDIR `pwd`
cd $JMVSDIR
if ( "a$VALIDATE" == "ay" ) then
  echo ""
  echo "--------------------------------------------------------------------------------"
  cat $JMVSDIR/jmvs/$TEMPJOBNAME
  echo "--------------------------------------------------------------------------------"
  echo ""
endif
mvstrans $TEMPJOBNAME -f >& $DEVNULL
if ( $status != 0 ) then
  echo "XSUBMIT: translation aborted, input file was $JMVSDIR/jmvs/$TEMPJOBNAME"
  echo "-------------------------------------------------------------------------------"
  cat $JMVSDIR/jmvs/$TEMPJOBNAME
  echo "-------------------------------------------------------------------------------"
  echo ""
  echo "XSUBMIT: output of translator:"
  echo "-------------------------------------------------------------------------------"
  mvstrans $TEMPJOBNAME -f
  echo "-------------------------------------------------------------------------------"
  cd $CURRDIR
  rm -f $JMVSDIR/jmvs/$TEMPJOBNAME 
  rm -f $JMVSDIR/ish/$TEMPJOBNAME
  rm -f $JMVSDIR/ish/$TEMPJOBNAME.*
  rmdir $JMVSDIR/jmvs 
  rmdir $JMVSDIR/ish
  rmdir $JMVSDIR
  echo 1 > $CURRDIR/status.${JON}
  exit 1
endif
cd $CURRDIR

#the following lines are to submit the step
echo "XSUBMIT: submitting step $EXECNAME, class=$XCLASS, priority=$XPRIORITY, type=$PRINTTYPE"
if ( "a$VALIDATE" == "ay" ) then
  echo ""
  echo "--------------------------------------------------------------------------------"
  cat $JMVSDIR/ish/$TEMPJOBNAME
  echo "--------------------------------------------------------------------------------"
  echo ""
endif
setenv TEMPJOBJON `$SUBCMD $TEMPJOBNAME -dk -p$XPRIORITY -c$XCLASS -j -W$JMVSDIR -k$EBMSYS $VALOPT $ANMOPT`
echo "XSUBMIT: step $EXECNAME submitted, number $TEMPJOBJON"

echo "abtjob $TEMPJOBJON \> $DEVNULL" >> ${JON}_WK4a
echo "insjob $TEMPJOBJON -w \> $DEVNULL" >> ${JON}_WK4a
echo "dltjob $TEMPJOBJON \> $DEVNULL" >> ${JON}_WK4a
echo "rm -f $HOME/$PROCNAME.$JON.jon" >> ${JON}_WK4a
echo "rm -f $HOME/$PROCNAME.$JON.name" >> ${JON}_WK4a

# the following lines are to remove the temporary files and directories
rm $JMVSDIR/jmvs/$TEMPJOBNAME $JMVSDIR/ish/$TEMPJOBNAME
rmdir $JMVSDIR/jmvs $JMVSDIR/ish
rmdir $JMVSDIR

# the following lines are to create input files for the XCHECK procedure
echo $TEMPJOBJON > $HOME/$PROCNAME.$JON.jon
echo $EXECNAME > $HOME/$PROCNAME.$JON.name

# the end of XSUBMIT procedure
echo $PROCSTATUS > status.${JON}
exit 0
