/*
 * Config ShadowMaster via it's Rexx port...
 *
 * Copyright 1991, Mike Meyer
 * All Rights Reserved
 *
 * See the file "ShadowMaster:Distribution" for information on distribution.
 */
options results
address SHADOWMASTER

/* Make sure we got tools we need to do the job */
if ~show('Libraries', 'rexxarplib.library') then
	if ~addlib('rexxarplib.library', 0, -30) then do
		say "Can't find rexxarplib.library"
		exit 10
		end

if ~show('Libraries', 'rexxsupport.library') then
	if ~addlib('rexxsupport.library', 0, -30) then do
		say "Can't find rexxsupport.library"
		exit 10
		end

/* Make sure there's a running ShadowMaster */
if ~show('Ports', 'SHADOWMASTER') then do
	say "Can't configure a ShadowMaster that isn't running"
	exit 10
	end

/* If we're not already running, set up my host and port */
if showlist('Ports', SMCONFIGPORT) | showlist('Ports', SMCONFIGHOST) then exit
address AREXX '"call createhost SMCONFIGHOST, SMCONFIGPORT"'
call openport SMCONFIGPORT
address command "waitforport SMCONFIGHOST"

/* Make sure we clean up if there's a problem (old versoin of RexxArpLib */
signal on syntax
signal on error

/* Create the world */
call OpenWindow SMCONFIGHOST, 0, 11, 350, 60, ,
	'CLOSEWINDOW+GADGETUP', 'WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH', ,
	'Screen Saver Configuration'

/* The String Gadgets */
Call SetAPen SMCONFIGHOST, 1
Call Move SMCONFIGHOST, 10, 27
Call Text SMCONFIGHOST, "Command"
Call AddGadget SMCONFIGHOST, 75, 20, "COMMAND", "", '%d%1%g', 255, RIDGEBORDER
Call Move SMCONFIGHOST, 10, 45
Call Text SMCONFIGHOST, "Seconds"
Call AddGadget SMCONFIGHOST, 75, 38, "SECONDS", "", '%d%1%g', 45, RIDGEBORDER
Call AddGadget SMCONFIGHOST, 130, 37, "CONFIG", "Config", '%d'
Call AddGadget SMCONFIGHOST, 190, 37, "BLANK", "Blank", '%d'
Call AddGadget SMCONFIGHOST, 242, 37, "QUIT", "Quit", '%d'
Call AddGadget SMCONFIGHOST, 300, 37, "USE", "Use", '%d'

/* Get the state of the world, and put it in old */
call saverstate
old.command = new.command
old.seconds = new.seconds
old.blanker = new.blanker
old.args = new.args

do forever
	call waitpkt SMCONFIGPORT
	do forever
		pkt = getpkt(SMCONFIGPORT)
		if c2d(pkt) = 0 then leave
		gadget = getarg(pkt, 0)
		select
			when gadget = "CLOSEWINDOW" then signal cancel
			when gadget = "BLANK" then 'blank'
			when gadget = "USE" then do
				call windowstate
				signal use
				end
			when gadget = "QUIT" then do
				'quit'
				signal use
				end
			when gadget = "CONFIG" then do
/* Now nonfunctional...
				call windowstate
				address command new.blanker'-config'
				call saverstate
*/				end
			when gadget = "COMMAND" then do
				new.command =  getarg(pkt, 1)
				'command' new.command
				call fixcommand
				end
			when gadget = "SECONDS" then do
				new.seconds = getarg(pkt, 1)
				'seconds' new.seconds
				end
			otherwise nop
			end
		end
	end

exit

cancel:
	'command' old.command
	'seconds' old.seconds
error:
syntax:
use:
	call closewindow SMCONFIGHOST
	call closeport SMCONFIGPORT
	exit

/* saverstate - makes the world confirm to what the saver thinks it is */
saverstate: procedure expose new.
	'command'
	new.command = result
	call fixcommand
	call RemoveGadget SMCONFIGHOST, "COMMAND"
	call AddGadget SMCONFIGHOST, 75, 20, "COMMAND", new.command, '%d%1%g', 255, RIDGEBORDER

	'seconds'
	new.seconds = result
	call RemoveGadget SMCONFIGHOST, "SECONDS"
	call AddGadget SMCONFIGHOST, 75, 38, "SECONDS", new.seconds, '%d%1%g', 45, RIDGEBORDER
	return

/* windowstate - makes the world confirm to whats in the window. */
windowstate: procedure expose new.

	/* Command gadget */
	call ReadGadget SMCONFIGHOST, "COMMAND"
	call waitpkt SMCONFIGPORT
	pkt = getpkt(SMCONFIGPORT)
	if getarg(pkt, 0) ~= "COMMAND" then exit 20
	new.command = getarg(pkt, 1)
	'command' new.command
	call fixcommand

	/* Seconds gadget */
	call ReadGadget SMCONFIGHOST, "SECONDS"
	call waitpkt SMCONFIGPORT
	pkt = getpkt(SMCONFIGPORT)
	if getarg(pkt, 0) ~= "SECONDS" then exit 20
	new.seconds = getarg(pkt, 1)
	'seconds' new.seconds
	return

/* fixcommand - given new.command, set new.blanker and new.args */
fixcommand: procedure expose new.
	parse value new.command with new.blanker new.args
	new.args = strip(new.args)
	if new.command == "" then new.blanker = "ShadowMaster:savers/"
