#!/bin/sh
TRUNK=../../../
LIBMBTK=$TRUNK/libmbtk

HEAD='
<html>
<body>
'

TAIL='
</body>
</html>
'

gen()
{
	echo "$HEAD"
	awk '
		BEGIN {
			q = "\""
		}
		function quote(s    ,link) {
			gsub("&", "&amp;", s)
			gsub("<", "&lt;", s)
			gsub(">", "&gt;", s)
			gsub("\t", "  ", s)

			if (match(s, "see:?[ \t]*[^ \t]*.html")) {
				link =  substr(s, RSTART, RLENGTH)
				sub("^see:?[ \t]*", "", link)
				sub("doc/ref", link)
				s = substr(s, 1, RSTART-1) " see: <a href=" q "../" link q ">" link "</a>"
			}
			return s
		}

		BEGIN { print "<pre>" }
		/^#[ \t]*ifndef LIBMBTK_.*_H$/ { next }
		/^#[ \t]*define LIBMBTK_.*_H$/ { next }
		/^#[ \t]*endif/ { next}
		/^#[ \t]*include/ { next}
		/^\/[*]+[ \t]*[Ii]nternal/ { exit }
		/^\/[*]+[ \t]*[Ii]mplementation/ { exit }
		/doc-off/,/doc-on/ { next }

		# skip license
		/^[\/][*][ \t]*miniboxtk[ \t]*-/,/^[ \t]*[*][\/]/ {
			if (!lic_printed) print "<!-- License: GNU LGPL2.1+ -->"
			lic_printed=1
			next
		}

		/\/[*]+[ \t]*API:/ {
			title=$0
			sub("^.*API:[ \t]*", "", title)
			sub("[*]+/", "", title)
			print "<h2>" quote(title) "</h2>"
			next
		}

		/\/[*][*][*]/ {
			print "<h3>" quote($0) "</h3>"
			next
		}


		{ print quote($0) }
		END { print "</pre>" }
	' < "$1"
	echo "$TAIL"
}

for h in $LIBMBTK/*.h
do
	bn=`basename $h`
	gen "$h" > $bn.html
done

touch gen.stamp
