/************************************************************/
/*            AmiGate accounting generator                  */
/*          $VER: AmiStat.rexx 0.1ß (10.2.94)               */
/*                                                          */
/* This program analyzes the AmiGate logfile and generates  */
/* accounting information.                                  */
/*                                                          */
/* Example: rx AmiStat.rexx 24.12.93 24.02.94               */
/* or just: rx AmiStat.rexx                                 */
/*                                                          */
/* Author: Brian Jacobsen   FidoNet: 2:236/19.73            */
/*                          Email: brj@boble.ping.dk        */
/************************************************************/
signal on syntax
options results
trace off

nodes. = 0
received. = 0
sent. = 0
nodecounter = 0
StartDate = ''
StopDate = ''
Complete = 0

PARSE ARG argstartdate argstopdate

logfile = 'Logs:AmiGateAcc.log'							/* Logfile to examine */

IF argstartdate ~= '' THEN
	DO
		PARSE VAR argstartdate day '.' month '.' year
		day = RIGHT(day,2,'0')
		month = RIGHT(month,2,'0')
		if LENGTH(year) = 2 THEN year = '19' || year
		StartDate = year||month||day
	END

IF argstopdate ~= '' THEN
	DO
		PARSE VAR argstopdate day '.' month '.' year
		day = RIGHT(day,2,'0')
		month = RIGHT(month,2,'0')
		if LENGTH(year) = 2 THEN year = '19' || year
		StopDate = year||month||day
	END

IF StartDate = '' | StopDate = '' THEN
	Complete = 1

IF open('loghandle',logfile,'Read') ~= 1 THEN
	DO
		say 'Open logfile:' logfile 'failed.'
		SIGNAL exit
	END

inline = READLN('loghandle')

DO WHILE ~EOF('loghandle')
	IF LEFT(inline,1) = '$' THEN
		DO
			PARSE VAR inline . date '-' month '-' year logtime direction ' # ' node ' # ' size .

			date=STRIP(date,'B')
			year = '19' || year
			nummonth = monthtonum(month)
			logdate = year || nummonth || date

			IF Complete | (logdate >= StartDate & logdate <= StopDate) THEN
				DO
					IF sent.node = 0 & received.node = 0 THEN		/* Add him to the list */
						DO
							nodecounter = nodecounter + 1
							nodes.0 = nodecounter
							nodes.nodecounter = node
						END

					IF direction = 'F->U:' THEN
						sent.node = sent.node + size
					ELSE
						received.node = received.node + size
				END
		END

	inline = READLN('loghandle')
END

CALL CLOSE('loghandle')

CALL nodesort 1,nodes.0										/* Sort nodes */

SAY 'Dear Sysop'
SAY ''
SAY 'Here are the results from your log-request. Generated' DATE()
SAY ''
IF Complete = 0 THEN
	DO
		DispStartDate = DATE('N',StartDate,'S')		/* For display in stats  */
		DispStopDate = DATE('N',StopDate,'S')
		SAY 'Covering the period from' DispStartDate 'to' DispStopDate
		SAY ''
	END

SAY '+-----------------+---------+---------+'
SAY '| Node            | Kb sent | Kb rec. |'
SAY '+-----------------+---------+---------+'
DO i = 1 TO nodecounter
	name = nodes.i
	kbsent = sent.name / 1024
	Remainder = kbsent // 1
	kbsent = kbsent % 1
	IF Remainder > 0.5 THEN kbsent = kbsent + 1

	kbreceived = received.name / 1024
	Remainder = kbreceived // 1
	kbreceived = kbreceived % 1
	IF Remainder > 0.5 THEN kbreceived = kbreceived + 1

	SAY '|' LEFT(name,15) '|' RIGHT(kbsent,7) '|' RIGHT(kbreceived,7) '|'
	SAY '+-----------------+---------+---------+'
END

SIGNAL exit

syntax:
say rc errortext(rc) 'in line' SIGL
exit:
EXIT

/***************************************/
/* Sort-routines. QuickSort algoritm   */
/***************************************/
nodesort: PROCEDURE EXPOSE nodes.
ARG Left,Right
i = Left; j = Right
m = (Left + Right) % 2
tempm = nodes.m
DO UNTIL (i > j)
	DO WHILE (nodes.i < tempm & i < Right)
		i = i + 1
	END

	DO WHILE (nodes.j > tempm & j > Left)
		j = j - 1
	END
	IF i <= j THEN
		DO
			Temp = nodes.i
			nodes.i = nodes.j
			nodes.j = Temp
			i = i + 1
			j = j - 1
		END
END
IF Left < j  THEN CALL nodesort Left,j
IF i < Right THEN CALL nodesort i,Right
RETURN

monthtonum: PROCEDURE
ARG month
SELECT
	WHEN month = 'JAN' THEN nummonth = '01'
	WHEN month = 'FEB' THEN nummonth = '02'
	WHEN month = 'MAR' THEN nummonth = '03'
	WHEN month = 'APR' THEN nummonth = '04'
	WHEN month = 'MAY' THEN nummonth = '05'
	WHEN month = 'JUN' THEN nummonth = '06'
	WHEN month = 'JUL' THEN nummonth = '07'
	WHEN month = 'AUG' THEN nummonth = '08'
	WHEN month = 'SEP' THEN nummonth = '09'
	WHEN month = 'OCT' THEN nummonth = '10'
	WHEN month = 'NOV' THEN nummonth = '11'
	WHEN month = 'DEC' THEN nummonth = '12'
	OTHERWISE nummonth = '00'
END

RETURN(nummonth)
