!-----------------------------------------------------------------------
!  @ pipeline-odp-report.cal  OutputFile
!    /REFDATE RefDate     (YYYYMMDD)
!    /LOGFILE InputFile
!-----------------------------------------------------------------------

sic\parse 1 /refdate 1 /logfile 1

define char*256 outfile indexdir
let outfile 'pro%parse%command%arg[1]'
let indexdir "/vis/mrt/mrtcal-dir/index"

! Ensure the previous file is not kept in case of failure
sic delete 'outfile'

!-----------------------------------------------------------------------
! 1) Merge the index files (from RefDate to Yesterday) in the named
!    index file
define integer refdate yesterday
datetime /from previous DAY /to yesterday GAG_DATE
if pro%parse%refdate%narg.gt.0 then
  datetime /from pro%parse%refdate%arg[1] YYYYMMDD /to refdate GAG_DATE
else
  let refdate yesterday
endif
!
define logical first
let first yes
define integer jdate
define char*128 indexfile
for idate refdate to yesterday
  let jdate idate  ! idate is float, convert to int
  datetime /from jdate GAG_DATE /to indexfile YYYYMMDD
  let indexfile 'indexdir'"/"'indexfile'".mrt"
  say 'indexfile' 'file(indexfile)'
  if file(indexfile) then
    if first then
      index open   /file 'indexfile'
      let first no
    else
      index append /file 'indexfile'
    endif
  endif
next

mfind
! mlist

! ZZZ This temporary file has to be saved in a controled place in the
! future
mcopy "pipeline-odp-report-index.mrt"

!-----------------------------------------------------------------------
! 2) Invoke the standard pipeline-report

define char*8 mydate
datetime /from refdate GAG_DATE /to mydate YYYYMMDD

if pro%parse%logfile%narg.eq.1 then
  @ pipeline-report.cal "online" 'outfile' -
    /refdate 'mydate' -
    /index "pipeline-odp-report-index.mrt" -
    /logfile 'pro%parse%logfile%arg[1]'
else
  @ pipeline-report.cal "online" 'outfile' -
    /refdate 'mydate' -
    /index "pipeline-odp-report-index.mrt"
endif
