#!/bin/sh

myloc="`echo $0 | sed 's/\/[^/]*$//'`/..";
cd "$myloc"
src="`pwd`"
target=$src/docs/generated/html

update=0;
search=0;
clean=0;
tree=0;
extract_all="EXTRACT_ALL = NO";
always_detailed_sec="ALWAYS_DETAILED_SEC = NO";
generate_treeview="GENERATE_TREEVIEW = NO";
searchengine="SEARCHENGINE = NO";
enabled_sections="ENABLED_SECTIONS += INTERNAL";
input="$src";
exclude="$input/TinyFTPD $input/ms $input/tools $input/contrib $input/deprecated $input/Shared/jpeg-6b $input/Motion/Spline.h $input/Motion/gvector.h $input/Motion/Path.h $input/SoundPlay/WAV.h $input/SoundPlay/WAV.cc $input/Wireless/ionetstream.h $input/Motion/Kinematics.h $input/Motion/Kinematics.cc $input/Motion/Geometry.h $input/Shared/Util.h";

while [ $# -gt 0 ] ; do
	case $1 in
		-a | --all) extract_all="EXTRACT_ALL = YES"; shift;;
		-u | --update) update=1; shift;;
		-d | --detailed) always_detailed_sec="ALWAYS_DETAILED_SEC = YES"; shift;;
		-t | --tree) tree=1; generate_treeview="GENERATE_TREEVIEW = YES"; shift;;
		-s | --search) search=1; searchengine="SEARCHENGINE = YES"; shift;;
		-r | --release) enabled_sections=""; shift;;
		-c | --clean) clean=1; shift;;
		*) target="$1"; shift;;
	esac;
done;

output_directory="`echo OUTPUT_DIRECTORY = $target | sed 's/\/[^/]*$//'`";
html_output="`echo $target | sed 's/.*\//HTML_OUTPUT = /'`";

cd "$input/docs" ;

echo INPUT = $input > doxygenbuildcfg;
echo $enabled_sections >> doxygenbuildcfg;
echo EXCLUDE = $exclude >> doxygenbuildcfg;
echo $output_directory >> doxygenbuildcfg;
echo $extract_all >> doxygenbuildcfg;
echo $always_detailed_sec >> doxygenbuildcfg;
echo $generate_treeview >> doxygenbuildcfg;
echo $html_output >> doxygenbuildcfg;
echo $searchengine >> doxygenbuildcfg;

cancelupdate=0;
if [ $update -ne 0 -a ! -d "$target" ] ; then
	printf "\`\`$target'' does not exist - rebuilding...\n";
	cancelupdate=1;
fi;

if [ $update -eq 0 -o $clean -ne 0 ] ; then
	printf "Cleaning previous build of \`\`$target''...";
	rm -rf ${target}/*.html;
	rm -rf ${target}/*.png;
	rm -rf ${target}/*;
	printf "done\n";
	if [ $update -ne 0 ] ; then
		update=0;
	fi;
fi;

if [ $cancelupdate -ne 0 ] ; then
	update=0;
fi;

printf "Building documentation for \`\`$input'' ...";
doxygen doxygencfg ;
printf "done\n";

if [ $update -eq 0 ] ; then
	printf "Copying additional documentation..."
	for x in ${src}/docs/html/* ; do
		printf "${x}, "
		if echo "$x" | grep -q /index.html\$ ; then
			if [ $tree -eq 0 ] ; then
				cp -f $x $target ;
			else
				cp -f $x ${target}/main.html ;
			fi;
		else
			if echo "$x" | grep -v -q /CVS\$ ; then
				cp -rf $x $target ;
			fi;
		fi;
	done;
  printf "${src}/docs/html/.htaccess..."
	cp -f "$src"/docs/html/.htaccess $target ;
#	rm -rf $target/CVS ;
	printf "done\n"
else
	if [ $tree -eq 0 ] ; then
		printf "Replacing generated index.html with docs/html/index.html..."
		cp -f "$src"/docs/html/index.html $target ;
	else
		printf "Replacing generated main.html with docs/html/index.html..."
		cp -f "$src"/docs/html/index.html $target/main.html ;
	fi;
	printf "done\n"
fi;

exit 0;