/* ACron by Jim Hines 1994-1995 All Rights Reserved
** 10-23-95 Added requester to verify making a NEW cron.config file. See newrtn:
** 10-25-95 Fixed Bug in the above and added renaming of the old config file. See newrtn:
** 11-30-95 added the text string reminder support and the sfx functions
** 12-05-95 Fixed case sensitivity problem with weekly, monthly & Daily routeens 
** 12-13-95 Added Speech Function
*/
/* call trace(all) */


/* ======== Envirorment Varables == */
	clock = time('N')
	clock2 = LEFT(clock,5)
	date = Date(USA)
	date2 = Date(S)             /*     -> 19920804  */
	date3 = Date(W, date2, 'S')  /*    -> Friday  */
	sec = right(clock, 2)  /* Get Seconds */
	sec2 = 60 - sec        /* 61 (seconds) minus sec) */
	delayvar = sec2 * 50   /* sec2 x 50 ticks or 1 second sets the delay time */
	LT = delayvar / 50
	LTime = 60 - LT

/* ======== CHARACTOR TYPE VARABLES == */
	csi='9b'x
	Ital=csi'3m'
	bold=csi'1m'
	norm=csi'0m'
	black=csi'31m'
	white=csi'32m'
	blue=csi'33m'
	LF = '0a'x


/* ========END VARABLES == */

	arg command lg .

	if ~show('L','rexxsupport.library') then
		call addlib('rexxsupport.library',0,-30)
	if ~show('L','rexxreqtools.library') then
	        call addlib("rexxreqtools.library", 0, -30, 0)

/* ======== THIS IS THE INPUT PARSING / USAGE SECTION WITH A SUB-ROUTINE== */

	select
		when command = 'START' then do
			call startrtn()
		end
		when command = "STOP" then do
			call stoprtn()
		end
		when command = "QUIT" then do
			command = "STOP"
			call stoprtn()
		end
		otherwise
			say ''
			say bold'ERROR,'norm 'Unknown option'blue command norm
			call nulinp()
		end
	end
	
stoprtn:
	if command = "STOP" then do
		if showlist('P', 'ACRON_REXX') then do
			say ital 'Exiting ACron: Please Wait. Program will exit in' LT 'seconds!' norm
			address 'ACRON_REXX' STOP
			exit
		end
	else
		say bold ital 'ACron is not running.' norm
		exit
		end

/* =======LOOP== */
startrtn:
	if ~exists('s:cron.config') then do
		call rtezrequest('s:cron.config does NOT exist', "Okay", , "rt_reqpos = reqpos_centerscr")
		exit
	end

	RC = showlist('P', 'ACRON_REXX')
	if RC = 1 then do
		say bold ital white 'ACron is Already Active' norm
	exit
	end

	openport('ACRON_REXX')
	SIGNAL on HALT

/* =======PARSE CONFIG FILE SECTION == */
	do forever
	config = 's:cron.config'		/* Name of file for reading events from */
	event. = 0

	if ~open(cronfile, config,'READ') then do
		exit 20
	end

	errors = 0

		do until eof(cronfile)
			/* Grab the line, parse the event and ignore the comments */
			linein = readln(cronfile)
			parse var linein line '#'
	
			next = event.0 + 1
		
	        parse var line event.next.command event.next.pargs1 event.next.pargs2,
			event.next.pargs3 event.next.pargs4 event.next.time	event.next.date,
			event.next.sfx event.next.txt

			event.next.date = translate(event.next.date, 'abcdefghijklmnopqrstuvwxyz',,
							'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
							/* THE ABOVE TRANSLATES THE DATE TO LOWERCASE IF NOT NUMERIC */

			event.next.time = translate(event.next.time, 'abcdefghijklmnopqrstuvwxyz',,
							'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
							/* THE ABOVE TRANSLATES THE TIME TO LOWERCASE IF NOT NUMERIC */

			
	
			select
				when event.next.command = "" then iterate
				when event.next.command = "" then do
					say "Not enough fields in:" linein
					errors =  1
					iterate
				end
				otherwise event.0 = next
			end
		end
		call close(cronfile)

/* ======== END OF PARSE SECTION == */

		sec = right(clock, 2)  /* Get Seconds */
		sec2 = 60 - sec        /* 60 (seconds) minus sec) */
		delayvar = sec2 * 50   /* sec2 x 50 ticks or 1 second sets the delay time */
		LT = delayvar / 50
		LTime = 60 - LT

/* ======== AREXX PORT STUFF == */
		pkt = getpkt('ACRON_REXX')
			if pkt ~= '0000 0000'x then do
				call aport(pkt)
			end

		call delay(delayvar)    /* 3000 would be equal to 1 minute */
	
		DO i = 1 to event.0
			clock = time('N')
			clock2 = LEFT(clock,5)
			date = Date(USA)               /*  -> 08/04/92  */
			date2 = Date(S)                /*  -> 19920804   only used in next function */
			date3 = Date(W, date2, 'S')    /*  -> Friday    */
	
	/* ======= Arguments Routine == */
			if event.i.pargs1 = '-' then
				event.i.pargs1 = ''
			if event.i.pargs2 = '-' then
				event.i.pargs2 = ''
			if event.i.pargs3 = '-' then
				event.i.pargs3 = ''
			if event.i.pargs4 = '-' then
				event.i.pargs4 = ''
	
	/* ======= Misc Routines == */
			if event.i.sfx = '-' then
				event.i.sfx = ''
			if event.i.txt = '-' then
				event.i.txt = ''
	
	/* ======= Every Minute Routine == */
			if event.i.time = 'minute'
				then event.i.time = clock2
	
	/* ======= Every Hour Routine == */
			parse var clock2 hrs ':' min
			if min = 00 & event.i.time = 'hourly' then event.i.time = clock2
	
	/* ======= Daily Routine == */
			if event.i.date = 'daily'
				then event.i.date = date
	
	/* ======= Weekly Routine == */
	/* ======= Note that case IS significant */

			if event.i.date = 'sunday' & date3 = 'Sunday'
				then event.i.date = date
			if event.i.date = 'monday' & date3 = 'Monday'
				then event.i.date = date
			if event.i.date = 'tuesday' & date3 = 'Tuesday'
				then event.i.date = date
			if event.i.date = 'wednesday' & date3 = 'Wednesday'
				then event.i.date = date
			if event.i.date = 'thursday' & date3 = 'Thursday'
				then event.i.date = date
			if event.i.date = 'friday' & date3 = 'Friday'
				then event.i.date = date
			if event.i.date = 'saturday' & date3 = 'Saturday'
				then event.i.date = date
	
	/* ======= Monthly Routine == */
			month.event = event.i.date
			parse var month.event mth '-' day2
			if mth = 'monthly' then do
				parse var date month '/' day '/' year
				if day2 = day & event.i.time = clock2 then event.i.date = date
			end
	
	/* ======== Logging Routine == */
			if lg = log then do
		       	if event.i.time = clock2 & event.i.date = date then do
					if ~exists('s:cronlog') then do
						open(log, 's:cronlog', 'W')
					end
				else
					open(log, 's:cronlog', 'A')
					writeln(log, event.i.command "was executed at" event.i.time "on" event.i.date)
					close(log)
				end
			end
	
	/* ======= Final Routine == */
			if lg = log then delay(25) /* This delay is to allow for the log file
	        		                      to be deleted automatically if so desired */
	       	if event.i.time = clock2 & event.i.date = date then
			address command 'run >NIL:' event.i.command event.i.pargs1,
			event.i.pargs2 event.i.pargs3 event.i.pargs4
	
			if event.i.time = clock2 & event.i.date = date & event.i.sfx ~= "" then do
				if exists('EZCron:prefs/sfx.prefs') then do
					open(sfxconfig, 'EZCron:prefs/sfx.prefs', 'r')
					sfxplayer = readln(sfxconfig)
					close(sfxconfig)
					address command 'run >nil:' sfxplayer event.i.sfx
				end
			end
	
			if event.i.time = clock2 & event.i.date = date & event.i.txt ~= "" then do
				address command 'run >nil: rx >nil: ezcron:rexx/Reminder.rexx' event.i.txt /*This calls the external rexx proggy for the event display */
				if showlist(h,SPEAK) then do
						address command 'echo' event.i.txt '>speak:'
				end
			end

			if event.i.time = clock2 & event.i.date = date
			then event.i.date = 'Finished with Event'
		end
	end

/* ======== CLEANUP == */
	if event.0 = 0 then exit
	options failat 300
	trace 'Off'

/* ======== Arexx Port Message Check == */
aport:
	Cmd = getarg(Pkt)
	if Cmd = 'STOP' then do
        call reply(Pkt, rc)
	exit
	end
