#!/bin/sh

tmp=/tmp/make-doc.tmp.$$
tmp2=/tmp/make-doc.tmp2.$$
sigs=/tmp/make-doc.sigs.$$
prose=/tmp/make-doc.prose.$$
exms=/tmp/make-doc.exms.$$

cleanup() {
	rm -f $tmp $tmp2 $sigs $prose $exms
}

trap cleanup 1 2 3

format() {
	sed -e '1,/^;$/d' <$1 >$tmp
	sed -ne '1,/^;$/p' <$tmp | sed -e 's/^; //' -e '$d' >$sigs
	sed -e '1,/^;$/d' \
	    -e 's/; (load-from-library/;         (load-from-library/' \
	    <$tmp >$tmp2 && mv -f $tmp2 $tmp

	if grep '^; Given: ' $tmp >/dev/null; then
		sed -ne '1,/^;.*Given:   /p' <$tmp | \
			sed -e 's/^; //' -e 's/^;$//' -e '$d' >$prose

		sed -ne '/^;.*Given:   /,/^$/p' <$tmp | sed -e '$d' | \
			sed -e 's/^;$//' | \
			sed -e 's/^.............//' >$exms
	else
		sed -ne '1,/^;.*Example)*:/p' <$tmp | \
			sed -e 's/^; //' -e 's/^;$//' -e '$d' >$prose

		sed -ne '/^;.*Example)*:/,/^$/p' <$tmp | sed -e '$d' | \
			sed -e 's/^;$//' | \
			sed -e 's/^.............//' >$exms
	fi

	sed -e '1s/^/S9 LIB  /' -e '2,$s/^/        /' <$sigs
	echo
	cat $prose
	cat $exms
}

if [ ! -d help-new ]; then
	mkdir help-new
	mkdir help-new/sys-unix
	mkdir help-new/curses
	mkdir help-new/csv
fi

for file in lib/*.scm contrib/*.scm; do
	if echo $file | grep -- '-test.scm' >/dev/null 2>&1; then
		continue
	fi
	format $file >help-new/$(basename $file .scm)
	echo $file
done

for ext in sys-unix curses csv; do
	for file in ext/$ext/*.scm; do
		format $file >help-new/$ext/$(basename $file .scm)
		echo $file
	done
done

cd help-new

rm -f	csv/csv		\
	curses/curses	\
	fluid-let-sr	\
	format		\
	io-tools	\
	list-tools	\
	math-tools	\
	set-tools	\
	string-tools	\
	syntax-rules	\
	sys-unix/unix	\
	unix-tools	\
	vector-tools

mv -f amk runstar
mv -f array make-array
mv -f bitops bit0
mv -f bitwise-ops bitwise-and
mv -f char-canvas make-canvas
mv -f hof complement
mv -f hash-table make-hash-table
mv -f letcc letslashcc
mv -f matcher define-matcher
mv -f setters pushb
mv -f rb-tree make-rbt
mv -f records record
mv -f regex re-comp
mv -f simple-modules module
mv -f s9sos define-class
mv -f sys-unix/standard-error sys-unix/standard-error-port
mv -f streams make-stream
mv -f string-case string-upcase
mv -f symbols r4rs-procedures
mv -f threads thread-create
mv -f time-ops time-add

cleanup
