#!/bin/sh

if test $# -ge 2 -o \( $# -eq 1 -a ! \( "$1" = "-remove" -o "$1" = "-new" \) \)
then
	echo "usage: $0 [-remove | -new]";
	echo "purpose: generate latex pages for ug";
	echo "Options are";
	echo " -remove: remove the old latex pages";
	echo " -new: remove old and generate new latex pages";
	exit 1;
fi

if test "x$UGROOT" = "x"
then
	echo "$0: to use $0 set shell environment variable UGROOT!";
	exit 1;
fi

if test "$1" = "-remove" -o "$1" = "-new"
then
	for i in $UGROOT/doc/ug3/*/*.3 $UGROOT/doc/ug3/*/*/*.3
	do
		if test -f $i
		then
			rm $i;
		fi
	done
	echo "$0: old latex pages removed!"
fi

#if no parameter is specified make the specified module
if test $# -eq 0 -o "$1" = "-new" 
then
	#generate ug man pages
	cd $UGROOT;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug -heading ug -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug -quotefmt *.doc

	# generate dev man pages
	cd $UGROOT/dev;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/dev -heading ug/dev -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/dev -quotefmt *.doc

	# generate dev man pages
	cd $UGROOT/dev/xif;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/dev -heading ug/dev/xif -quotefmt $i
	done

	# generate dev man pages
	cd $UGROOT/dev/sif;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/dev -heading ug/dev/sif -quotefmt $i
	done

	# generate dev man pages
	cd $UGROOT/dev/ps;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/dev -heading ug/dev/ps -quotefmt $i
	done

	# generate gm man pages
	cd $UGROOT/gm;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/gm -heading ug/gm -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/gm -quotefmt *.doc

	# generate dom man pages
	cd $UGROOT/dom;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/dom -heading ug/dom -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/dom -quotefmt *.doc

	# generate dom/std man pages
	cd $UGROOT/dom/std;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/dom/std -heading ug/dom/std -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/dom/std -quotefmt *.doc

	# generate simple man pages
	cd $UGROOT/../simple;
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading simple -quotefmt *.doc

	# generate graph man pages
	cd $UGROOT/graphics;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/graphics -heading ug/graphics -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/graphics -quotefmt *.doc

	# generate graph man pages
	cd $UGROOT/graphics/uggraph;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/graphics -heading ug/graphics/uggraph -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/graphics/uggraph -quotefmt *.doc

	# generate low man pages
	cd $UGROOT/low;
	for i in *.c *.h
	do
		doctext -latex -mpath $UGROOT/doc/ug3/ug/low -heading ug/low -quotefmt $i
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/low -quotefmt *.doc

	# generate np man pages
	cd $UGROOT/np;
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/np -quotefmt *.doc
	doctext -latex -mpath $UGROOT/doc/ug3/ug/np -heading ug/np -quotefmt *.c *.h
	cd $UGROOT/np/udm;
	doctext -latex -mpath $UGROOT/doc/ug3/ug/np -heading ug/np/udm -quotefmt *.c *.h
	cd $UGROOT/np/algebra;
	doctext -latex -mpath $UGROOT/doc/ug3/ug/np -heading ug/np/algebra -quotefmt *.c *.h
	cd $UGROOT/np/procs;
	for i in *.c *.h
	do
		doctext -latex -heading ug/np/procs $i
		mv [a-z][a-z].3 $UGROOT/doc/ug3/ug/commands;
		mv [a-z][a-z][a-z]*.3 $UGROOT/doc/ug3/ug/commands;
		for j in *.3
		do 
			mv $j $UGROOT/doc/ug3/ug/np
		done
	done

	# generate ui man pages
	cd $UGROOT/ui;
	for i in *.c *.h
	do
		if test "$i" = "commands.c"
		then
	  		doctext -latex -heading ug/ui -quotefmt $i
			mv [a-z]*.3 $UGROOT/doc/ug3/ug/commands;
			for j in *.3
			do 
				mv $j $UGROOT/doc/ug3/ug/ui;
			done
		else
	  		doctext -latex -mpath $UGROOT/doc/ug3/ug/ui -heading ug/ui -quotefmt $i
		fi
	done
	doctext -latex -mpath $UGROOT/doc/ug3/ug/overview -heading ug/ui -quotefmt *.doc

	#generate administration man pages
	if test -d $UGROOT/..
	then
		cd $UGROOT/..
		doctext -latex -mpath $UGROOT/doc/ug3/ug/admin -heading admin -quotefmt *.doc
	fi
	cd $UGROOT/bin
	doctext -latex -mpath $UGROOT/doc/ug3/ug/admin -heading admin -quotefmt *.doc
	cd $UGROOT/doc
	doctext -latex -mpath $UGROOT/doc/ug3/ug/admin -heading admin -quotefmt *.doc

	#generate man pages for diff2d
	cd $UGROOT/../diff2d
	doctext -latex -mpath $UGROOT/doc/ug3/diff2d/overview -heading diff2d -quotefmt *.doc
	cd $UGROOT/../diff2d/pclib
	doctext -latex -mpath $UGROOT/doc/ug3/diff2d/pclib -heading diff2d/pclib -quotefmt *.c *.h
	cd $UGROOT/../diff2d/appl
	doctext -latex -mpath $UGROOT/doc/ug3/diff2d/appl -heading diff2d/appl -quotefmt *.c *.h

	#generate man pages for fe
	cd $UGROOT/../fe
	doctext -latex -mpath $UGROOT/doc/ug3/fe/overview -heading fe -quotefmt *.doc
	cd $UGROOT/../fe/pclib
	doctext -latex -mpath $UGROOT/doc/ug3/fe/pclib -heading fe/pclib -quotefmt *.c *.h
	cd $UGROOT/../fe/appl
	doctext -latex -mpath $UGROOT/doc/ug3/fe/appl -heading fe/appl -quotefmt *.c *.h

	#generate man pages for ns
	cd $UGROOT/../ns
	doctext -latex -mpath $UGROOT/doc/ug3/ns/overview -heading ns -quotefmt *.doc
	cd $UGROOT/../ns/pclib
	doctext -latex -mpath $UGROOT/doc/ug3/ns/pclib -heading ns/pclib -quotefmt *.c *.h
	cd $UGROOT/../ns/appl2d
	doctext -latex -mpath $UGROOT/doc/ug3/ns/appl -heading ns/appl2d -quotefmt *.c *.h
	cd $UGROOT/../ns/appl3d
	doctext -latex -mpath $UGROOT/doc/ug3/ns/appl -heading ns/appl3d -quotefmt *.c *.h

	echo "$0: latex pages for ug created!";
	exit 0;
fi

