/* REXX */
/*                                                                  */
/*         Run through REPAIR output                                */
/* ---------------------------------------------------------------- */

call init_program
call open_log_file

call start_processing

call open_input_file

call read_input

call close_input_file
call end_processing
call close_log_file
EXIT error_rc

Init_program:
/* ---------------------------------------------------------------- */
/* Initialization of Program                                        */
/* ---------------------------------------------------------------- */
error_rc            = 0
error_location      = ''
err_reas.           = ''
/* ---------------------------------------------------------------- */
timestamp_date      = ''                 /*  Time init              */
timestamp_time      = ''
timestamp           = ''
timestamp_start     = ''
timestamp_end       = ''
/* ---------------------------------------------------------------- */
input_file          = 'REPIN'            /*  Input file init        */
input_file_open     = 'N'
eof_input_file      = 0
log_file            = 'REPLOG'           /*  Log file               */
log_file_open       = 'N'
RETURN

Open_log_file:
/* ---------------------------------------------------------------- */
/* Open the log file                                                */
/* ---------------------------------------------------------------- */
ADDRESS TSO
"EXECIO" 0 "DISKW" log_file "(OPEN"
error_rc = rc

if error_rc /= 0 then
  do
    say
    say 'Error at open of log file.'
    say 'Action     : EXECIO 0 DISKW ' !! log_file !! ' (OPEN'
    say 'Position   : 1001'
    say 'Return Code: ' error_rc
    say
    EXIT error_rc
  end
else log_file_open  = 'Y'
RETURN

Start_processing:
/* ---------------------------------------------------------------- */
/* Setting timestamp at starting point                              */
/* ---------------------------------------------------------------- */
call get_timestamp
timestamp_start = timestamp

err_reas.       = ''
err_reas.1      = 'Began checking at:' timestamp_start

call write_err_reas
RETURN

Open_input_file:
/* ---------------------------------------------------------------- */
/* Open the input file stream                                       */
/* ---------------------------------------------------------------- */
ADDRESS TSO
"EXECIO" 0 "DISKR" input_file "(OPEN"
error_rc = rc

if error_rc /= 0 then
  do
    error_location = '0001'
    call error_exit
  end
else input_file_open  = 'Y'
RETURN

Read_input:
/* ---------------------------------------------------------------- */
/* Read the input file stream                                       */
/* ---------------------------------------------------------------- */
drop repin.

call read_input_file

do until eof_input_file = 2
   call read_input_file
end
RETURN

Read_input_file:
/* ---------------------------------------------------------------- */
/* Read the input file stream record                                */
/* ---------------------------------------------------------------- */
repin. = ''

ADDRESS TSO
"EXECIO" 1 "DISKR" input_file "(STEM repin."
error_rc = rc

select
  when error_rc = 0  then
       call process_input
  when error_rc = 2  then
       eof_input_file = rc
  otherwise
    do
       error_location = '0003'
       call error_exit
   end
end
RETURN

Process_input:
/* ---------------------------------------------------------------- */
/* Process the input file stream record                             */
/* ---------------------------------------------------------------- */
select
   when substr(repin.1,2,7) = 'DSNU770' then /* Start of DB */
   do
     err_reas.  = ''
     err_reas.1 = 'Database ' !! substr(repin.1,75,8) !!,
                  ' started.'
     call write_err_reas
     found_index = 'N'
   end
   when substr(repin.1,2,7) = 'DSNU916' &,     /* Bad guy found */
        substr(repin.1,61,8) = 'OBDINNUM' then
   do
     found_index   = 'Y'
     found_exist   = ' '
     found_rebuilt = ' '
   end
   when substr(repin.1,2,7) = 'DSNU904' &,     /* Bad guy found */
        found_index = 'Y'                 then
   do
     found_exist   = substr(repin.1,45,7)
   end
   when substr(repin.1,2,7) = 'DSNU905' &,     /* Bad guy found */
        found_index = 'Y'                 then
   do
     found_rebuilt = substr(repin.1,45,7)
   end
   when substr(repin.1,2,7) = 'DSNU913' &,     /* Bad guy found */
        found_index = 'Y'                 then
   do
     err_reas.  = ''
     err_reas.1 = 'OBID ' !! substr(repin.1,29,7) !!,
                  ' is ' !! found_exist !! ' rebuilt ' !!,
                  found_rebuilt
     call write_err_reas
     found_index = 'N'
   end
   when substr(repin.1,2,7) = 'DSNU771' then /* End of DB */
   do
     err_reas.  = ''
     err_reas.1 = 'Database ' !! substr(repin.1,76,8) !!,
                  ' ended.'
     call write_err_reas
   end
   otherwise
     nop
end
RETURN

Close_input_file:
/* ---------------------------------------------------------------- */
/* Close the input file stream                                      */
/* ---------------------------------------------------------------- */
ADDRESS TSO
"EXECIO" 0 "DISKR" input_file "(FINIS"
error_rc = rc

if error_rc /= 0 then
  do
    error_location = '0006'
    call error_exit
  end
else input_file_open  = 'N'
RETURN

End_processing:
/* ---------------------------------------------------------------- */
/* Setting timestamp at ending point                                */
/* ---------------------------------------------------------------- */
call get_timestamp
timestamp_end   = timestamp

err_reas.       = ''
err_reas.1      = 'Ended checking at:' timestamp_end
err_reas.2      = 'Ended with return code ' !! error_rc !! '.'

call write_err_reas
RETURN

Close_log_file:
/* ---------------------------------------------------------------- */
/* Close the log file                                               */
/* ---------------------------------------------------------------- */
ADDRESS TSO
"EXECIO" 0  "DISKW" log_file "(FINIS"
error_rc = rc

if error_rc /= 0 then
  do
    say
    say 'Error at close of log file.'
    say 'Action     : EXECIO 0 DISKW ' !! log_file !! ' (FINIS'
    say 'Position   : 1002'
    say 'Return Code: ' error_rc
    say
    EXIT error_rc
  end
else log_file_open  = 'N'
RETURN

Get_timestamp:
/* ---------------------------------------------------------------- */
/* Get timestamp in DB2 format:  YYYY-MM-DD-HH.MM.SS.nnnnnn         */
/* ---------------------------------------------------------------- */
timestamp_date  = Date('s')
timestamp_date  = Insert('-',timestamp_date,4)
timestamp_date  = Insert('-',timestamp_date,7)
timestamp_date  = Insert('-',timestamp_date,10)

timestamp_time  = translate(time('L'),'.',':')

timestamp       = timestamp_date !! timestamp_time
timestamp       = strip(timestamp)

date_yy         = substr(timestamp,1,4)
date_mm         = substr(timestamp,6,2)
date_dd         = substr(timestamp,9,2)
time_hh         = substr(timestamp,12,2)
time_mi         = substr(timestamp,15,2)
time_ss         = substr(timestamp,18,2)

d_date          = date_yy !! '-' !! date_mm !! '-' !! date_dd
t_time          = time_hh !! ':' !! time_mi !! ':' !! time_ss
RETURN

Error_exit:
/* ---------------------------------------------------------------- */
/* Exit routine and error handling section                          */
/* ---------------------------------------------------------------- */
call get_timestamp
timestamp_end   = timestamp

ADDRESS TSO
err_header.   = ''
err_header.1  = 'Error detected.'

"EXECIO" 1 "DISKW" log_file "(STEM err_header."

err_reas. = ''

err_reas.1 = ' Location = ' !! error_location

Select
  when error_rc = 1 then
    do
       err_reas.2 = 'Data was truncated during DISKW operation.'
   end
  when error_rc = 4 then
    do
       err_reas.2 = 'During a DISKR or DISKRU operation,',
       'an empty data set'
       err_reas.3 = 'was found in a concatenation of data sets.'
   end
  when error_rc = 20 then
    do
       err_reas.2 = 'Severe error.'
   end
  otherwise
   nop
End
call write_err_reas

err_tail.     = ''
err_tail.1    = ' '
err_tail.2    = 'Ended checking at:' timestamp_end
err_tail.3    = 'Ended with return code ' !! error_rc !! '.'

"EXECIO" 3 "DISKW" log_file "(STEM err_tail."

ADDRESS TSO
If input_file_open     = 'Y' then
  "EXECIO" 0 "DISKR" input_file "(FINIS"
If log_file_open       = 'Y' then
  "EXECIO" 0  "DISKW" log_file "(FINIS"

EXIT error_rc

Write_err_reas:
/* ---------------------------------------------------------------- */
/* Write Error Reason Text                                          */
/* ---------------------------------------------------------------- */
ADDRESS TSO
"EXECIO * DISKW" log_file "(STEM err_reas."
RETURN

/* ***************************************************************** */
/*                                                                   */
/*        (C) COPYRIGHT SOFTWARE ENGINEERING GMBH 2013 - 2014.       */
/*                        ALL RIGHTS RESERVED.                       */
/*                                                                   */
/* ***************************************************************** */