################################################################################
#
#                        PROCEDURE: "XWAIT"
#
# Input parameters:
# XLIST (def=NO, YES or NO) specifies if the status output of the step 
#            submitted via XSUBMIT must be included or not in the status 
#            output of the job
# XSTEPNAME (mandatory) Specifies what XSUBMIT's step name XWAIT refers to
#
# Action:
# XWAIT will suspend the job execution until the step executing via XSUBMIT
# terminates. 
# The condition code of the step executed via XSUBMIT will affect the job status
# 
# Requires:
#                    MVS/JCL Translator "mvstrans" ( Version 9.0 or later )
#                    PPF Handler                   ( Version 4.0 or later )
#
################################################################################
# The begin of the XWAIT procedure
start msg="start=init,XLIST=YES,XSTEPNAME=null"
setenv PROCSTATUS 0
setenv SHELL /bin/ksh

#check if XSTEPNAME is specified
if ( "$XSTEPNAME" == "null" ) then
  echo "XWAIT: aborted, parameter XSTEPNAME must be defined"
  echo 1 > status.${JON}
  exit 1
endif

#check if XSTEPNAME is a valid XSUBMIT step name
if ( ! -f $HOME/$XSTEPNAME.$JON.name ) then
  echo "XWAIT: aborted, $XSTEPNAME does not refer to a XSUBMIT step name"
  echo 1 > status.${JON}
  exit 1
endif
if ( ! -f $HOME/$XSTEPNAME.$JON.jon ) then
  echo "XWAIT: aborted, $XSTEPNAME is not a valid XSUBMIT step name"
  echo 1 > status.${JON}
  exit 1
endif

# the following lines are to run XWAIT 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 read input files from the XSUBMIT procedure
setenv EXECNAME `cat $HOME/$XSTEPNAME.$JON.name`
setenv TEMPJOBNAME ${EXECNAME}_$JON
setenv TEMPJOBJON `cat $HOME/$XSTEPNAME.$JON.jon`

# the following lines are to suspend the job until the step terminates
echo "XWAIT: checking status of $EXECNAME"
insjob $TEMPJOBJON -w
if ( $status != 0 ) then
	 echo "XWAIT: step $EXECNAME, number $TEMPJOBJON aborted"
     setenv PROCSTATUS 1
else
	 echo "XWAIT: step $EXECNAME, number $TEMPJOBJON finished"
endif

# the following lines are to remove the step from the sysin
dltjob $TEMPJOBJON >& $DEVNULL
if ( $status != 0 ) then
  echo "XWAIT: step $EXECNAME, number $TEMPJOBJON deletion aborted"
  echo 1 > status.${JON}
  exit 1
endif

# the following lines are to list the status output of the step
if ( "$XLIST" == "YES" ) then
  lststs -j$TEMPJOBJON -c | sed -e 's/^/\+$EXECNAME\($TEMPJOBJON\)\ /' > XWAIT.list.${JON}
  echo "XWAIT: listing output of step $EXECNAME"
  cat XWAIT.list.${JON}
  echo "XWAIT: the end of listing output"
  rm XWAIT.list.${JON}
endif

# the following lines are to get back the return code and the condition code
if ( -f $HOME/$XSTEPNAME.$JON.retcod ) then
  cp $HOME/$XSTEPNAME.$JON.retcod ${JON}_RET
  cat ${JON}_RET >> ${JON}_RETCOD
endif
if ( -f $HOME/$XSTEPNAME.$JON.condcod ) then
  setenv PROCSTATUS `sh $HOME/$XSTEPNAME.$JON.condcod`
else
  setenv PROCSTATUS 255
endif

# the following lines are to delete temporary files created by XSUBMIT
rm $HOME/$XSTEPNAME.$JON.name
rm $HOME/$XSTEPNAME.$JON.jon
rm -f $HOME/$XSTEPNAME.$JON.retcod
rm -f $HOME/$XSTEPNAME.$JON.condcod

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