#!/bin/sh

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

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
		;;
	x-r) 	shift
		if [ "x$1" = "x" ]; then usage; fi
		V=$1
		export V
		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`
		echo -n $oname' ...'
		if [ "x$name" = "x" ]; then
			echo -n " NO DESCRIPTION!"
		fi
		if [ "x$desc" != "x-" -a "x$name" != "x-" ] ; then
			echo ".h1 $name" | sed -e 's/_/\\_/g' |
				sed -e 's/\*/\\*/' >$oname
			echo ".h2 $desc" >>$oname
		elif [ "x$desc" != "x-" ] ; then
			echo ".h1 $desc" >$oname
		else
			echo ".h1 $name" | sed -e 's/_/\\_/g' |
				sed -e 's/\*/\\*/' >$oname
		fi
		if [ "x$base" != "x" ]; then
			echo >>$oname
			echo -n "Location: [$base], " >>$oname
		fi
		sed -e "1,/^$/d" <$file | wc -l >>$oname
		echo " Lines" >>$oname
		echo >>$oname
		echo ".html" >>$oname
		prog/scm2html1.scm -9x $file >>$oname
		echo ".." >>$oname
		echo ".sp" >>$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`
		echo -n $oname' ...'
		echo ".h1 `basename $file`" | sed -e 's/_/\\_/g' |
			sed -e 's/\*/\\*/' >$oname
		sed -e "1,/^$/d" <$file | wc -l >>$oname
		echo " Lines" >>$oname
		echo >>$oname
		echo ".html" >>$oname
		prog/c2html1.scm $file >>$oname
		echo ".." >>$oname
		echo ".sp" >>$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`
		echo -n $oname' ...'
		echo ".h1 `basename $file`" | sed -e 's/_/\\_/g' |
			sed -e 's/\*/\\*/' >$oname
		echo '.html' >>$oname
		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
		echo '..' >>$oname
		echo '.sp' >>$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`
		echo -n $ofile' ...'
		echo ".h1 `basename $file`" | sed -e 's/_/\\_/g' |
			sed -e 's/\*/\\*/' >$ofile
		echo ".html" >>$ofile
		echo '<PRE class=man>' >>$ofile
		if [ $hrefs = yes ]; then
			nroff -mdoc -c $file | ./rpp -p html | \
			sed -e 's/\(http:\/\/.*\)</<A href="\1">\1<\/A></' \
				>>$ofile
		else
			nroff -mdoc -c $file | ./rpp -p html >>$ofile
		fi
		echo '</PRE>' >>$ofile
		echo ".." >>$ofile
		echo ".sp" >>$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`
		echo -n $oname' ...'
		echo ".h1 `basename $file`" | sed -e 's/_/\\_/g' |
			sed -e 's/\*/\\*/' >$oname
		sed -e "s|@BASE@|<A href="$html">$html</A>|g" \
			<util/libhead >$tmpfile
		echo '.h2 Naah' >>$oname
		echo >>$oname
		echo "This $what is not included here. Please" >>$oname
		echo ' download the {s9fes-'$V'.tgz} archive' \ >>$oname
		echo ' to have a look at it.' >>$oname
		echo '.sp' >>$oname
		echo " done"
	done
}

dirlist() {
	dir=$1
	ofile=$2
	title=$3
	echo -n $ofile' ...'
	echo ".h1 $title" >$ofile
	if [ "$4" = "about" ]; then
		sed -e 's/\$V/'"$V"'/g' <util/blurb >>$ofile
	fi
	echo ':{<PRE>}' >>$ofile
	(cd $dir; ls -l | grep -v "$dumpdir") | grep -v " edoc$" | awk '
		BEGIN { V=ENVIRON["V"] }
		{ html=0
		  if ($9 == "CATEGORIES")
			name="["$9"]"
		  else if ($9 == "s9fes-"V".tgz")
			name="{"$9"}"
		  else if ($9 == "s9core-"V".tgz")
			name="{"$9"}"
		  else if ($9 == "s9core.pdf")
			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=1
		  }
		  if ($9 == "")
			print $0
		  else if (html) {
			gsub("_", "\\_", name)
			gsub("\\*", "\\*", name)
			printf "%s %2s %3s %4s %7s %3s %2s %6s [%s|%s]\n", \
				$1, $2, $3, $4, $5, $6, $7, $8, name, $9
		  }
		  else
			printf "%s %2s %3s %4s %7s %3s %2s %6s %s\n", \
				$1, $2, $3, $4, $5, $6, $7, $8, name
		}' >>$ofile
	echo ':{</PRE>}' >>$ofile
	echo ".sp" >>$ofile
	echo " done"
}

if [ $update_only = no -o $update_only = dir ]; then
	if [ $update_only = no ]; then
		make cdist && mv -f s9core-$V.tgz /tmp/s9core$$.tgz
		make dist
		# s9 -f util/make-cats.scm
	fi

	if [ -d $dumpdir ]; then
		rm -f /tmp/s9core$$.tgz
		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-$V.tgz MASCOT.png $dumpdir
		cp prog/edoc.css contrib/scheme.css contrib/ccode.css $dumpdir
		mv -f /tmp/s9core$$.tgz s9core-$V.tgz
	fi

	dirlist . $dumpdir/index \
		"Scheme 9 from Empty Space" "about"
	dirlist lib $dumpdir/lib "lib\/" ""
	dirlist contrib $dumpdir/contrib "contrib\/" ""
	dirlist ext $dumpdir/ext "ext\/" ""
	dirlist ext/sys-unix $dumpdir/sys-unix "sys-unix\/" ""
	dirlist ext/curses $dumpdir/curses "curses\/" ""
	dirlist ext/csv $dumpdir/csv "csv\/" ""
	dirlist prog $dumpdir/prog "prog\/" ""
	dirlist util $dumpdir/util "util\/" ""

	if [ $update_only = no ]; then
		# mv -f CATEGORIES $dumpdir
		mv -f s9core-$V.tgz $dumpdir
		make
	fi
fi

if [ $update_only = no -o $update_only = txt ]; then
	format_txt_files "" HISTORY LICENSE PREHISTORY
	format_txt_files "" Makefile README TODO README.s9core
	format_txt_files "" configure _csums mkfile s9.1.txt s9core.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 util util/descriptions util/rp_html util/s9.rc
	format_txt_files util util/libtest.sh util/make-docs util/make-html
fi

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

make_edoc() {
	echo -n "cmsdump/edoc ..."
	echo ".h1 Rendered EDOC Code" >cmsdump/_
	echo ".html" >>cmsdump/_
	edoc -l scheme -w prog/edoc.scm.edoc \
		| sed -e '1,/^$/d' >>cmsdump/_
	echo ".." >>cmsdump/_
        head -$(expr `wc -l cmsdump/_ | awk '{print \$1}'` - 3) \
		cmsdump/_ > cmsdump/edoc
	rm -f cmsdump/_
	echo " done"
}

if [ $update_only = no -o $update_only = scm ]; then
	make_edoc
	format_scm_files "" s9.scm
	format_scm_files "" config.scm
	format_scm_files lib lib/*.scm
	format_scm_files contrib contrib/*.scm
	format_scm_files sys-unix ext/sys-unix/*.scm
	format_scm_files curses ext/curses/*.scm
	format_scm_files csv ext/csv/*.scm
	format_scm_files prog prog/*.scm prog/*.scm.edoc
	format_scm_files util util/*.scm
fi

if [ $update_only = no -o $update_only = nah ]; then
	naah "" directory help
	naah "" directory freebsd-port
	naah "" directory attic
	naah prog file adventure.intro
	naah prog file edocbook.tr
	naah util file blurb
	naah util file book
	naah util file dirhead
	naah util file dirtail
	naah util file libhead
	naah util file libtail
	naah util file mktoc.sed
	naah util file pagehead
	naah util file pagetail
	naah util file stress-tests.tgz
	naah util file categories.html
	naah util file make-cms
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
