/*
** $VER: GetCmdInfo.thor 1.0 (5.9.97)
** by Eirik Nicolai Synnes
**
** Displays the templates of all, one or a matching set of the THOR or
** BBSREAD ARexx command sets. Use "rx GetCmd.br ?" for help
**
*/

options results
parse arg arguments

cmdline = 'COMMAND,THOR/S,BBSREAD/S,MATCH/S'
cmdhelp = 'Template: ' || cmdline || '0A'x || '   Usage: GetCmd.br [BBSREAD] [THOR] (shows all available commands)' || '0A'x || '       or GetCmd.br <command> [BBSREAD] [THOR] (shows one specific command)' || '0A'x || '       or GetCmd.br MATCH <command> [BBSREAD] [THOR] (substring search)'

showncmd = 0; showcmd = 0


/*
** Open BBSREAD ARexx port
*/

if ~(show('P', 'BBSREAD')) then do
	address(command)
	'Run >NIL: `GetEnv THOR/THORPath`bin/LoadBBSRead'
	'WaitForPort BBSREAD'
	if (rc ~= 0) then displayerror(30, 'SortMail', 'Couldn''t open BBSREAD''s ARexx port.')
end


/*
** See if there is a Thor ARexx port we can shanghai
*/

ports = show('P')
do i = 1 to words(ports)
	if pos(' THOR.', ports) > 0 then thorport = word(substr(ports, pos(' THOR.', ports)), 1)
end

/*
** Parse command line arguments
*/

if (arguments = '?') then do
	say cmdhelp
	exit(5)
end

address(bbsread)
'READARGS TEMPLATE "'cmdline'" STEM 'args' CMDLINE 'arguments
if (rc ~= 0) then do
	say BBSREAD.LASTERROR
	say cmdhelp
	exit(5)
end

if (symbol('args.COMMAND') = 'VAR') then showcmd = 1

if ~(args.BBSREAD) & ~(args.THOR) then do
	say 'Specify BBSREAD and/or THOR.'
	say cmdhelp
	exit(5)
end

if (args.THOR) & ~(args.BBSREAD) & (symbol('thorport') ~= 'VAR') then do
	say 'Could not find Thor ARexx port.'
	exit(5)
end

if (args.THOR) & (symbol('thorport') ~= 'VAR') then do
	say 'Could not find Thor ARexx port, only showing BBSREAD commands.'; say
end

if ~(showcmd) & (args.MATCH) then do
	say 'MATCH can only be used together with a string to search for.'
	say cmdhelp
	exit(5)
end

/*
** Show the command template
*/

cnt = 0
if (args.THOR) then do
	cnt = cnt + 1; port.cnt.name = thorport; port.cnt.type = 'THOR'
end
if (args.BBSREAD) then do
	cnt = cnt + 1; port.cnt.name = 'BBSREAD'; port.cnt.type = 'BBSREAD'
end
port.count = cnt; drop cnt

do j = 1 to port.count
	address(port.j.name)

	'GETCOMMANDINFO STEM 'cmd
	if (rc ~= 0) then do
		say 'GETCOMMANDINFO: 'THOR.LASTERROR
		exit(rc)
	end

	if ~(showcmd) then do
		if port.j.type = 'THOR' then say 'THOR Arexx commands (' || cmd.count || ' in total):'
		else say 'BBSREAD Arexx commands (' || cmd.count || ' in total):'
	end

	do i = 1 to cmd.count
		if (showcmd) then do
			if (~(args.MATCH) & (upper(args.COMMAND) = upper(cmd.i))) | ((args.MATCH) & (index(upper(cmd.i), upper(args.COMMAND)) > 0)) then do
				if port.j.type = 'BBSREAD' then say 'Found BBSREAD command:'; else say 'Found THOR command:'
				say 'Command:  'cmd.i
				say 'Template: 'cmd.i.template
				say
				showncmd = 1
			end
		end
		else do
			say 'Command:  'cmd.i
			say 'Template: 'cmd.i.template
			say
		end
	end
end

if (showcmd) & ~(showncmd) then do
	if (args.MATCH) then say 'Could not find substring "' || args.COMMAND || '" in any ARexx command.'
	else say 'Could not find ARexx command "' || args.COMMAND || '"'
end

exit(0)
