#!/bin/sh
processline()
{
	echo "- $1 -"
	echo "=============================================================================================="
	name="$1"
	anchors=""
	echo "$1.txt $*" >> anchors
	while [ "$1" != "" ] ; do
		echo "s!\[help:$1\]!$name.html!g" >> htmllinks.sed
		echo "s!\[help:$1\]!help:anchor=$name bookID=XaoS Help!g" >> applehelplinks.sed
		shift
	done
	# I changed "0" to "$" because my sed
	# doesn't want to understand "0".
	# -- Zoltan
	echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE>XaoS online help</TITLE>
</HEAD>
<BODY>' > $name.html
	cat $mypath/xaos.hlp | sed '1,/%'"$name"'/d;/^%/,$ d' | sed \
'1,2s_<head>\([^<]*\)</head>_<H1>\1</H1>_g
s_<head>_<center><H2>_g
s_</head>_</H2></center>_g
s_<emph>_<strong>_g
s_<submenu>__g
s_<menuhead>__g
s_</menuhead>__g
s_<mainmenu>__g
s_<menuitems>__g
s_</menuitems>__g
s_<main>__g
s_</main>__g
s_<math>__g
s_</math>__g
s_<notex>__g
s_</notex>__g
s_<node.*>__g
s_</emph>_</strong>_g
s_<tt>_<code>_g
s_</tt>_</code>_g
s_<tutor \([^>]*\)>_<A HREF="[tutor:\1]">_g
s_</tutor>_</A>_g
s_<a \([^>]*\)>_<A HREF="\1">_g
s/'\'\''/"/g
s/``/"/g' >> $name.html
        if [ $2 ]; then
           echo $* >> aliases.conf
	fi
	echo "</BODY></HTML>" >> $name.html
	cat -n $name.html
	tidy -utf8 -asxml -qm $name.html
	$mypath/xhtml2rest.py $name.html > $name.txt
	rm $name.html
}
process()
{
	while read line ; do
		eval "processline $line"
	done
}
mypath=`dirname $0`
rm htmllinks.sed
rm applehelplinks.sed
rm anchors
grep "^%" $mypath/xaos.hlp | sed '/%%/d;s/%//g' | process
