/*

   AnnounceAutoAdd.rexx 1.0 by Johan Billing 1994

   This script will scan CrashMail's logfile and announce any auto-added
   and forward-requested areas in an area of your choice. 

*/

/* Configuring */

logfile = "Work:UMS/CrashMail/CrashMail.log"
crashwrite = "Work:UMS/CrashMail/CrashWrite"
announcearea = "KCC_INFO"
node = "2:200/207.6"

/* Script starts here! */

options results

lastline=""
areas=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)="AnnounceAutoAdd.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)~="AnnounceAutoAdd.rexx logscan" then do
 call seek('file',pos,'B')
end

/* Scan for auto-added and forward-requested areas */

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

 area=""

 if substr(str,21,12)="Unknown area" then do
  if pos("auto-adding",str)~=0 then do
   realline=substr(str,21)
   parse var realline 'Unknown area ' area ' '
  end
 end

 if substr(str,21,8)="AreaFix:" then do
  if pos("requested from",str)~=0 then do
   realline=substr(str,21)
   parse var realline 'AreaFix: ' area ' '
   area=area || ' (AreaFix forward-request)'
  end
 end

 if area~="" then do
  if areas=0 then do
   call open('outfile','T:AnnounceAutoAdd.tmp','W')
   call writeln('outfile','New areas:')
   call writeln('outfile','')
  end

  call writeln('outfile',' 'area)
  areas=areas+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)
call writeln('file',date || " " || time('n') || "  " || "AnnounceAutoAdd.rexx logscan")
close('file')

/* No areas found? */

if areas=0 then exit

/* Close tempfile */

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

/* *** WRITING *** */

address command crashwrite 'FN "CrashMail" FA 'node' TN "All" TA 'node' SUBJ "New areas" ORIGIN "Yet another interesting message" AREA "'announcearea'" T:AnnounceAutoAdd.tmp'

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

call delete('T:AnnounceAutoAdd.tmp') 
