#!/bin/bash

howto=content/html/howto.content

print_top()
{
	echo "<TABLE>
<TR><TD WIDTH="10%"></TD>
<TD WIDTH="60%">
</TD><TD WIDTH="10%"></TD></TR>

<TR>
<TD></TD>
<TD ALIGN="LEFT">
<P>
<FONT COLOR="#99CCFF">
<pre>" >> "$1" 
}

print_bottom()
{
	echo "</pre>
</FONT>
</P>

</TD>
</TR>

</TR>
</TABLE>" >> "$1" 
}

rm "$howto"
print_top "$howto"
cat ../README >> "$howto"
echo >> "$howto" 
echo >> "$howto" 
echo "For the benefit of the website, the man page now follows." >> "$howto"
echo >> "$howto" 
echo >> "$howto" 
man ../manpages/burp.8 | col -b >> $howto
print_bottom "$howto"

mkdir -p html/txt
cp -v ../docs/*.txt html/txt

go_for_it()
{
	i=$1
	case "$i" in
		*.content)
			file=${i%.content}
			area=file
			;;
		*.html)
			b=$(basename $i)
			cp $i $root/$2/$b
			echo $root/$2/$b
			return
			;;
		*)
			file=${i%.*}
			area=${i#*.}
			;;
	esac
	h=$root/$2/$file.html
	globhuman=
	cat $template | while read line ; do
		if [ "${line:0:1}" = "@" ] ; then
			k=${line#@}
			name=${k%@*}
			name=${name%@*}
			human=${k#*@}
			human=${human%@*}
			header=${line##*@}
			if [ "$k" = "header" ] ; then
				if [ -n "$globhuman" ] ; then
					echo "<H1 ALIGN=CENTER>$globhuman</H1>" >> $h
				fi
			elif [ "$name" = "content" ] ; then
				cat $i >> $h
			elif [ "$name" = "$file" ] ; then
				if [ -n "$human" ] ; then
					echo "<TD ALIGN=CENTER><IMG BORDER=0 SRC=\"/images/"$name"sel.jpg\" ALT=\"$human\"></TD>" >> $h
				fi
				globhuman=$header
			elif [ "$name" = "wiki" ] ; then
				echo "<TD ALIGN=CENTER><A HREF=\"/wiki/index.php\"><IMG BORDER=0 SRC=\"/images/$name.jpg\" ALT=\"$human\"></A></TD>" >> $h
			else
				echo "<TD ALIGN=CENTER><A HREF=\"/$name.html\"><IMG BORDER=0 SRC=\"/images/$name.jpg\" ALT=\"$human\"></A></TD>" >> $h
			fi
		else
			echo "$line" >> $h
		fi
	done
	echo $h
}

recurse()
{
	dir=$1
	for i in * ; do
		if [ -d "$i" ] ; then
			local x=$i
			rm -rf $root/$i/*.html
			mkdir -p $root/$i
			cd $i
			recurse $i
			cd ..
			mkdir -p $root/html/$x
			mv $root/$x/*.html $root/html/$x
			if ! [ $x = "html" ] ; then rmdir $root/$x ; fi
		else
			case "$i" in
				*.content|*.howto|*.faq|*.html|*.about|*.contact)
					go_for_it "$i" "$dir"
					;;
				*.template|*.jpg|*.rtf|*.png)
					;;
				"*")
					echo "empty directory."
					;;
				*)
					echo "Don't know what to do with '$i'"
					;;
			esac
		fi
	done
}

root=$(pwd)
template=$(pwd)/content/main.template
cd content
recurse html
mv $root/html/html/* $root/html
rmdir $root/html/html

