/* REXX */
/*--------------------------------------------------------------------*/
/* ZDBATCH - common batch bridge for the zDIAG utility suite          */
/*                                                                    */
/* The selected full utility remains the diagnostic engine.           */
/* ZDBATCH supplies common validation, logging and RC handling.        */
/*--------------------------------------------------------------------*/

signal on syntax name Fatal

parse arg zbRaw
parse var zbRaw zbUtil zbParm
zbUtil = translate(strip(zbUtil))
zbParm = strip(zbParm)

zbList = 'JOBDIAG JES2DIAG CPUDIAG LONGDIAG STORDIAG ENQDIAG'
zbList = zbList' ASIDDIAG XCFDIAG ZFSDIAG LOGRDIAG'

if pos(' 'zbUtil' ',' 'zbList' ') = 0 then do
  call ErrorReport 'Invalid or missing zDIAG utility: 'zbUtil
  call LogLine 'ZDBATCH rejected invalid utility: 'zbUtil
  call WriteLog
  exit 12
end

zbStart = time('N')
call LogLine 'zDIAG batch bridge started'
call LogLine 'Utility    : 'zbUtil
call LogLine 'Parameters : 'zbParm
call LogLine 'System     : 'translate(mvsvar('SYSNAME'))
call LogLine 'User       : 'sysvar('SYSUID')
call LogLine 'Start time : 'zbStart

/* Trap only messages not written through the patched PresentReport. */
zbOut. = ''
zbOut.0 = 0
zbTrap = outtrap('zbOut.',2000)
zbMsg = trapmsg('ON')

zbCmd = '%'zbUtil
if zbParm <> '' then zbCmd = zbCmd' 'zbParm
address TSO zbCmd
zbRc = rc

zbMsg = trapmsg('OFF')
zbTrap = outtrap('OFF')

call LogLine 'Utility RC : 'zbRc
call LogLine 'End time   : 'time('N')

if zbOut.0 > 0 then do
  call LogLine 'Additional child output follows:'
  do zbI = 1 to zbOut.0
    call LogLine '  'zbOut.zbI
  end
end

call WriteLog
if result <> 0 then exit 20

/* RC 00 means a usable report was produced by the selected utility. */
if zbRc = 0 then exit 0
if zbRc = 12 then exit 12
exit 20


ErrorReport:
parse arg erText
er. = ''
er.0 = 6
er.1 = copies('=',78)
er.2 = ' zDIAG BATCH EXECUTION ERROR'
er.3 = copies('-',78)
er.4 = erText
er.5 = 'No usable diagnostic report was produced.'
er.6 = copies('=',78)
address TSO 'EXECIO 'er.0' DISKW ZREPORT (STEM er. FINIS)'
return


LogLine:
parse arg llText
if symbol('zlog.0') <> 'VAR' then do
  zlog. = ''
  zlog.0 = 0
end
zlog.0 = zlog.0 + 1
llN = zlog.0
zlog.llN = left(llText,132)
return


WriteLog:
if symbol('zlog.0') <> 'VAR' then return 0
if zlog.0 = 0 then return 0
address TSO 'EXECIO 'zlog.0' DISKW ZLOG (STEM zlog. FINIS)'
return rc


Fatal:
zbLine = sigl
call ErrorReport 'ZDBATCH REXX syntax/runtime failure at line 'zbLine'.'
call LogLine 'ZDBATCH fatal condition: 'condition('C')
call LogLine 'Description: 'condition('D')
call LogLine 'Source line: 'zbLine
if datatype(zbLine,'W') then call LogLine sourceline(zbLine)
call WriteLog
exit 20
