/* rexx */
arg db2s
/*
"free f(BPOOL OUTP)"
"alloc f(BPOOL) Da('FLETCHP.P061BP2.TXT') SHR"
"alloc f(OUTP) da('FLETCHP.BPOOL.TXT') shr"
*/
"execio 1 diskr INPUT("
rcd = rc
if rc <> 0 then do
   say 'error reading INPUT DD return code 'rcd
   exit 16
end
colnames = ''
inpdata = ''
do while rcd = 0
   pull rec
   ep = pos('=',rec)
   if ep = 0 then do
      say 'Format of INPUT must be colname = colvalue'
      exit 16
   end
   colnames = substr(colnames,1)strip(substr(rec,1,ep-1))','
   inpdata = substr(inpdata,1)strip(substr(rec,ep+1))','
   "execio 1 diskr INPUT("
   rcd = rc
end
"execio 1 diskr INPUT(finis"
filefnd = 'n'
SUB = 1
X=OUTTRAP('LINE.')
"LISTA St"
DO Z = 1 TO LINE.0
   IF word(LINE.Z,1) = 'BPOOL' then filefnd = 'y'
END
if filefnd = 'n' & db2s = '' then do
   say 'You must either provide a file for DD name BPOOL'
   say 'or provide the DB2 system name'
   exit 16
end

if filefnd = 'y' then do
   rcd = 0
   start = 1
   b401fnd = 'n'
   "execio 1 diskr bpool("
   rcd = rc
   bp. = ''
   z = 0
   do while rcd = 0
      pull inp
      if word(inp,1) = '1READY' then start = 2
      else if pos('ISF031I',inp) > 0 then do
         start = pos('ISF031I',inp)-1
         jdate = word(inp,2)
         call convdate
      end
      inp = substr(inp,start)
      if word(inp,1) = 'DSNB401I' then b401fnd = 'y'
      if b401fnd = 'y' then do
         z = z + 1
         bp.z = inp
      end
      "execio 1 diskr bpool("
      rcd = rc
   end
   bp.0 = z
end
else do
   A=OUTTRAP('bp.')
   queue '-dis bpool(*) detail'
   queue 'end'
   "dsn s("db2s")"
end
hdr = 'y'
first = 'y'
hd = colnames
total = 0
fieldcount = ''
msgnames = ''
msgfound = ''
data = inpdata
msg = ''
first01 = 'y'
first546 = 'y'
do z = 1 to bp.0
   msgstart = substr(word(bp.z,1),1,4)
   if msgstart = 'DSNB' ,
   | msgstart = 'DSN9' then do
      if first = 'y' then do
         first = 'n'
      end
      else call process_msg
   end
  msg = msg bp.z
end
call process_msg
if hdr = 'y' then do
   hdr = 'n'
   queue hd
end
queue data
"execio "queued()" diskw outp(finis"
'free f(outp)'
process_msg:
/*
if pos('BP3',data) > 0 then trace ?i
*/
msgnum = word(msg,1)
if hdr = 'n' & msgnum <> 'DSNB401I' then do
   wp = wordpos(msgnum,msgnames)
   if wordpos(msgnum,msgnames) = 0 then do
      msg = ''
      return
   end
   if words(fieldcount) > 0 then ,
   if first01 = 'n' then ,
   if wp <> next & next <= words(msgnames)then do
      numoccs = word(fieldcount,next)
      do sub = 1 to numoccs
         data = substr(data,1)'n/a,'
      end
   end
end
if msgnum = 'DSNB401I' then do
   if next > 1 then ,
      if next <= words(msgnames) then do
         do while next <= words(msgnames)
            numoccs = word(fieldcount,next)
            do sub = 1 to numoccs
               data = substr(data,1)'n/a,'
            end
            next = next + 1
         end
      end
   if first01 = 'y' then first01 = 'n'
   else do
      if hdr = 'y' then do
         hdr = 'n'
         queue hd
      end
      queue data
      next = 1
      "execio "queued()" diskw outp("
      msgfound = ''
   end
   if hdr = 'y' then do
      hd = colnames 'System,' word(msg,3) word(msg,4)','
      hd = hd word(msg,6) word(msg,7)','
      hd = hd word(msg,9) word(msg,10) ','
      msgnames = word(msg,1)
      fieldcount = 3
   end
   db2s = strip(word(msg,2))
   if length(db2s) > 4 then db2s = substr(db2s,2)
   data = substr(inpdata,1)db2s','strip(word(msg,5),'t',',')
   bpoolname = word(msg,5)
   data = data ','strip(word(msg,8),'t',',')','
   data = substr(data,1)word(msg,11)','
end
ELSE ,
if msgnum = 'DSNB546I' then do
   if hdr = 'y' then do
      hd = hd word(msg,3) word(msg,4) word(msg,5)','
      hd = hd 'Buffers Using Frame Size,'
      msgnames = msgnames word(msg,1)
      fieldcount = fieldcount 2
   end
   data = substr(data,1)word(msg,6)','word(msg,7)','
end
ELSE ,
if msgnum = 'DSNB409I' | msgnum = 'DSNB410I' then do
   if hdr = 'y' then do
      hd = hd word(msg,3) word(msg,4) word(msg,5)' Time,'
      hd = hd word(msg,3) word(msg,4) word(msg,5)' Date,'
      msgnames = msgnames word(msg,1)
      fieldcount = fieldcount 2
   end
   sp = pos('SINCE',msg)
   cp = pos(':',msg,sp)
   data = substr(data,1)word(msg,6)','
   date = substr(msg,cp+7)
   cp = pos(',',date)
   date = substr(date,1,cp-1)substr(date,cp+1)
   date = word(date,2)'-'word(date,1)'-'word(date,3)
   data = substr(data,1)date','
end
ELSE IF msgnum = 'DSNB402I' ,
| msgnum = 'DSNB404I' ,
| msgnum = 'DSNB411I' ,
| msgnum = 'DSNB412I' ,
| msgnum = 'DSNB413I' ,
| msgnum = 'DSNB414I' ,
| msgnum = 'DSNB415I' ,
| msgnum = 'DSNB420I' ,
| msgnum = 'DSNB421I' ,
| msgnum = 'DSNB432I' ,
| msgnum = 'DSNB440I' ,
| msgnum = 'DSNB441I' ,
| msgnum = 'DSNB406I' THEN DO
  if hdr = 'y' then ,
     msgnames = msgnames word(msg,1)
  mnum = word(msg,1)
  msg = substr(msg,17)
  epos = pos('=',msg)
  count = 0
  start = 1
  do while epos > 0
     if hdr = 'y' then do
        hd = hd strip(substr(msg,start,epos - start))','
        count = count + 1
     end
     start = epos + 1
     do while substr(msg,start,1) = ' '
        start = start + 1
     end
     last = 'n'
     do while last = 'n'
        start = start + 1
        if start > length(msg) then last = 'y'
        if substr(msg,start,1) = ' ' then last = 'y'
     end
     dt  = strip(substr(msg,epos+ 1,start-epos))
     if substr(dt,length(dt),1) = ',' & mnum = 'DSNB404I' then do
        dt2 = word(msg,wordpos(dt,msg)+1)
        dt = substr(dt,1,length(dt)-1)
        HD = SUBSTR(HD,1,LENgth(HD)-1)' %, VERTICAL DEFERRED WRITE NUM,'
        dt = dt','dt2
        count = count + 1
     end
     data = substr(data,1)dt','
     epos = pos('=',msg,epos+1)
  end
   if hdr = 'y' then do
      fieldcount = fieldcount count
      total = total + count
   end
end
msg = ''
if hdr = 'n' then ,
next = next + 1
return
convdate:
   months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'
   tyear = substr(jdate,1,4)
   YEAR = Substr(Jdate,3,2)
   JULN = Substr(Jdate,5,3)
   BYEAR = (TYEAR / 4)
   XYEAR = TRUNC(BYEAR)
   ZYEAR = BYEAR - XYEAR
   If ZYEAR > 0 Then ,
   days =  '0   31  59  90 120 151 181 212 243 273 304 334'
   else ,
   days = '0   31  60  91 121 152 182 213 244 274 305 335'
   mm = 12
   do while juln <= word(days,mm)
      mm = mm -1
   end
    MONTH = mm
    numcom = 0
    CALC = word(days,mm)
    DAY = JULN - CALC
    If DAY < 10 Then DAY = 0||DAY
    ndate = day'-'word(months,month)'-'tyear
    lp=lastpos('CMDDATE',colnames)
    if lp > 0 then do
       colnameso  = colnames
       colnames = substr(colnames,1,lp-1)
       cp = pos(',',colnames)
       numcom = numcom + 1
       do while cp > 0
          cp = pos(',',colnames,cp+1)
          if cp > 0 then ,
          numcom = numcom + 1
       end
       do x = 1 to numcom
          cp = pos(',',inpdata,cp+1)
       end
    inpdata = substr(inpdata,1,cp)
    end
    colnames = colnames'CMDDATE,CMDTIME,'
    inpdata = substr(inpdata,1)ndate','substr(word(inp,3),1,8)','
return
