#!/bin/bash

# Defaults for Debian
CNV=`pwd`/contrib/latex2html
LYX=lyx

# Sanity checks
if [ ! -d doc/core ]; then
	echo "Can't find usable core documentation structure - aborting"
	exit 1
fi

# Commands present?
for cmd in $CNV $LYX; do
	if [[ ! `which $cmd` ]]; then
		echo "Can't find executable for '$cmd' - aborting"
		exit 2
	fi
done

# Create guide.xml
cat << EOF > doc/guide.xml
<?xml version="1.0"?>
 	<!-- 
		WARNING:
		This file is automatically generated by update-online-help.
		If you want to add entries, use doc/core/guide.xml or doc/plugins/"Appropriate Plugin Directory"/guide.xml.
		Then execute update-online-help to merge them into this file.
	-->
	
	<!--
		This xml file specifies which class is documented in which help file.

		If isset ( $_SESSION['current_class_for_help'] ) then open the helpfile which is  
		specified for this class below.
	-->
	
	<!--
		<ENTRY NAME="class name" VALUE="displayed text" PATH="path to helpfiles" FILE="path to htmlfile" />
		 Leave blank to display message "There is no helpfile specified for this class."	
	-->
<ENTRIES>
EOF

echo -en "Generating doc/guide.xml..."
# core guide.xml
pushd . &> /dev/null
cd doc/core
cat < guide.xml >> ../guide.xml
popd &> /dev/null

# plugins guide.xml
pushd . &> /dev/null
if [ -d doc/plugins ]; then
	cd doc/plugins
	for plugin in *; do
		[ -r $plugin/guide.xml ] && cat < $plugin/guide.xml >> ../guide.xml
	done
fi
popd &> /dev/null

echo -en "</ENTRIES>\n" >> doc/guide.xml
sed -i s/"[ \t][ \t]*"/" "/g doc/guide.xml
echo -en " done.\n"

# Core help
echo -en "Processing core\n"
pushd . &> /dev/null
cd doc/core
for lang in *; do
	[ ! -d "$lang" ] && continue

	pushd . &> /dev/null
	echo -en "\tProcessing language $lang...\n"
	cd $lang/lyx-source

	for source in $(find . -name \*.lyx -exec basename {} \;); do
		echo -en "\t\tProcessing $source..."
		echo -en " tex"
		$LYX -e latex $source &> /dev/null
		d=../html/${source%%\.*}/
		[ -d $d ] || mkdir -p $d && rm -r $d/* &> /dev/null
		echo -en " html"
		$CNV -no_navigation -dir $d ${source%%\.*}.tex &> /dev/null
		echo -en " cleanup"
		rm images/*.eps &> /dev/null
		rm ${source%%\.lyx}.tex &> /dev/null
		echo -en " done.\n"
		continue
	done
	popd &> /dev/null
done

popd &> /dev/null

# Plugin help
pushd . &> /dev/null
if [ -d doc/plugins ]; then
	cd doc/plugins

	for plugin in *; do
		pushd . &> /dev/null
		echo -en "Processing $plugin\n"
		cd $plugin
		for lang in *; do
			[ ! -d "$lang" ] && continue

			pushd . &> /dev/null
			echo -en "\tProcessing language $lang...\n"
			cd $lang/lyx-source

			for source in $(find . -name \*.lyx -exec basename {} \;); do
				echo -en "\t\tProcessing $source..."
				echo -en " tex"
				$LYX -e latex $source &> /dev/null
				d=../html/${source%%\.*}/
				[ -d $d ] || mkdir -p $d && rm -r $d/* &> /dev/null
				echo -en " html"
				$CNV -no_navigation -dir $d ${source%%\.*}.tex &> /dev/null
				echo -en " cleanup"
				rm images/*.eps &> /dev/null
				rm ${source%%\.lyx}.tex &> /dev/null
				echo -en " done.\n"
				continue
			done
			popd &> /dev/null
		done
		popd &> /dev/null
	done

fi
popd &> /dev/null
echo
exit 0
