/*

                  $VER: MM_BundleFiles_SetCfg 0.10  (10.05.96)

                            (C) 1996 Robert Hofmann

*/

parse arg args

options cache
options failat 99
options results

signal on break_c
signal on break_d
signal on break_e
signal on break_f
signal on halt
signal on ioerr
signal on syntax

address 'MAILMANAGER'


Main:

	call Init
	call Header
	call Parse_Args(strip(args))

	MM_ReadStem system.prg.cfg 'system.cfg'

	if system.arg.get then ret	= Get_Cfg(system.arg.node)
	if system.arg.set	then ret	= Set_Cfg(system.arg.node, system.arg.archiver, system.arg.name)

	if system.changed	then
		do
			MM_CopyFile system.prg.cfg system.prg.cfg
			MM_WriteStem system.prg.cfg 'system.cfg'
		end

call Quit(ret, 'All done.')
exit


break_c:; break_d:; break_e:; break_f:; halt:

	signal off break_c
	signal off break_d
	signal off break_e
	signal off break_f
	signal off halt

	return_code		=	5
	error_line    = 0
	error_msg			= 'Execution halted!!!'
	rc						= 0
signal Exit


Build_Line: procedure Expose system.

	parse arg node, arcer, name

	line	= '#ARCNODE     ' left(node, max(29, length(node)+1))

	if arcer~=''	then line = line 'ARCHIVER'	left(arcer, max(5, length(arcer)+1))
	if name~=''		then line = line 'NAME'			name

return strip(line)


Exit:

	select
		when return_code>=40 then error = 'INTERNAL-ERROR:'
		when return_code>=30 then error = 'IO-ERROR:'
		when return_code>=20 then error = 'ERROR:'
		when return_code>=10 then error = 'WARNING:'
		when return_code>=5  then error = 'INFO:'
		otherwise									error = ''
	end

	tmp	= '***' strip(error error_msg) '***'

	if return_code>5 then say tmp

  call Log()
	call Log(tmp, '+')
	call Log(,'\')

	call setclip('MM_LogPre', system.mm.logpre)

exit return_code


Get_Arg: procedure Expose args system.

  arg keyword, mode, old

	uargs	= upper(args)
	p			= find(uargs, keyword)

	if p=0	then
		do
			p = pos(' 'keyword'=', ' 'uargs)

			if p>0 then	args	= overlay(' ', args, p+length(keyword))

      p = find(upper(args), keyword)
		end

	system.cmdopt.keyword	= p>0

	select
		when mode=0	then
			if p>0 then
				do
					ret		= 1
					args	= delword(args, p, 1)
				end
			else ret	= old

		when mode=1 then
			if p>0 then
				do
					left	= subword(args, 1, p-1)
					rest	= subword(args, p+1)

					if left(rest, 1)='"' then	parse var rest . '"'	ret '"'	rest
					else											parse var rest				ret			rest

					args	= strip(left strip(rest))
				end
			else ret	= old

		when mode=2 then
			do
				if left(args, 1)='"'	then	parse var args . '"'	ret '"'	args
				else												parse var args				ret 		args

				if strip(ret)=''			then	ret = old
			end

		otherwise exit 99
	end

	args	= strip(args)
	ret		= strip(ret, 'b', '" ')

return ret


Get_Cfg: procedure Expose system.

	parse arg node

	ret.	= ''
	MM_SearchInStem 'system.cfg' 'ret' '"?ARCNODE#?'node'#?"' 'STR'

	say ret.0 

	call setclip('MM_BundleFiles_Cfg', ret.0)

return (ret.0='')*5


Get_Version: procedure

	parse arg mode

	parse value sourceline(3-mode) with . . ver .
	parse var ver tst 'ß' .

	if ~datatype(strip(tst, 'b', '/ce '), 'N') then
		if ~mode then ver = Get_Version(1)
		else exit 99

return ver


Header:

	call Log(,'/')
	call Log('***' system.prg.id '***', '+')
	call Log(' 'system.prg.cr)
	call Log()

return


Init:

	system.						= 0

	system.prg.ver		= Get_Version(0)
	system.prg.name		= 'MM_BundleFiles_SetCfg'
	system.prg.id			= system.prg.name 'v'system.prg.ver
	system.prg.cfg		= 'MM:Config/MM_BundleFiles.cfg'
	system.prg.cr			= '(C) 1996 Robert Hofmann'
	system.tmpfile		= 'T:'system.prg.name'.tmp'
	system.mm.logpre 	= getclip('MM_LogPre')
	system.prg.logpre	= system.mm.logpre'|'
	call								setclip('MM_LogPre', system.prg.logpre)
	system.cmdopts		= 'GET/S,SET/S,NODE/K/A,ARCHIVER/K,NAME/K,DELETE/S'

	call Include_Lib('rexxsupport')
return


Include_Lib: procedure Expose system.

	parse arg lib, prio
	if right(upper(lib), 8)~='.LIBRARY' then lib	= lib'.library'
	if prio=''													then prio	= 0

	if ~show('l', lib) then
		if ~addlib(lib, prio, -30, 0) then call Quit(20, 'Could not open' lib'!!!')

return


IOerr:

	signal off ioerr

	return_code		= 20
	error_line    = sigl
	error_msg			= 'IO-error' rc 'at line' sigl '['errortext(rc)']')
	rc						= 0
signal Exit


Log: procedure Expose system.

	parse arg text, pre, level

	if ~datatype(level, 'N') then level = system.prg.loglevel

	tmp		= word('PRG MM', (pre~='')+1)
	text	= system.tmp.logpre || pre' 'text

	MM_WriteLog 'text' level
return


Parse_Args: procedure Expose system.

	parse arg args

	tpl		= system.cmdopts',?/S'
	args	= translate(args, '  ', '9'x'=')

	pk		= pos('/K', tpl)
	ps		= pos('/S', tpl)

	select
		when pk=0	& ps=0	then	p	= 0
		when pk=0 & ps>0	then  p	= ps
		when ps=0 & pk>0	then	p	= pk
		otherwise								p	= min(pk, ps)
	end

	p			= lastpos(',', left(tpl, p))
	tpl		= substr(tpl, p+1) || left(tpl, max(p-1, 0))

	do while tpl~=''
		parse var tpl template ',' tpl
		parse var template keyword '/' .

		bool	= pos('/S',	template)>0
		key		= pos('/K', template)>0
		must	= pos('/A', template)>0
		num		= pos('/N', template)>0

		select
			when must then		system.arg.keyword	= '0'x
			when bool	then		system.arg.keyword	= 0
			when num	then		system.arg.keyword	= 0

			otherwise					system.arg.keyword	= ''
		end

		if bool | key	then	mode	= ~bool
		else								mode	= 2

		system.arg.keyword	= Get_Arg(keyword, mode, system.arg.keyword)

		if keyword='?' & system.arg.keyword=1 then leave

		if must & system.arg.keyword='0'x then
			do
				tmp	= template 'missing!!!'

				say
				say ' ***' tmp '***'

				signal Usage
			end

		if num & ~datatype(system.arg.keyword, 'N') then
			if ~must & system.arg.keyword='' then	system.arg.keyword	= 0
			else
				do
					tmp	= 'Numeric value expected for' template', but is "'system.arg.keyword'"!!!'

					say
					say ' ***' tmp '***'

					signal Usage
				end
	end

	tmp	= '?'; if system.arg.tmp then signal Usage

	if args~='' then call Quit(10, 'Unknown option(s):' args)


	if (~system.arg.get & ~system.arg.set) | system.arg.node='' then call Quit(11, 'Too few arguments!')

	MM_GetNodeInfo system.arg.node 'tmp'
	if RC~=0 then call Quit(12, 'No MM-cfg found for node "'system.arg.node'"!')

	if system.arg.get | system.arg.delete then
		if system.arg.archiver~='' | system.arg.name~='' then call Quit(11, 'Too many arguments!')

return


Parse_Cfg_Args: procedure Expose cfg. system.

	parse arg args, tpl, cfgkey

	args	= strip(translate(args, '  ', '9'x'='))

	pk		= pos('/K', tpl)
	ps		= pos('/S', tpl)

	select
		when pk=0	& ps=0	then	p	= 0
		when pk=0 & ps>0	then  p	= ps
		when ps=0 & pk>0	then	p	= pk
		otherwise								p	= min(pk, ps)
	end

	p			= lastpos(',', left(tpl, p))
	tpl		= substr(tpl',', p+1) || left(tpl, max(p-1, 0))

	do while tpl~=''
		parse var tpl template ',' tpl
		parse var template keyword '/' .

		bool	= pos('/S',	template)>0
		key		= pos('/K', template)>0
		must	= pos('/A', template)>0
		num		= pos('/N', template)>0

		select
			when must then		cfg.prm.keyword	= '0'x
			when bool	then		cfg.prm.keyword	= 0
			when num	then		cfg.prm.keyword	= 0

			otherwise					cfg.prm.keyword	= ''
		end

		if bool | key	then	mode	= ~bool
		else				mode	= 2

		cfg.prm.keyword	= Get_Arg(keyword, mode, cfg.prm.keyword)

		if must & cfg.prm.keyword='0'x then call Quit(15, template 'for' cfgkey 'missing!')

		if num & ~datatype(cfg.prm.keyword, 'N') then
			if ~must & cfg.prm.keyword='' then	cfg.prm.keyword	= 0
			else call Quit(15, 'Numeric value expected for'cfgkey template', but is "'cfg.prm.keyword'"!!!')
	end

	if args~='' then call Quit(10, 'Unknown option(s) "'args'" for' cfgkey'!!!')

return


Quit:

	parse arg return_code, error_msg

	error_line    = 0
	rc						= 0
signal Exit


Replace: procedure

	parse arg string, new, old

	do while index(string, old) ~= 0
		interpret "parse var string l '"old"' r"
		string = l || new || r
	end

return string


Set_Cfg: procedure Expose system.

	parse arg node, arcer, basename
	upper arcer name

	call Log(' Setting' node'...')

	ret.	= 0
	MM_SearchInStem 'system.cfg' 'ret' '"?ARCNODE#?'node'#?"' 'NUM'

	MM_GetNodeInfo node 'ninfo'
	if arcer=ninfo.archiver then arcer=''

	if ret.count=0 then
		if system.arg.delete then call Log('  -> No cfg-entry for node "'node'"!')
		else
			do
				if arcer='*'		then arcer		= ''
				if basename='*'	then basename	= ''

				line	= Build_Line(node, arcer, basename)

				call Log('  -> Adding "'line'".')

				MM_AddToStem 'system.cfg' 'line'

				system.changed	= 1
			end
	else
		do
			nr	= ret.0

			if system.arg.delete then
				do
					call Log('  -> Deleting "'system.cfg.nr'"')

					do n=nr+1 to system.cfg.count-1
						last						= n-1
						system.cfg.last	= system.cfg.n
					end

					system.cfg.count	= system.cfg.count-1
				end
			else
				do
					call Parse_Cfg_Args(subword(system.cfg.nr, 2), 'NODE/A,ARCHIVER/K,NAME/K', '#ARCNODE')

					if arcer=''			then arcer		= cfg.prm.archiver
					if arcer='*'		then arcer		= ''
					if basename=''	then basename	= cfg.prm.name
					if basename='*'	then basename	= ''

					line = Build_Line(node, arcer, basename)

					if system.cfg.nr~=line then
						do
							call Log('  -> Old: "'system.cfg.nr'"')
							call Log('  -> New: "'line'"')

							system.cfg.nr		= line
							system.changed	= 1
						end
					else call Log('  -> Nothing to change.')
				end

	call setclip('MM_BundleFiles_Cfg', line)

return ~system.changed*5


Syntax:

	signal off syntax

	return_code		= 40
	error_line    = sigl
	error_msg			= 'Syntax-error' rc 'at line' sigl '['errortext(rc)']'
	rc						= 0
signal Exit


Usage:

	rx.			= ''
	rx.0.0	= '[rx] '
	rx.0.1	= '[.rexx]'
	m				=	pos('/e', system.prg.ver)>0

	say
	say 'Usage:' rx.m.0 || system.prg.name || rx.m.1 system.cmdopts
	say
call Quit(0, 'Usage requested.')


