/* FaxScript For GPFax And ZVT					*/
/* Start/Listen GPFax, Recieve Fax, Exit/Unlisten GPFax		*/

options results

signal on break_c
signal on halt
signal on syntax

options failat 50

say "FaxScript For GPFax And ZVT"

/*
 * Check if GPFax is already there
 */

ADDRESS COMMAND "WFP REXX_GPFAX 0"

if rc>0 then
	do
		/*
		 * No GPFax here. Start and wait for it.
		 */

		ADDRESS COMMAND "run >nil: <nil: GPFax:GPFax -z -W -NP -Q"
		ADDRESS COMMAND "WFP REXX_GPFAX 30"
		if rc>0 then
		do
			/*
			 * GPFax didn't come up for a reason.
			 */

			call setVars(rc)
			exit
		end

		/*
		 * Start fax handshaking and receive fax, then
		 * shut down GPFax
		 */ 

		ADDRESS rexx_gpfax recfax
		call setVars(rc)
		ADDRESS rexx_gpfax quitfax
    end
else
	do
		/*
		 * Wake up GPFax, start fax handshaking and receive fax,
		 * then put GPFax to sleep mode again.
		 */

		ADDRESS rexx_gpfax openserial
		ADDRESS rexx_gpfax recfax
		call setVars(rc)
		ADDRESS rexx_gpfax closeserial
	end
exit

/*
 * Set ZVT logfile entry according to GPFax' status report
 */

setVars: procedure
	parse arg error

	if error=0 then
		do
		address rexx_gpfax 'reportlog' 6
		address zvt.1 'setlength ' || result
		address rexx_gpfax 'reportlog' 1
		address zvt.1 'setname ' || result
		end
	else		/* No fax here. */
		do
		address rexx_gpfax 'reportlog' 6
		address zvt.1 'setlength ' || 0
		address zvt.1 'setname no_file'
		end
	return rc


error:
	call Debug("Error" rc "at line" sigl)
	return rc

break_c:
halt:
	call Debug("CTRL_C at line" sigl)
	exit 20

syntax:
	call Debug("Syntax error" rc "at line" sigl)
	return rc


Debug: procedure
	parse arg info

	firstLine = sourceline(1)
	parse var firstLine '/*' title '*/'
	say title ':' info
	return
