/* FILESBBS_TO_SUBJECT.CMD -- Convert FILES.BBS to descriptions */
/*
 * This is a REXX script that parses the contents of the FILES.BBS and
 * DESCRIPT.ION in the current directory and for each file or directory
 * found attempts to set the .SUBJECT extended attribute to the description
 * given.
 *
 * (c) Copyright 1998-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 'SysPutEA','RexxUtil','SysPutEA'
call RxFuncAdd 'SysGetMessage','RexxUtil','SysGetMessage'

if "" \= stream('FILES.BBS','c','query exists') then
	call ParseFile 'FILES.BBS'
if "" \= stream('DESCRIPT.ION','c','query exists') then
	call ParseFile 'DESCRIPT.ION'

return

ParseFile: procedure
	parse arg DescriptionFileName

	DescriptionFile = .stream~new(DescriptionFileName)
	do while DescriptionFile~lines() \= 0
		Line = DescriptionFile~linein()
		select
			when line == '' then nop
			when line~left(1) == '-' then nop
			when line~left(1) == '"' then do
				parse var line '"'filename'"' description
				call Describe filename description
			end
			otherwise do
				parse var line filename description
				call Describe filename description
			end
		end
	end
	error = DescriptionFile~close()
	drop DescriptionFile
return

Describe: procedure
	parse arg Filename Description
	subject = "FDFF"x||description~length()~d2c(2)~reverse()||description
	error = SysPutEA(Filename, ".SUBJECT", subject)
	if 0 \= error then
		say SysGetMessage(error,,Filename),
			' "'directory()'\'Filename'"'
return
