/*
 * Simple brake's emsilog analyzer(ReXX Script)
 * (c) by Kirya Perevalov, 2:5010/183
 *	  14 March 1999 (fixed 30 Apr 1999)
 *   looks like T-Lan's emsi analyzer
 *
 * keys:
 * 	All - ขฅแ์ ฅฌแจซฎฃ
 *	Day - แฅฃฎคญ๏่ญจฉ คฅญ์
 *	Last -  กฎ็จฉ คฅญ์(ขเฅฌ๏ เ กฎโ๋ - ฏฅเฅฌฅญญ ๏ Work)
 *	<DateFrom> <DateTo> - ฏเฎฌฅฆใโฎช ขเฅฌฅญจ (ex. 22/02/99 21/03/99)
 *			      ฅแซจ ญฅ ใช ง ญฎ DateTo, DateTo = date() - แฅฃฎคญ๏
 *
 */

/************* YOUR SETTINGS ************/

Yours = '2:5010/152'    	/* Your 4D address */
System = 'Fox-Sage'      	/* Your system	   */
Emsilog = 'd:\ftn\log\Brake!.ems'
Out = 'd:\ftn\log\emsi.txt'
IsPoint = No
/* 
 * แซจ ข๋ ญฎค  โฎ ฏเจ งญ ็ฅญจจ Yes ํโฎฉ ฏฅเฅฌฅญญฎฉ ข ่จ ฏฎจญโ๋
 * กใคใโ ฎโฌฅ็ โ์แ๏ ขฎโ โ ช - Point .4, จญ ็ฅ - 2:5010/183.4
 */

Work = '22:00-07:00'
/* 
 * เจ ชซ๎็จชฅ LAST กใคฅโ แ็จโ โ์ แ ฏฅเขฎฃฎ ขเฅฌฅญจ 
 * ฏฅเฅฌฅญญฎฉ ข็ฅเ ่ญฅฃฎ คญ๏, คฎ ขโฎเฎฃฎ ขเฅฌฅญจ แฅฃฎคญ๏่ญฅฃฎ
 *  ฏเจฌฅเ ฏเจ Work = '22:00-6:59' ฏเจ ชซ๎็จชฅ LAST กใคฅโ แ็จโ โ์ 
 * ฎโ 22:00 ข็ฅเ ่ญฅฃฎ คญ๏ คฎ 6:59 แฅฃฎคญ๏่ญฅฃฎ
 */



/******************** MAIN PART *********************/


parse arg DateFrom DateTo
parse upper var datefrom datefrom
parse upper var dateto dateto
parse var work timefrom '-' timeto

parse var datefrom a1'/'b1'/'c1

If datefrom = '' then datefrom = 'DAY'
if datefrom = 'DAY' then
  do
   datefrom = date('E')' 00:00'
   dateto = date('E')' 23:59'
  end
Else
  If datefrom = 'LAST' then
    do
     parse value date('E') with a1 '/' b1 '/' c1
     a1 = a1-1
     datefrom = a1'/'b1'/'c1' 'timefrom
     dateto = date('E')' 'timeto
   end
   Else do
         If datatype(c1) = 'NUM' then  datefrom = datefrom' 00:00'       
	 If dateto = '' then dateto = date('E')' 24:00'
         else dateto = dateto' 23:59'
	end
n = 0
parse var datefrom DayFrom'/'MonthFrom'/'YearFrom' 'HourFrom':'MinFrom
parse var dateTo DayTo'/'MonthTo'/'YearTo' 'HourTo':'MinTo


/** ๆจชซ **/
do l = 1 while lines(emsilog)
 ems = linein(emsilog)
 If word(ems,4) \= 'From' then iterate
 if a = '' then iterate
 Parse var ems day'/'Month'/'Year' 'Hour':'Min':' '{EMSI}{' address '}{' '}' '{' '}' '{' '}' '{' '}' '{' Mailer '}' '{' Version '}' '{IDENT}' '[' ']' '[' ']' '[' Name ']'

   If datefrom \=  'ALL' then
    do
       If Year < YearFrom Then iterate l
       Else
         If Year = YearFrom & Month < MonthFrom Then iterate l
         Else
             If Year = YearFrom & Month = MonthFrom & Day < DayFrom Then iterate l
             Else
		If Year = YearFrom & Month = MonthFrom & Day = DayFrom & Hour < HourFrom Then iterate l
		Else If Year = YearFrom & Month = MonthFrom & Day = DayFrom & Hour = HourFrom & Min < MinFrom Then iterate l
       If Year > YearTo Then leave l
       Else
         If (Year = YearTo) & (Month > MonthTo) Then leave l
         Else
             If (Year = YearTo) & (Month = MonthTo) & (Day > DayTo) Then leave l
	     Else
		If (Year = YearTo) & (Month = MonthTo) & (Day = DayTo) & (Hour > HourTo) Then leave l
		Else
			If (Year = YearTo) & (Month = MonthTo) & (Day = DayTo) & (Hour = HourTo) & (Min > MinFrom) Then leave l
   end

 If fdate = 'FDATE' then fdate = day'/'Month'/'Year' 'Hour':'Min
 mailer = mailer'/'version
 parse value word(address,1) with address '@'
 parse var address '*' trash
 flag = 1
 do i = 1 to n 
  If address.i = address then 
    do
	flag = 0
	If mailer \= '/'then mailer.i = mailer
  	If name \= '' then name.i = name
   end
 end
 If flag = 1 & address  \= '' & trash = '' & address \= yours then do; n = n+1; address.n = address; mailer.n = mailer; name.n = name; end
end
edate = day'/'Month'/'Year' 'Hour':'Min



/** sorting **/
minnum = 1
do i = 1 to n
 min = '99999:9999/9999.99999'
 do j = i to n
  parse var min zone ':' region '/' node '.' point
  If point = '' then point = 0
  parse var address.j zone1 ':' region1 '/' node1 '.' point1
  If zone1 < zone then do; min = address.j; minnum = j; end
  else If (zone1 = zone) & (region1 < region) then do; min = address.j; minnum = j; end
       else If (zone1 = zone) & (region1 = region) & (node1 < node ) then do; min = address.j; minnum = j; end
	    else If (zone1 = zone) & (region1 = region) & (node1 = node ) & (point1 < point) then do; min = address.j; minnum = j; end
 end 
 temp = address.i
 address.i = address.minnum
 address.minnum = temp
 temp = mailer.i
 mailer.i = mailer.minnum
 mailer.minnum = temp
 temp = name.i
 name.i = name.minnum
 name.minnum = temp
end 

fdate = normaldate(fdate)
edate = normaldate(edate)

/** Out **/
 Call RxFuncAdd 'SysFileDelete', 'RexxUtil', 'SysFileDelete'
 Call SysFileDelete out

call lineout out , ''
call lineout out , centre(System' ('yours') had connects with following 'n' nodes',78)
call lineout out , centre('for the period from 'fdate' to 'edate,78)
call lineout out , ''
call lineout out , ' ษอออออออออออออออออัออออออออออออออออออออออออัออออออออออออออออออออออออออออออออป'
call lineout out , ' บ     Address     ณ        Sysop           ณ            Mailer              บ'
call lineout out , ' ฬอออออออออออออออออุออออออออออออออออออออออออุออออออออออออออออออออออออออออออออน'
do i = 1 to n
 parse var address.i node '.' point
 If point \= '' & node = yours & IsPoint = 'YES' then address.i = 'Point .'point
 if point = 0 then address.i = node

 flagok = 0
 name = ''
  do forever
    parse var name.i ab '\' a
    if a = '' then do
     name = name||ab
     leave
    end
    else flagok = 1
    b = substr(a,1,2)
    if datatype(b,x) \= 1 then 
      do 
        name = name||ab'\'
        name.i = a
        iterate
      end
    c = substr(a,3)
    b = d2c(x2d(b))
    name = name||ab||b
    name.i = c
  end
 if flagok = 1 then name.i = name

 parse var mailer.i mailer.i '/Noncommercial'
 If address.i \= yours then call lineout out , ' บ 'left(address.i,15)' ณ 'left(name.i,22)' ณ 'left(mailer.i,30)' บ '
end
call lineout out , ' ฬอออออออออออออออออฯออออออออออออออออออออออออฯออออออออออออออออออออออออออออออออน'
call lineout out , " บ     The Brake's EmsiLog analyzer (c) by Kirya Perevalov, 2:5010/183       บ"
call lineout out , ' บ                 Thanks to john for the best OS/2 mailer                   บ'
call lineout out , ' ศอออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออผ'


exit 0

NormalDate:
 parse arg date
 parse var date day '/' month '/' ' 'time
 select
   when Month = 1 then Month = 'Jan'
   when Month = 2 then Month = 'Feb'
   when Month = 3 then Month = 'Mar'
   when Month = 4 then Month = 'Apr'
   when Month = 5 then Month = 'May'
   when Month = 6 then Month = 'Jun'
   when Month = 7 then Month = 'Jul'
   when Month = 8 then Month = 'Aug'
   when Month = 9 then Month = 'Sep'
   when Month = 10 then Month = 'Oct'
   when Month = 11 then Month = 'Nov'
   when Month = 12 then Month = 'Dec'
 otherwise nop
 end
 a = day Month time
return a
