/* TITLE: avm:rexx/addentry.avm */
parse arg mailbox .
call addlib("rexxsupport.library", 0, -30, 0)
call initLogEntry(); handle = makeUniqueFile()
log.fileName = handle; log.type = 'Unknown'; log.returnRetry = 0
log.returnInterval = 5; log.altFileName = voiceFile('Anonymous', 'Introduction')
log.returnSendFunc = 'DefaultSender'; log.origMailbox = 'Anonymous'
call saveLogEntry(mailbox, handle)
address command 'run <nil: >nil: avm:schedulerview' mailbox handle

error:
exit

exit

mailboxDir: procedure
	parse arg mailbox

	return 'avmmbox:' || mailbox || '/'

logFile: procedure
	parse arg mailbox, magiccookie

	return 'avmmbox:' || mailbox || '/logs/' || magiccookie

voiceFile: procedure
	parse arg mailbox, magiccookie

        if (verify(magiccookie, '/:', 'M') = 0) then
          return 'avmmbox:' || mailbox || '/voices/' || magiccookie
        else
          return magiccookie

makeUniqueFile: procedure
	if arg() ~= 0 then do
		rc = "makeUniqueFile: bad args"
		signal error
	end
	return address() || '.' || date('i') || '.' || time('s') || '.' || random(1, 999, time('s'))

convertToDate: procedure
	if arg() ~= 1 then do
		rc = "convertToDate: bad args"
		signal error
	end
	parse arg timeInC

	actualTime = (timeInC - (2922)*86400) // (86400)
	actualDate = (timeInC - actualTime - 2922*86400) % 86400

	return actualDate /* returning it in 'internal' format */

convertToTime: procedure
	if arg() ~= 1 then do
		rc = "convertToTime: bad args"
		signal error
	end
	parse arg timeInC
	
	actualTime = (timeInC - (2922)*86400) // (86400)

	return actualTime

cTime: procedure
	/* 2922 = 8*365 + 2 */
	/* 86400 = 24*60*60 */
	return (date('i')+2922)*86400 + time('s')

/* this returns a handle that you must use after initializing log. */
initLogEntry: procedure expose log.
	if arg() ~= 0 then do
		rc = "initLogEntry: bad args"
		signal error
	end
	
	drop log.
        log. = ''

        acidname = getclip(address() || 'CIDNAME')
        acidnumber = getclip(address() || 'CIDNUMBER')

	/* 2922 = 8*365 + 2 */
	/* 86400 = 24*60*60 */
	log.time = (date('i')+2922)*86400 + time('s')
	log.cidname = acidname
	log.cidnumber = acidnumber

	return

loadLogEntry: procedure expose log.
	if arg() ~= 2 then do
		rc = "loadLogEntry: bad args"
		signal error
	end
	parse arg mailbox, handle

        drop log.
        log. = ''

	if ~exists(mailboxDir(mailbox)) then do
		call showDebugger('Dir=' || mailboxDir(mailbox) 'does not exist')
		return
	end

	opened = open(handle, logFile(mailbox, handle), 'r')
        if opened then do
		call showDebugger('Loading entry' logFile(mailbox, handle))
		do while ~eof(handle)
			line = readln(handle)
			parse upper var line variable '=' value
			log.variable = value
		end
		call close(handle)
	end; else call showDebugger('Could not load' logFile(mailbox, handle))
	return

/* pass a handle here */
saveLogEntry: procedure expose log.
	if arg() ~= 2 then do
		rc = "saveLogEntry: bad args"
		signal error
	end
	parse arg mailbox, handle

	if ~exists(mailboxDir(mailbox)) then do
		call showDebugger('Dir=' || mailboxDir(mailbox) 'does not exist')
		return
	end

	opened = open(handle, logFile(mailbox, handle), 'w')

	if opened then do
		call showDebugger('Saving entry' logFile(mailbox, handle))
		call writeln(handle, 'TYPE=' || log.type)
		call writeln(handle, 'TIME=' || log.time)
		call writeln(handle, 'LENGTH=' || log.length)

		call writeln(handle, 'ORIGMAILBOX=' || log.origmailbox)

		call writeln(handle, 'CIDNAME=' || log.cidname)
		call writeln(handle, 'CIDNUMBER=' || log.cidnumber)

		call writeln(handle, 'COMMENT=' || log.comment)

		call writeln(handle, 'FILENAME=' || log.filename)
		call writeln(handle, 'ALTFILENAME=' || log.altfilename)

		call writeln(handle, 'RETURNNUMBER=' || log.returnnumber)
		call writeln(handle, 'RETURNSENDFUNC=' || log.returnsendfunc)
		call writeln(handle, 'RETURNSTATUS=' || log.returnstatus)

		call writeln(handle, 'RETURNRETRY=' || log.returnretry)
		call writeln(handle, 'RETURNINTERVAL=' || log.returninterval)

		call close(handle)
		address rexx 'broadcast' 'addtomailbox' mailbox handle
	end; else call showDebugger('Could not save' logFile(mailbox, handle))

	return

/* pass a handle here */
updateLogEntry: procedure expose log.
	if arg() ~= 2 then do
		rc = "updateLogEntry: bad args"
		signal error
	end
	parse arg mailbox, handle

	if ~exists(mailboxDir(mailbox)) then do
		call showDebugger('Dir=' || mailboxDir(mailbox) 'does not exist')
		return
	end

	if ~exists(logFile(mailbox, handle)) then do
		call showDebugger('Unable to update non-existent' logFile(mailbox, handle))
		return
	end
	opened = open(handle, logFile(mailbox, handle), 'w')

	if opened then do
		call showDebugger('Updating entry' logFile(mailbox, handle))
		call writeln(handle, 'TYPE=' || log.type)
		call writeln(handle, 'TIME=' || log.time)
		call writeln(handle, 'LENGTH=' || log.length)

		call writeln(handle, 'ORIGMAILBOX=' || log.origmailbox)

		call writeln(handle, 'CIDNAME=' || log.cidname)
		call writeln(handle, 'CIDNUMBER=' || log.cidnumber)

		call writeln(handle, 'COMMENT=' || log.comment)

		call writeln(handle, 'FILENAME=' || log.filename)
		call writeln(handle, 'ALTFILENAME=' || log.altfilename)

		call writeln(handle, 'RETURNNUMBER=' || log.returnnumber)
		call writeln(handle, 'RETURNSENDFUNC=' || log.returnsendfunc)
		call writeln(handle, 'RETURNSTATUS=' || log.returnstatus)

		call writeln(handle, 'RETURNRETRY=' || log.returnretry)
		call writeln(handle, 'RETURNINTERVAL=' || log.returninterval)

		call close(handle)
		address rexx 'broadcast' 'refreshmailboxentry' mailbox handle
	end; else call showDebugger('Could not update' logFile(mailbox, handle))

	return



showDebugger: procedure
	if arg() ~= 1 then do
		say "showDebugger: ERROR"
		exit 20
	end

	parse arg debuggerInfo
	
	firstLine = sourceline(1)
	parse var firstLine '/*' 'TITLE:' title '*/'
	if showlist('p', 'AVMUSERINTERFACE') then
		address 'AVMUSERINTERFACE' 'addlog' title ':' debuggerInfo
	else
		say title ':' debuggerInfo

	return 

/*-----------------------------------------------------------------------*/
/*                         signal processing                             */

arexxerror:
error:
	call showDebugger("Error" rc "at line" sigl)
	exit 20

break_c:
halt:
	call showDebugger("Halt/Break_C at line" sigl)
	exit 20

novalue:
	call showDebugger("No value at line" sigl)
	exit 20

syntax:
	call showDebugger("Syntax error" rc "at line" sigl)
	exit 20

