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


########################################################################
#                                                                      #
# Batch Utility       :     IEBEDIT                                    #
# Purpose             :     Copy JCL stream to file                    #
#                           Submit jcl stream for translation and      #
#                           execution                                  #
#                                                                      #
########################################################################
if_in_validation() {
   if [ $invalidation -eq 1 ]
   then
      #echo "PGM=`basename $0` in validation mode is not executed"
      echo "IEBEDIT  (I) Executing in validation mode"
      echo 0 > ${original_workdir}/status.${JON}
      exit 0
   fi
   return;
}
#
# START OF MAIN SHELL SCRIPT CODE
#
original_workdir=`pwd`
echo " "
echo "Start:  IEBEDIT" 
echo " "
if [ "a$VALIDATE" = "ay" ]
then
   invalidation=1
   export invalidation
else
   invalidation=0
   export invalidation
fi
if [ "a$DD_SYSUT1" = a ]
then
   echo "IEBEDIT  (S) ERROR: SYSUT1 is undefined"
   if_in_validation
   # if in validation we will not return
   echo 16 > ${original_workdir}/status.${JON}
   exit 16
fi
if [ "a$DD_SYSUT2" = a ]
then
   echo "IEBEDIT  (S) ERROR: SYSUT2 is undefined"
   if_in_validation
   # if in validation we will not return
   echo 16 > ${original_workdir}/status.${JON}
   exit 16
fi

if [ "a$DD_SYSIN" != a -a "$DD_SYSIN" != "$DEVNULL" ]
then
   #
   # We do NOT support SYSIN file control statements to IEBEDIT
   # If any SYSIN data is provided we should ignore it
   # and continue with the IEBEDIT command
   #
   echo " "
   echo "IEBEDIT  (W) IEBEDIT Control statements are not supported in the current version"
   echo " "
   if_in_validation
   # if in validation we will not return
   SETRETCODE 12
fi

if [ "a$SYSUT2_FILETYPE" = a ]
then
  #Do not  have file type
  if [ "a$SYSUT2_FILENAME" != a ]
  then
     #Have filename, determine if file type is SYSOUT
     echo $SYSUT2_FILENAME | grep sysoutdir >$DEVNULL 2>&1
     if [ $? -eq 0 ]
     then
        SYSUT2_FILETYPE=SYSOUT
     fi
  else
     #Do not have file type or file name
     if [ "a$DSN_SYSUT2" = a ]
     then
        #Do not have file type, file name or datasetname to search File_Map
        echo "IEBEDIT  (S) ERROR: SYSUT2 - Unable to determine filename or type  "
        if_in_validation
        # if in validation we will not return
        echo 16 > ${original_workdir}/status.${JON}
        exit 16
       
     else 
        sysut2_filemap_filetype=`ftval $DSN_SYSUT2 $JOB_CAT 3`
        if [ $? -ne 0 ]
        then
           echo "IEBEDIT  (S) ERROR: accessing File_Map: ftval $DSN_SYSUT2 $JOB_CAT 3"
           if_in_validation
           # if in validation we will not return
           echo 16 > ${original_workdir}/status.${JON}
           exit 16
        else
           if [ "a$sysut2_filemap_filetype" != FS ]
           then
              echo "IEBEDIT  (S) ERROR: SYSUT2 file type must be FS or SYSOUT" 
              if_in_validation
              # if in validation we will not return
              echo 16 > ${original_workdir}/status.${JON}
              exit 16
           else
              SYSUT2_FILETYPE=FS
           fi
        fi
     fi
  fi 
fi

 
case $SYSUT2_FILETYPE in
   FS|fs)
      errorval=0
      if [ "a$SYSUT2_FILENAME" = a ]
      then
         if [ "a$DSN_SYSUT2" != a ]
         then
            filemap_filename=`ftval $DSN_SYSUT2 $JOB_CAT 4`
            if [ $? -ne 0 ]
            then 
              echo "IEBEDIT  (S) ERROR: accessing File_Map: ftval $DSN_SYSUT2 $JOB_CAT 4"
              errorval=16
            else 
               if [ "a$filemap_filename" = a ]
               then 
                  echo "IEBEDIT  (S) ERROR: Unable to determine output file name, verify File_Map"
                  errorval=16
               else
                  SYSUT2_FILENAME=$filemap_filename
               fi
            fi
         else
            echo "IEBEDIT  (S) ERROR: Unable to determine output file name"
            echo "IEBEDIT  (S) ERROR: dataset or filename is required"
            errorval=16
         fi 
      fi
      if [ $errorval -gt 0 ]
      then
         if_in_validation
         # if in validation we will not return
         echo $errorval > ${original_workdir}/status.${JON}
         exit $errorval
      else
         if_in_validation
         # if in validation we will not return
         #
         # may have concatenated input for sysut1, which is only
         # valid if doing a copy of sysut1 to sysut2.  
         # splitconcpaths will replace colon seperators with 
         # spaces, so we can 'cat' all the files to sysut2
         #
         tmp_dd_sysut1=`ebmsed $DD_SYSUT1 splitconcpaths`
         cat $tmp_dd_sysut1 > $SYSUT2_FILENAME
         cat_stat=$?
         if [ $cat_stat -ne 0 ]
         then
            echo "IEBEDIT  (S) ERROR: SYSUT1 to SYSUT2 cat failed"
            echo 16 > ${original_workdir}/status.${JON}
            exit 16
         else
            echo "IEBEDIT  (I) copy complete, file created: $SYSUT2_FILENAME"
         fi
      fi
   ;;
   SYSOUT|sysout)
      if_in_validation
      # if in validation we will not return
      if [ "a$SYSUT2_WRITER" != a ]
      then
         #
         # at various times we need to cat or copy the
         # SYSUT1 file, but we need to know if we're dealing
         # with an instream line sequential versus a perm file
         # determine which we have, set a local var to be the
         # same and reference the appropriate file through the
         # local var
         #
         # use the '-n' instead of '-f' because we may
         # have concatentated input 'a:b' which would
         # fail the '-f' check. we are ASSUMING that since
         # we build the LN_SYSUT1 variable, the contents
         # must be good if it exists.
         #
         if [ -n "$LN_SYSUT1" ]
         then
            sysut1_file=`eval echo $LN_SYSUT1`
         else
            sysut1_file=`eval echo $DD_SYSUT1`
         fi
#
# SYSUT1 may have been concatenated.  If so, split into a temp
# file and change sysut1_file to the name of the temp file.
#
         echo $sysut1_file | grep ":"
         if [ $? -eq 0 ]
         then
            sysut1_file=`ebmsed $sysut1_file splitconcpaths`
         fi
         if [ $SYSUT2_WRITER = INTRDR ]
         then
            workdir=${HOME}/.IEBEDIT
            #
            # whenever doing the remove of this workdir, use 
            # the full path just to make sure we don't get a 
            # messed up variable and 'rm -fr' something erroneous
            #
            rm -fr ${HOME}/.IEBEDIT 
            mkdir $workdir 
            if [ $? != 0 ]
            then
               echo "IEBEDIT  (S) Unable to create work directory: $workdir"
               echo 16 > ${original_workdir}/status.${JON}
               exit 16
            else
               command="mkdir ${workdir}/jmvs ${workdir}/ish ${workdir}/include"
               $command
               if [ $? != 0 ]
               then
                  echo "IEBEDIT  (S) Unable to create work directories: $command"
                  echo 16 > ${original_workdir}/status.${JON}
                  exit 16
               fi
            fi
        
            echo "IEBEDIT  (I) submitting job stream for translation: "
            echo "IEBEDIT  (I) start JCL stream:"
            echo " "
            # cat $DD_SYSUT1
            cat $sysut1_file
            echo " "
            echo "IEBEDIT  (I) end JCL stream:"
            echo " "
            echo " "
	    #
	    # need to get this job's name from the job card
	    # of SYSUT1
	    #
#
# if line sequential file exists, use it for the while read
# otherwise, use the record format version.  Can't just 
# default to DD (record format) version because if instream,
# the file won't have newlines, and can't be processed by the
# read of the 'while read'
#
            cat $sysut1_file | while read linein
	    do
               tmp=`echo $linein | cut -f1 -d' '`
               jobname=`echo $tmp | cut -f3 -d'/'`
	       break
	    done
	    #
	   if [ "a${jobname}" = "a" ]
	   then
              echo 16 > ${original_workdir}/status.${JON}
              echo "IEBEDIT  (S) job name not found in DD_SYSUT1"
              exit 16
	   fi
            # cp $sysut1_file  ${workdir}/jmvs/${jobname}
            # could be dealing w/ concatenated list, so cat
            # instead of cp
            cat $sysut1_file > ${workdir}/jmvs/${jobname}
            cd $workdir
            mvstrans ${jobname} -v
            if [ $? -eq 0 ]
            then
               mvstrans ${jobname} -f
               if [ $? -eq 0 ]
                  then
                  echo " "
                  echo "IEBEDIT  (I) submitting batch job for execution: "
                  if [ "a$VERBOSE" = "aYES" ]
                  then
                     echo "IEBEDIT  (I) start display of batch job:"
                     echo " "
                     cat ish/${jobname}
                     echo " "
                     echo "IEBEDIT  (I) end display of batch job:"
                  fi
                  echo " "
                  if [ "a$KIXSYS" != a ]
                  then 
                     unikixjob ${jobname}  -k${EBMSYS}
                  else
                     subjob ${jobname} -k${EBMSYS}
                  fi
####################
# the following lines may be commented out if you wish to keep
# the spawned job's jmvs, ish, include directories and files
# for later viewing.  However, note that these files are under
# the home directory of the uid which submitted the original
# job and may not be secure
####################
                  cd ${HOME}
                  rm -fr ${HOME}/.IEBEDIT
                  cd ${original_workdir}
               else
                  echo "IEBEDIT  (S) mvstrans ${workdir}/jmvs/${jobname} -f failed"
                  echo 16 > ${original_workdir}/status.${JON}
                  exit 16
               fi
            else
               echo "IEBEDIT  (S) mvstrans ${workdir}/jmvs/${jobname} -v failed"
               echo 16 > ${original_workdir}/status.${JON}
               exit 16
            fi
         else
            if_in_validation
            # if in validation we will not return
            tmp_dd_sysut1=`ebmsed $DD_SYSUT1 splitconcpaths`
            cat $tmp_dd_sysut1 > $SYSUT2_FILENAME
           # cp $sysut1_file $DD_SYSUT2
         fi
      fi
   ;;
   *)
      echo "IEBEDIT  (S) ERROR: output filetype not supported: $SYSUT2_FILETYPE"
      if_in_validation
      # if in validation we will not return
      echo 16 > ${original_workdir}/status.${JON}
      exit 16
   ;;
esac
echo 0 > ${original_workdir}/status.${JON}
echo "End:    IEBEDIT" 
exit 0
