#!/bin/sh

dumpdir=webdump
update_only=no
tmpfile=/tmp/mkhtml.$$

about='S9fES is a mature, portable, and comprehensible R4RS Scheme
interpreter with an (optional) Unix interface. It is in the public domain.
You can <A href="s9fes.tgz">download</A> it here (~450KB).'

cleanup() {
	rm -f $tmpfile
	rm -f rpp rp_html
	exit
}

trap cleanup 1 2 3

usage() {
	echo "Usage: make-html [-u {scm|cee|txt|man|nah|dir}]"
	exit 1
}

while [ "x$1" != "x" ]; do
	case x$1 in
	x-u) 	shift
		if [ "x$1" = "x" ]; then usage; fi
		update_only=$1
		shift
		;;
	*)	usage
		;;
	esac
done

format_scm_files() {
	base=$1
	shift
	files=$*
	for file in $files; do
		html=`basename $file`.html
		fname=$(basename $file)
		name=$(grep "^$fname|" util/descriptions | cut -f 2 -d "|")
		desc=$(grep "^$fname|" util/descriptions | cut -f 3 -d "|")
		oname=$dumpdir/`basename $fname`.html
		echo -n $oname' ...'
		if [ "x$name" = "x" ]; then
			echo -n " NO DESCRIPTION!"
		fi
		sed -e "s|@BASE@|<A href="$html">$html</A>|g" \
			<util/libhead >$tmpfile
		if [ "x$desc" != "x-" -a "x$name" != "x-" ] ; then
			sed \
			-e "s|@DESC@|<CODE>$name\&nbsp;\&nbsp;</CODE> $desc|" \
			-e "s|@TITLE@|$name\&nbsp;\&nbsp; $desc|" \
				<$tmpfile >$oname
		elif [ "x$desc" != "x-" ] ; then
			sed -e "s|@DESC@|$desc|" \
			    -e "s|@TITLE@|$desc|" \
				<$tmpfile >$oname
		else
			sed -e "s|@DESC@|<CODE>$name</CODE>|" \
			    -e "s|@TITLE@|$name|" \
				<$tmpfile >$oname
		fi
		echo -n "<P>" >>$oname
		if [ "x$base" != "x" ]; then
			echo "Location: <A href=\"$base.html\">$base</A>, " \
				>>$oname
		fi
		sed -e "1,/^$/d" <$file | wc -l >>$oname
		echo " Lines</P>" >>$oname
		prog/scm2html1.scm -9x $file >>$oname
		cat util/libtail >>$oname
		echo " done"
	done
}

format_C_files() {
	base=$1
	shift
	files=$*
	for file in $files; do
		html=`basename $file`.html
		fname=$(basename $file)
		oname=$dumpdir/`basename $fname`.html
		echo -n $oname' ...'
		sed -e "s|@BASE@|<A href="$html">$html</A>|g" \
			<util/libhead >$tmpfile
		if [ "x$base" = "x" ]; then
			sed -e "s/@DESC@/`basename $file`/" \
			    -e "s/@TITLE@/`basename $file`/" \
				<$tmpfile >$oname
		else
			sed -e "s|@DESC@|<A href=\"$base.html\">$base</A>/`basename $file`|" \
			    -e "s/@TITLE@/`basename $file`/" \
				<$tmpfile >$oname
		fi
		echo -n "<P>" >>$oname
		sed -e "1,/^$/d" <$file | wc -l >>$oname
		echo " Lines</P>" >>$oname
		prog/c2html1.scm $file >>$oname
		cat util/libtail >>$oname
		echo " done"
	done
}

format_txt_files() {
	hrefs=no
	if [ "x$1" = "x-r" ]; then hrefs=yes; shift; fi
	base=$1
	shift
	files=$*
	for file in $files; do
		html=`basename $file`.html
		oname=$dumpdir/`basename $file`.html
		echo -n $oname' ...'
		sed -e "s|@BASE@|<A href="$html">$html</A>|g" \
			<util/libhead >$tmpfile
		if [ "x$base" = "x" ]; then
			sed -e "s/@DESC@/`basename $file`/" \
			    -e "s/@TITLE@/`basename $file`/" \
				<$tmpfile >$oname
		else
			sed -e "s|@DESC@|<A href=\"$base.html\">$base</A>/`basename $file`|" \
			    -e "s/@TITLE@/`basename $file`/" \
				<$tmpfile >$oname
		fi
		echo '<PRE>' >>$oname
		if [ $hrefs = yes ]; then
			prog/htmlify.scm -t $file | \
			sed -e 's/\(http:\/\/.*\)/<A href="\1">\1<\/A>/' \
				>>$oname
		else
			prog/htmlify.scm -t $file >>$oname
		fi
		echo '</PRE>' >>$oname
		cat util/libtail >>$oname
		echo " done"
	done
}

format_man_pages() {
	hrefs=no
	if [ "x$1" = "x-r" ]; then hrefs=yes; shift; fi
	files=$*
	for file in $files; do
		html=`basename $file`.html
		ofile=$dumpdir/`basename $file`.html
		echo -n $ofile' ...'
		sed -e "s|@BASE@|<A href="$html">$html</A>|g" \
			<util/libhead >$tmpfile
		sed -e "s/@DESC@/`basename $file`/" \
		    -e "s/@TITLE@/`basename $file`/" \
		<$tmpfile >$ofile
		echo '<PRE class=man>' >>$ofile
		if [ $hrefs = yes ]; then
			nroff $file | ./rpp -p html | \
			sed -e 's/\(http:\/\/.*\)</<A href="\1">\1<\/A></' \
				>>$ofile
		else
			nroff $file | ./rpp -p html >>$ofile
		fi
		echo '</PRE>' >>$ofile
		cat util/dirtail >>$ofile
		echo " done"
	done
}

naah() {
	base=$1; shift
	what=$1; shift
	files=$*
	for file in $files; do
		html=`basename $file`.html
		oname=$dumpdir/`basename $file`.html
		echo -n $oname' ...'
		sed -e "s|@BASE@|<A href="$html">$html</A>|g" \
			<util/libhead >$tmpfile
		if [ "x$base" = "x" ]; then
			sed -e "s/@DESC@/`basename $file`/" \
			    -e "s/@TITLE@/`basename $file`/" \
				<$tmpfile >$oname
		else
			sed -e "s|@DESC@|<A href=\"$base.html\">$base</A>/`basename $file`|" \
			    -e "s/@TITLE@/`basename $file`/" \
				<$tmpfile >$oname
		fi
		echo '<H2>Naah</H2>' >>$oname
		echo "<P>This $what is not included here. Please" >>$oname
		echo ' download the <A href="s9fes.tgz">archive</A>' >>$oname
		echo ' to have a look at it.</P>' >>$oname
		cat util/dirtail >>$oname
		echo " done"
	done
}

dirlist() {
	dir=$1
	ofile=$2
	title=$3
	echo -n $ofile' ...'
	if [ "x$dir" = "x." ]; then
		sed -e "s/@BASE@//" <util/dirhead >$tmpfile
	else
		sed -e "s|@BASE@|<A href=\"`basename $ofile`\">$dir.html</A>|" \
			<util/dirhead >$tmpfile
	fi
	sed -e "s/@DESC@/$title/" <$tmpfile >$ofile
	echo $4 >>$ofile
	echo '<PRE class=dirlist>' >>$ofile
	(cd $dir; ls -l | grep -v "$dumpdir") | grep -v "edoc$" | awk '
		{ if ($9 == "CATEGORIES.html")
			name=$9
		  else if ($9 == "s9fes.tgz")
			name=$9
		  else if ($9 == "MASCOT.png")
			name=$9
		  else if ($9 == "freebsd-port")
			name="http://www.freebsd.org/cgi/cvsweb.cgi/ports/lang/s9fes"
		  else
			name=$9".html"
		  if ($9 == "")
			print $0
		  else
			printf "%s %2s %3s %4s %7s %3s %2s %6s" \
				" <A href=\"%s\">%s</A>\n", \
				$1, $2, $3, $4, $5, $6, $7, $8, name, $9
		}' >>$ofile
	echo '</PRE>' >>$ofile
	cat util/dirtail >>$ofile
	echo " done"
}

if [ $update_only = no -o $update_only = dir ]; then
	if [ $update_only = no ]; then
		# XXX edoc
		make stripped-arc
		# make HACKING.html
		s9 -f util/make-cats.scm
	fi

	if [ -d $dumpdir ]; then
		if [ $update_only = no ]; then
			echo -n "You may want to remove the $dumpdir"
			echo " directory first!"
			exit 1
		fi
	else
		mkdir $dumpdir
		cp s9fes.tgz util/s9.css MASCOT.png $dumpdir
		cp prog/edoc.css contrib/scheme.css contrib/ccode.css $dumpdir
	fi

	dirlist . $dumpdir/index.html "Scheme 9 from Empty Space" "$about"
	dirlist lib $dumpdir/lib.html "lib\/" ""
	dirlist contrib $dumpdir/contrib.html "contrib\/" ""
	dirlist ext $dumpdir/ext.html "ext\/" ""
	dirlist prog $dumpdir/prog.html "prog\/" ""
	dirlist util $dumpdir/util.html "util\/" ""

	if [ $update_only = no ]; then
		mv CATEGORIES.html $dumpdir
		# mv HACKING.html $dumpdir/HACKING.html.html
		make
	fi
fi

if [ $update_only = no -o $update_only = txt ]; then
	format_txt_files -r "" ABOUT
	format_txt_files "" CHANGES LICENSE Makefile README Todo
	format_txt_files "" configure _checksums mkfile s9.1.txt
	format_txt_files lib lib/_template
	format_txt_files prog prog/adventure.adv prog/edoc.css
	format_txt_files prog prog/advgen.txt
	format_txt_files contrib contrib/scheme.css contrib/ccode.css
	format_txt_files contrib contrib/format.txt contrib/s9sos.txt
	format_txt_files contrib contrib/s9e.help contrib/s9e-config
	format_txt_files util util/descriptions util/make-help-links
	format_txt_files util util/libtest.sh util/make-docs util/make-html
	format_txt_files util util/rp_html util/s9.css	
fi

if [ $update_only = no -o $update_only = cee ]; then
	format_C_files "" s9.c s9.h s9-real.c
	format_C_files ext ext/unix.c ext/curses.c
	format_C_files util util/rpp.c
fi

if [ $update_only = no -o $update_only = scm ]; then
	format_scm_files "" s9.scm
	format_scm_files "" s9-real.scm
	format_scm_files "" config.scm
	format_scm_files lib lib/*.scm
	format_scm_files contrib contrib/*.scm
	format_scm_files ext ext/*.scm
	format_scm_files prog prog/*.scm
	format_scm_files util util/*.scm
fi

if [ $update_only = no -o $update_only = nah ]; then
	naah "" directory help
	naah "" directory freebsd-port
	naah contrib file S9Book
	naah contrib file S9Book-bw
	naah prog file adventure.intro
	naah util file dirhead
	naah util file dirtail
	naah util file libhead
	naah util file libtail
	naah util file pagehead
	naah util file pagetail
	naah util file stress-tests.tgz
	naah util file categories.html
fi

if [ $update_only = no -o $update_only = man ]; then
	cc -o rpp util/rpp.c && ln -s util/rp_html rp_html
	format_man_pages -r s9.1
fi

cleanup
