/*
 * ADInd
 *
 * $VER: v1.0  Rick Younie Thu Mar 25 1993
 *
 *	 please send comments, questions or improvements:
 *			rick@emma.panam.wimsey.bc.ca	1:153/765.9
 *			rick@emma.tfbbs.wimsey.bc.ca
 *
 * USAGE: '?' for usage
 *
 * SYNOPSIS: 
 *	 builds an index file for the C= autodocs (or any doc file
 *	 in that format)
 *
 *	 libs:rexxarplib.library is required 
 */
	signal on BREAK_C
	signal on SYNTAX

/* constants and assigns */

	tab			= '09'x
	lf			= '0a'x
	ff			= '0c'x

	autoDir		= 'AUTODOCS:'

Main:
	parse arg comline
	select
		when comline = '' then nop
		when comline = '?' then call Usage
		otherwise do
			autoDir  = comline
			if right(autoDir,1)~=':' & right(autoDir,1)~='/' then
				call ErX '..directory must end in / or :'
		end
	end

	if pragma('D',autoDir)='' then call ErX '..can''t find "'autoDir'"'

	/* get autodoc file names */
	file.0 = filelist('*.doc','FILE','F')
	if file.0 = 0 then call ErX '..no *.doc files in' autoDir

	say
	do i = 1 to file.0
		parse var file.i file '.doc'
		call close 'DOC'
		call open 'DOC',autoDir || file.i,'Read'

		/* get the start of the first function definition */
		do forever
			line = readln('DOC')
			if eof('DOC') then iterate i
			if left(line,1)=ff then call DoDocs
		end

		/* do all the function defs */
		call DoDocs
	end
	exit


/* make autodoc.index file with functions and their offsets */
DoDocs:

	eof = 0
	do fred = 1

		/* starting at ff, lose whitespace to start of doc */
		do forever
			nameStart = verify(line,lf' 'tab || ff,'Nomatch')
			if nameStart~=0 then leave
			line = readln('DOC')
			if eof('DOC') then leave fred
		end
		docStart = seek('DOC',0,'C')-length(line)+nameStart-2

		/* parse for function name */
		line = translate(line,,'/'tab)
		funcName = word(line,2)	 /* handles both 'file/name  file/name'
									and 'name  name' */

		/* find end of description */
		do forever
			line = readln('DOC')
			if eof('DOC') then leave
			if left(line,1)=ff then leave
		end

		/* need eof because the seek() resets eof() */
		if eof('DOC') then do
			docEnd = seek('DOC',0,'E')-2
			eof = 1
		end
		else docEnd	= seek('DOC',0,'C')-length(line)-2

		say upper(funcName) file docStart docEnd-docStart
		if eof then leave fred

	end fred
	return

Usage:
	say 'Usage: ADInd >autodocs:INDEX [dir]'
	say '	 - all *.doc files in [dir] are parsed and'
	say '		an index file is printed to stdout'
	say '	 - default [dir] is' autoDir
	exit

SYNTAX:
	say '..aborting - you''re likely missing either rexxarplib.library'
	say '	or arp.library'
	exit
ErX:
	say arg(1)
BREAK_C:
	exit
