/* PX-NetEXP v2.2
   ~~~~~~~~~~~~~~
New Generation in Network Message Reporting
*/

CFGFILE="sysdata:pxnetrep.cfg"


if ~exists(CFGFILE) then do
     say ""
     say "PX NetRep 2 == Configuration file not found, aborting."
     say ""
     exit
end
if exists(CFGFILE) then do
     call open(infile,CFGFILE,"R")
     PATH.1=readln(infile)         /* Path 1 ('SYSDATA:LOG/')       */
     PATH.2=readln(infile)         /* Path 2 ('ifido.log')          */
     PATH.3=readln(infile)         /* Path 3 ('xfido.log')          */
     call close(infile)
end
if ~exists(PATH.1||PATH.3) then do
     say "*NPX-NetEXP -- '"||path.1||path.3||"' not found"
     exit
end
call open(infile,PATH.1||PATH.3,"R")
num=0
do until eof(infile) | FLAG=1
     text=readln(infile)
     len=length(text)
     text2=left(text,24)
     if text2="Total exported messages:" then do
          num=right(text,len-25)
          FLAG=1
     end
end
call close(infile)
if FLAG=0 then do
     say ""
     say "PX-NetEXP -- There must be an error in the file '"||PATH.1||PATH.2||"'"
     say ""
     exit
end
if exists("sysdata:exp2.dat") then do
     call open(infile,"sysdata:exp2.dat","R")
     todayimp=readln(infile)       /* 1. MSGS imported today */
     dayrecrd=readln(infile)       /* 2. SINGLE-DAY RECORD <NEW> */
     datelimp=readln(infile)       /* 3. DATE if last import (internal) */
     monthimp=readln(infile)       /* 4. MSGS imp'd this month */
     mnthrcrd=readln(infile)       /* 5. SINGLE-MONTH RECORD <NEW> */
     datemimp=readln(infile)       /* 6. MONTH of last import (month) */
     totalimp=readln(infile)       /* 7. MSGS imp'd TOTAL since install */
     dateiimp=readln(infile)       /* 8. INSTALL DATE (internal) */
     lastmnth=readln(infile)       /* 9. LAST MONTH TOTAL MSGS IMP'D */
     lstmnthn=readln(infile)       /* 10. LAST MONTH NAME */
     yestrday=readln(infile)       /* 11. YESTERDAY TOTAL MSGS IMP'D */
     ystrdayn=readln(infile)       /* 12. YESTERDAY NAME */
     times=readln(infile)          /* 13. Number of times imports logged */
     call close(infile)
     today=date('i')
     if today=datelimp then do
          todayimp=todayimp+num
          if todayimp>dayrecrd then dayrecrd=todayimp
     end
     if today~=datelimp then do
          diff=today-datelimp
          if diff=1 then do
               yestrday=todayimp
               ystrdayn=date('w',today-1)
          end
          if diff>1 then do
               yestrday=0
               ystrdayn=date('w',today-2)
          end
          datelimp=today
          todayimp=num
          drop diff
     end
     month=date('m')
     if month=datemimp then do
          monthimp=monthimp+num
          if monthimp>mnthrcrd then mnthrcrd=monthimp
     end
     if month~=datemimp then do
          lastmnth=monthimp
          lstmnthn=datemimp
          datemimp=month
          monthimp=num
     end
     totalimp=totalimp+num
     if today~=datelimp then times=times+1
     call open(outfile,"sysdata:exp2.dat","W")
     call writeln(outfile,todayimp)
     call writeln(outfile,dayrecrd)
     call writeln(outfile,datelimp)
     call writeln(outfile,monthimp)
     call writeln(outfile,mnthrcrd)
     call writeln(outfile,datemimp)
     call writeln(outfile,totalimp)
     call writeln(outfile,dateiimp)
     call writeln(outfile,lastmnth)
     call writeln(outfile,lstmnthn)
     call writeln(outfile,yestrday)
     call writeln(outfile,ystrdayn)
     call writeln(outfile,times)
     call close(outfile)
end
if ~exists("sysdata:exp2.dat") then do
     call open(outfile,"sysdata:exp2.dat","W")
     call writeln(outfile,num)
     call writeln(outfile,num)
     call writeln(outfile,date('i'))
     call writeln(outfile,num)
     call writeln(outfile,num)
     call writeln(outfile,date('m'))
     call writeln(outfile,num)
     call writeln(outfile,date('i'))
     call writeln(outfile,"0")
     call writeln(outfile,"*none*")
     call writeln(outfile,"0")
     call writeln(outfile,"*none*")
     call writeln(outfile,"1")
     call close(outfile)
end
say ""
say "PX-NetEXP -- Successfully completed"
say ""
exit
