/*

   NotifyErrors.rexx 1.0 by Johan Billing 1995

   This script will scan CrashMail's logfile and send any lines with errors
   (lines with the prefixes "!" or "%") to you in a message.

*/

/* Configuring */

logfile = "Work:UMS/CrashMail/CrashMail.log"
crashwrite = "Work:UMS/CrashMail/CrashWrite"
notifyarea = "KCC_ERRORS"
node = "2:200/207.6"
inbound = "MAIL:Inbound"

/* Script starts here! */

options results

lastline=""
lines=0

IF ~SHOW(Libraries,'rexxsupport.library') THEN
    IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN EXIT

/* Start reading the log... */

call open('file',logfile,'R')

/* Skipping the part we have read already */

pos=0

do while ~eof('file')
 str=readln('file')
 if substr(str,21)="NotifyErrors.rexx logscan" then pos=seek('file',0,'Current')
end

/* If we didn't find the string, the logfile must have been cleared and we
   should read it from the beginning */

if substr(str,21)~="NotifyErrors.rexx logscan" then do
 call seek('file',pos,'B')
end

/* Scan for errors */

do while ~eof('file')
 str = readln('file')
 if str~="" then lastline=str

 if left(str,2)="! " | left(str,2)="% " then do
  begin
   if lines=0 then do
    call open('outfile','T:NotifyErrors.tmp','W')
   end

   call writeln('outfile',str)
   lines=lines+1
  end
end

call close('file')

/* Write mark to logfile */

call open('file',logfile,'A')
rawdate=date('n')
date=left(rawdate,2) || "-" || substr(rawdate,4,3) || "-" || substr(rawdate,10,2)

/* "/" is the logfile prefix for miscellaneous information */

call writeln('file',"/ " || date || " " || time('n') || "  " || "NotifyErrors.rexx logscan")
close('file')

/* No lines found? */

if lines=0 then exit

/* Close tempfile */

call writeln('outfile','')
call close('outfile')

/* *** WRITING *** */

address command crashwrite 'FN "CrashMail" FA 'node' TN "All" TA 'node' SUBJ "Errors" ORIGIN "Yet another interesting message" AREA "'notifyarea'" DIR "'inbound'" T:NotifyErrors.tmp'

/* Yes, the line is VERY long... */

call delete('T:NotifyErrors.tmp')
