/* SUBJECT_TO_FILESBBS.CMD -- Convert descriptions to FILES.BBS */
/*
 * This is a REXX script that reads the .SUBJECT extended attributes of files
 * and directories in the current directory and from them generates a list
 * of names and descriptions to its standard output, suitable for inclusion in
 * a FILES.BBS or DESCRIPT.ION file.
 *
 * (c) Copyright 1999-2004,2007 Jonathan de Boyne Pollard.  "Moral" rights asserted.
 *
 * Permission is hereby granted to copy, modify, and redistribute this script
 * as long as the author is not held responsible for any consequences of its
 * possession or use, and as long as you agree that it comes with no guarantee.
 *
 * NOTE: This script requires Object REXX.
 */

call RxFuncAdd 'SysFileTree','RexxUtil','SysFileTree'
call RxFuncAdd 'SysGetEA','RexxUtil','SysGetEA'
call RxFuncAdd 'SysGetMessage','RexxUtil','SysGetMessage'

error = SysFileTree('*', 'names', 'BTO')
if error \= 0 then do
	say SysGetMessage(error,,'*')||' "*"'
	exit
end

do i = 1 to names.0
	basename = filespec("Name",names.i)

	error = SysGetEA(names.i, ".SUBJECT", "subject")
	select
		when error \= 0 then
			say SysGetMessage(error,,names.i)||' "'||names.i||'"'
		when subject \= '' then do
			len = subject~substr(3,2)~reverse()~c2d()
			description = subject~substr(5,len)
			say enquote(basename)||' '||description
		end
		otherwise nop
	end
end

return

enquote: procedure
	parse arg s

	if s~pos(' ') > 0 | s~pos(X'09') > 0 then
		quote = '"'
	else
		quote = ''

	return quote||s||quote
