/* $VER: EZCronGUI v2.00 by Jim Hines. 1995-97 All Rights Reserved.
** EZCronGUI is The Graphic User Interface for EZCron, 1994-1997 Jim Hines.
** Thanks must go to Gene Heskett for his help in coding some functions.
** Internet email me at <hines_j@iolinc.net>
*/

	LF= '0a'x
	defaultevent = 'rx,ezcron:rexx/TalkingClock.rexx,hourly,daily,--/--/--,--/--/--,--:--,--:--,-,-,'

	options results

	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)

	if ~showlist( 'p', 'VAREXX' ) then do
   		address command 'run >nil: varexx'
		call delay(25)
		waitforport
		waitforport
	end

	address VAREXX
	call openport(EZCRONPREFS)
	load 'EZCron:EZC.gui EZCRONPREFS'
	host = result
	address value host
	show editwindow
	set startdate enddate disable

startup:
	if showlist('P', 'EZCROND') then do
		settext text '"Active"'
	end
	if ~showlist('P', 'EZCROND') then do
		settext text '"Inactive"'
	end

/* Read the con.prefs file for later reference */
	open(confile, 'EZCron:Prefs/con.prefs', 'R')
	convar = readln(confile)
	close(confile)

/* Create it if cron.config ~exists */
	if ~exists('s:cron.config') then do
		call message('Config file not found. Creating a new one.')
		open(cronfile, 's:cron.config', 'w')
		writeln(cronfile, defaultevent)
		close(cronfile)
	end

/* make sure it's not empty */
	if exists('s:cron.config') then do
		open(cronfile, 's:cron.config', 'R')
		if readln(cronfile) = '' then do
			call message('Your config contains no entries. Creating one.')
			writeln(cronfile, defaultevent)
		end
	close(cronfile)
	end
	drop defaultevent

	address command 'copy s:cron.config ram:t'

eventparse:
	address value host  /* this is so the correct window's buttons work after closing other windows */
	drop result ev et rng1 rng2 ed sd edt arg1 sfx txt

	do forever
		setlist selectevent clear
		config = 's:cron.config'	/* Name of file for reading events from */
		event. = 0
		
		if ~open(cronfile, config,'R') then do
			call message('Config file not found. Creating a new one.')
			call AC_New()
		end
		
		errors = 0
		do until eof(cronfile)
			linein = readln(cronfile)
			parse var linein line ';'
			next = event.0 + 1
	
	        parse var line event.next.command','event.next.pargs','event.next.time',',
			event.next.date','event.next.startdate','event.next.enddate',',
			event.next.rng1','event.next.rng2','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 */


			event.next.txt = strip(event.next.txt) /* This Gets rid of the leading & trailing spaces in the LAST variable
													  If new fields are added, the variable must be changed to reflect this */
	
			if line ~= '' then do  /* This keeps the commented out lines from appearing */
				setlist selectevent event.next.command
			end
		
			select
				when event.next.command = '' then iterate
				when event.next.command = '' then do
					iterate
				end
				otherwise event.0 = next
			end
		end
		settext eventcount event.0  /* for event counter */
		call close(cronfile)
		call waitforpkt()
	end

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

waitforpkt:
	do forever
		call waitpkt('EZCRONPREFS')    /* Wait for a message from Varexx */
		packet = getpkt('EZCRONPREFS')        /* Get the message */

	   	if packet ~= '00000000'x then do /* This is not a null message */
			call reply(packet, 0)  /* FOR AREXX MESSAGES */
			class  = getarg(packet)    /* Get the information about the message */
			type = word(class, 1)
			msg = substr(class, 12,100)

			if class = eventtype then call eventtype()
			if class = closewindow then call exiting()
			if class = prefsgad then call prefsgad()
			if class = edit then call edit()
			if class = testevent then call testevent()
			if class = AC_New then call AC_New()
			if class = arg1gad then call arggad()
			if class = sfxgad then call sfxgad()
			if class = datefields then call mydate()
			if class = timefields then call mytime()
			if class = purgegad then call purge()
			if class = indis then call indis()
			if class = update then call update()
			if class = addevent then call addevent()
			if class = addefault then call addefault()
			if class = delete_event then call deleteevent()
			if class = ac_toggle then call ac_toggle()
			if class = clockon then call clockon()
			if class = refresh then call refresh()
			if class = about then call about()
			if class = copy then call copy()
			if msg ~= '' & type = 'SELECTEVENT' then call selectevent()
		end
	end
	return

/* SUBROUTINES */

copy:
	address command 'copy s:cron.config s:cron.config.bak'
	return

about:
    call rtezrequest('EZCron v2.00'||LF||'1995-1997 Jim Hines'||LF||'EZCron is emailware.'||LF||'If you use it, please lemme know.','Alright','About EZCron','rtez_flags=ezreqf_centertext rt_reqpos = reqpos_centerscr',)
	return

eventtype:
	call rtezrequest("Please choose what type"||LF||"of  Event you would like.", "Program|Cancel|Reminder", ,)
	if rtresult == 1 then call getfile()
	if rtresult == 2 then call waitforpkt()
	if rtresult == 0 then call txtgad
	return

clockon:
	address command 'run <>nil: clock seconds digital format 2'
	return

AC_Toggle:
	if ~showlist('P', 'EZCROND') then do
		address command 'run <>nil: rx EZCron:EZCronD start'
	end	
	else
		if showlist('P', 'EZCROND') then do	
		address command 'run <>nil: rx EZCron:EZCronD stop'
		settext text '"Exiting..."'
	end
	return

edit:
	if ~exists('EZCron:prefs/txted.prefs') then address command 'ed s:cron.config'
	if exists('EZCron:prefs/txted.prefs') then do
		open(edconfig, 'EZCron:prefs/txted.prefs', 'r')
		linein = readln(edconfig)
		address command linein 's:cron.config'
		close(edconfig)
	end
	address command 'copy s:cron.config ram:t'
	call eventparse()

testevent:
	if exists('EZCron:prefs/sfx.prefs') then do
		open(sfxconfig, 'EZCron:prefs/sfx.prefs', 'r')
		sfxplayer = readln(sfxconfig)
		close(sfxconfig)
	end

	read event
	ev = RESULT
	if left(ev,1) = '!' then ev = substr(ev,2,256)
	
	read arg1
	arg1 = RESULT
	if arg1 = '-' then arg1 = ''
	
	read sfx
	fx = RESULT
	if fx = '-' then fx = ''
	
	read txtstr
	tx = result
				
	if ev ~= textreminder then do
		address command 'run' convar ev arg1
	end
	
	if fx ~= '' then do
		address command 'run >nil:' sfxplayer fx
	end
	
	if ev = TEXTREMINDER then do
		address command 'run >nil: rx >nil: ezcron:rexx/Reminder.rexx' tx
		if showlist(h,SPEAK) then do
			address command 'echo' '"'tx'"' '>speak:'
		end
	end
	drop result ev et ed arg1 rng1 rng2 sfx txt
	return	

selectevent:
	read selectevent NUMBER
	en = RESULT  /* en is the event number */

	settext event msg
	set arg1 arg1gad enable
	if msg = 'TEXTREMINDER' | msg = '!TEXTREMINDER' then do
		set arg1 arg1gad disable
	end
	a = 0
	if pos('##', event.en.date, 1) > 0 then do
		a = 1
	end
	settext event_time event.en.time
	settext event_date event.en.date
	settext rng1 event.en.rng1
	settext rng2 event.en.rng2
	if event.en.date = 'range' | event.en.date = 'daily' | a = 1 then do
		set startdate enddate enable
	end
	else do
		set startdate enddate disable
	end
	settext startdate event.en.startdate
	settext enddate event.en.enddate
	settext arg1 event.en.pargs
	settext rng1 event.en.rng1
	settext rng2 event.en.rng2
	settext sfx event.en.sfx
	settext txtstr event.en.txt
	drop a
	return

getfile:
	set arg1 arg1gad enable
	clock = time('N')
	clock2 = LEFT(clock,5)
	filename = rtfilerequest(, , "Pick a File To Run",OK)
	if filename = '' then call eventparse() /* Get out of routine */
	settext event filename
	settext event_time clock2
	settext rng1 '--:--'
	settext rng2 '--:--'
	settext event_date Date(USA)
	settext startdate '--/--/--'
	settext enddate '--/--/--'
	settext arg1 '-'
	settext sfx '-'
	settext txtstr '-'
	return

AC_New:
	if exists('EZCron:prefs/defev.prefs') then do
		open(prefs, 'EZCron:prefs/defev.prefs', 'r')
		defevent = readln(prefs)
		close(prefs)
	end
	if ~exists('s:cron.config') then do
		open(MkCfg, 's:cron.config',W)
		writeln(MkCfg, defevent)
		close(MkCfg)
	end
	else 
	call rtezrequest("A cron.config file already exists."||LF||"Do you wish to overwrite it?.", "_Yes|_No", , "rt_reqpos = reqpos_centerscr")
	if rtresult == 1 then do
		if exists('s:cron.config.old') then address command 'delete s:cron.config.old'
		address command 'rename s:cron.config s:cron.config.old'
		open(MkCfg, 's:cron.config','w')
		writeln(MkCfg, defevent)
		close(MkCfg)
	end
	address command 'copy s:cron.config ram:t'
	call eventparse()
	return
	
arggad:
	argname = rtfilerequest(, , "Pick a file argument",OK)
	if argname = '' then argname = '-'
	settext arg1 argname
	return

sfxgad:
	read sfx
	sfxtxt = result
	parse var sfxtxt device ':'
	if device = '-' then device = 'sys'
	argname = rtfilerequest(device':',, "Pick a soundfile",OK,)
	if argname = '' then argname = '-'
	settext sfx argname
	return	

txtgad:
	read txtstr
	txt = result
	if txt = '-' then txt = '' 
	argname = rtgetstring(txt,'Enter Reminder String', 'Reminder Requester', ,)
	if argname = '' then call eventparse()
	clock = time('N')
	clock2 = LEFT(clock,5)
	settext txtstr argname
	settext event TextReminder
	settext event_time clock2
	settext event_date Date(USA)
	settext startdate '--/--/--'
	settext enddate '--/--/--'
	set arg1 startdate enddate disable
	settext arg1 '-'
	settext sfx '-'
	drop txt
	return

prefsgad:
	call openport(PREFSWINPORT)
	spawn PREFSWINPORT
	host2 = result
	address value host2
	show prefswindow
	call prefs()
	return

mydate:
	call openport(DATEWINPORT)
	spawn DATEWINPORT
	host3 = result
	address value host3
	show datewindow
	call datefields()
	return

mytime:
	call openport(TIMEWINPORT)
	spawn TIMEWINPORT
	host4 = result
	address value host4
	show timewindow
	call timefields()
	return

purge:
	address command 'copy s:cron.config s:cron.config.old'
	address command 'delete s:cron.config'
	open(dfile, 's:cron.config', 'W') /* create it again*/
	close(dfile)

	do i = 1 to event.0
		if event.i.date = 'range' then event.i.date = event.i.enddate
		parse var event.i.date month '/' day '/' year
					
		if day ~= '' & if year ~= '' then do 
			sortdate = '19'year''month''day
			eventdate = date('c',sortdate,'s')        /* number of days since..*/
			currentdate = date(s) /* 19951221 */
			sortcurrent = date('c', currentdate, 's') /* number of days since..*/
			a = sortcurrent-eventdate
			if a > 0 then event.i.command = delete  /* this are the files you DONT want to write */
		end

		if event.i.command ~= delete then do
			open(dfile, 's:cron.config', 'a')
			writeln(dfile, event.i.command','event.i.pargs','event.i.time','event.i.date','event.i.startdate','event.i.enddate','event.i.rng1','event.i.rng2','event.i.sfx','event.i.txt',')
			drop result
		end
		close(dfile)
		address command 'copy s:cron.config ram:t'
		drop a
	end
	call eventparse()
	return
	
indis: /* Enable/Disable event */
	read event
	ev = result

	if left(ev,1) = '!' then do
		ev = substr(ev,2,256) /* start with 2nd char and go 256 deep */
		settext event ev
		class = update
	end
	else do
		settext event '!'ev
		class = update
	end
	return

update: /* Modify */
	address command 'copy s:cron.config s:cron.config.old'
	address command 'delete >NIL: s:cron.config'
	open(dfile, 's:cron.config', 'W') /* create it again*/
	close(dfile)

	read event
	ev = result
	if ev = '' then call message('No Event Field.')
	if ev = '' then call eventparse()
	if ev = 'None Selected' then call message('No Event Field.')
	if ev = 'None Selected' then call eventparse()
	read event_time
	et = result
	if et = '' then	call message('No Time Field.')
	read event_date
	ed = result
	if ed = '' then call message('No Date Field.')
	read startdate
	sd = result
	if sd = '' then sd = '--/--/--'
	read enddate
	edt = result
	if edt = '' then edt = '--/--/--'
	read arg1
	arg = result
	if arg = '' then arg = '-'
	read rng1
	trng1 = result
	if trng1 = '' then trng1 = '--:--'
	read rng2
	trng2 = result
	if trng2 = '' then trng2 = '--:--'
	read sfx
	fx = result
	if fx = '' then fx = '-'
	read txtstr
	rem = result
	if rem = '' then rem = '-'	

	read selectevent var h
	open(dfile, 's:cron.config', 'a')
	do i = 1 to h.count
		if en ~= i then do
			writeln(dfile, event.i.command','event.i.pargs','event.i.time','event.i.date','event.i.startdate','event.i.enddate','event.i.rng1','event.i.rng2','event.i.sfx','event.i.txt',')
			drop result
		end
		else do
			writeln(dfile, ev','arg','et','ed','sd','edt','trng1','trng2','fx','rem',')
		end
	end
	close(dfile)
	drop result ev et ed sd edt arg1 rng1 rng2 sfx txt
	address command 'copy s:cron.config ram:t'
	call eventparse()

addevent:
	read event
	ev = result
	if ev = '' then call message('No Event Field.')
	if ev = 'None Selected' then call message('No Event Field.')
				
	read event_time
	et = result
	if et = '' then	call message('No Time Field.')
	
	read event_date
	ed = result
	if ed = '' then call message('No Date Field.')

	read startdate
	sd = result
	if sd = '' then sd = '--/--/--'

	read enddate
	edt = result
	if edt = '' then edt = '--/--/--'
		
	read arg1
	arg1 = result
	if arg1 = '' then arg1 = '-'

	read rng1
	rng1 = result
	if rng1 = '' then rng1 = '--:--'

	read rng2
	rng2 = result
	if rng2 = '' then rng2 = '--:--'
	
	read sfx
	sfx = result
	if sfx = '' then sfx = '-'
	
	read txtstr
	txt = result
	if txt = '' then txt = '-'
				
	if ev = '' then call eventparse()
	if ev = 'None Selected' then call eventparse()
	if et = '' then call eventparse()
	if ed = '' then call eventparse()

	open(dfile, 's:cron.config', 'a')
	writeln(dfile, ev','arg1','et','ed','sd','edt','rng1','rng2','sfx','txt',')
	close(dfile)
	drop result ev et ed sd edt arg1 rng1 rng2 sfx txt
	address command 'copy s:cron.config ram:t'
	call eventparse()
			
addefault:
	read event
	ev = result
	if ev = '' then call message('No Event Field.')
	if ev = 'None Selected' then call message('No Event Field.')
	if ev = '' then call eventparse()
	if ev = 'None Selected' then call eventparse()
	if et = '' then call eventparse()
	if ed = '' then call eventparse()

	address command 'delete ezcron:prefs/defev.prefs'
	open(dfile, 'ezcron:prefs/defev.prefs', 'w')
	writeln(dfile, ev','event.en.pargs','event.en.time','event.en.date','event.en.startdate','event.en.enddate','event.en.rng1','event.en.rng2','event.en.sfx','event.en.txt',')
	close(dfile)
	drop result ev 
	call eventparse()
	return

deleteevent:
	address command 'copy s:cron.config s:cron.config.old'
	address command 'delete >NIL: s:cron.config'
	open(dfile, 's:cron.config', 'W') /* create it again*/
	close(dfile)

	read selectevent var h
	open(dfile, 's:cron.config', 'a')
	do n = 1 to h.count
		if en ~= n then do
			writeln(dfile, h.n','event.n.pargs','event.n.time','event.n.date','event.n.startdate','event.n.enddate','event.n.rng1','event.n.rng2','event.n.sfx','event.n.txt',')
		end
	end
	close(dfile)
	address command 'copy s:cron.config ram:t'
	
	settext event 'None Selected'
	settext event_time 'NA'
	settext event_date 'NA'
	settext startdate '--/--/--'
	settext enddate '--/--/--'
	settext arg1 '-'
	settext rng1 '--:--'
	settext rng2 '--:--'
	settext sfx '-'
	settext txtstr '-'
	call eventparse()

refresh:
	if showlist('P', 'EZCROND') then do
		settext text '"Active"'
	end
	if ~showlist('P', 'EZCROND') then do
		settext text '"Inactive"'
	end

	call eventparse()
	return

prefs:
	do forever
		call waitpkt( "PREFSWINPORT" )    /* Wait for a message from Varexx */
		packet = getpkt( "PREFSWINPORT" )        /* Get the message */
	
		if packet ~= '00000000'x then do /* This is not a null message */
			class  = getarg(packet)    /* Get the information about the message */
		   	type = word(class, 1)
			msg = substr(class, 11,25)

			if class = 'CLOSEWINDOW PrefsWindow' then do
				hide
				address value host
				call waitforpkt()
			end

			if class = texteditor then do
				editor = rtfilerequest(, , "Pick a text editor",OK, "rt_reqpos = reqpos_centerscr")
				if rtresult == 0 then call prefs() /*This get out of the edprefs function */
				open(edconfig,'EZCron:prefs/txted.prefs', 'w')
				writeln(edconfig, editor)
				close(edconfig)
				drop editor
			end
				
			if class = sfxplayer then do
				player = rtfilerequest(, , "Pick a sound player",OK, "rt_reqpos = reqpos_centerscr")
				if rtresult == 0 then call prefs() /*This get out of the edprefs function */
				open(edconfig,'EZCron:prefs/sfx.prefs', 'w')
				writeln(edconfig, player)
				close(edconfig)
				drop player
			end

			if class = conselect then do
				console = rtgetstring(convar,'Enter Output String', 'Test Output Requester', ,)
				if console ~= '' then convar = console
				if rtresult == 0 then call prefs() /*This get out of the edprefs function */
				open(conconfig,'EZCron:prefs/con.prefs', 'w')
				writeln(conconfig, console)
				close(conconfig)
				drop console
			end
		call prefs()
		end	
	end

datefields:
	do forever
		call waitpkt( "DATEWINPORT" )    /* Wait for a message from Varexx */
		packet = getpkt( "DATEWINPORT" )        /* Get the message */
	
		if packet ~= '00000000'x then do /* This is not a null message */
			class  = getarg(packet)    /* Get the information about the message */
		   	type = word(class, 1)
			msg = substr(class, 11,25)

			if class = once then field = Date(USA)
			if class = daily then call daterange('daily')
			if class = monthly then field = 'Monthly-'right(date(S),2)  /* Monthly-13 */ 
			if class = sunday then field = 'Sunday'
			if class = monday then field = 'Monday'
			if class = tuesday then field = 'Tuesday'
			if class = wednesday then field = 'Wednesday'
			if class = thursday then field = 'Thursday'
			if class = friday then field = 'Friday'
			if class = saturday then field = 'Saturday'
			if class = sunday then field = 'Sunday'
			if class = weekdays then field = 'Weekdays'
			if class = weekends then field = 'Weekends'
			if class = every_nn_dd then field = 'Every_3_Fri'
			if class = dspecial then call daterange('##/##/##')
			if class = 'CLOSEWINDOW DateWindow' then do
				hide
				address value host
				call waitforpkt()
				return
			end
			if field ~= '' then call fillindate()
			return
		end
	end

daterange:
	hide
	address value host
	settext event_date arg(1)
	set startdate enddate enable
	settext startdate '--/--/--'	
	settext enddate '--/--/--'
	call waitforpkt()
	return
	
timefields:
	do forever
		call waitpkt( "TIMEWINPORT" )    /* Wait for a message from Varexx */
		packet = getpkt( "TIMEWINPORT" )        /* Get the message */
	
		if packet ~= '00000000'x then do /* This is not a null message */
			class  = getarg(packet)    /* Get the information about the message */
		   	type = word(class, 1)
			msg = substr(class, 11,25)

			if class = timeonce then field = LEFT(time('N'),5)
			if class = minute then field = 'Minute'
			if class = five_min then field = 'Five_Min'
			if class = ten_min then field = 'Ten_Min'
			if class = quarterly then field = 'Quarterly'
			if class = thirty_min then field = 'Thirty_Min'
			if class = hourly then field = 'Hourly'
			if class = shour then field = '##:'substr(time('N'),4,2)
			if class = 'CLOSEWINDOW TimeWindow' then do
				hide
				address value host
				call waitforpkt()
				return
			end
			if field ~= '' then call fillintime()
			return
		end	
	end

fillindate:
	hide
	address value host
	settext event_date field
	settext startdate '--/--/--'
	settext enddate '--/--/--'
	set startdate enddate disable
	call waitforpkt()
	return

fillintime:
	hide
	address value host
	settext event_time field
	settext rng1 '--:--'
	settext rng2 '--:--'
	call waitforpkt()
	return

helpmessage: procedure

    call rtezrequest(arg(1),'Okay!','EZCron Online Help','rtez_flags=ezreqf_centertext',)
	return

message: procedure

    call rtezrequest(arg(1),'Okay!','EZCron ERROR','rt_reqpos = reqpos_centerscr rtez_flags=ezreqf_centertext',)
	return

exiting:
	'hide unload'  /* Hide the window and unload the gui file from memory */
	call closeport("EZCRONPREFS")  /* Close the port */
	address command 'run >nil: vxc'
	address command 'run >nil: vxc'
	address command 'run >nil: vxc'
	address command 'run >nil: avail flush'	
	exit

