#!/bin/sh

if test $# -ge 2 -o \( $# -eq 1 -a ! \( "$1" = "-remove" -o "$1" = "-new" \) \)
then
    echo "usage: $0 [-remove | -new]";
    echo "purpose: generate man pages for ug";
    echo "Options are";
    echo " -remove: remove the old man pages";
    echo " -new: remove old and generate new man 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/man/*/* 
	do
    	rm $i
	done
	if test -d $UGROOT/../sc/man
	then
		rm $UGROOT/../sc/man/*/*
	fi
	if test -d $UGROOT/../diff2d/man
	then
		rm $UGROOT/../diff2d/man/*/*
	fi
	if test -d $UGROOT/../fe/man
	then
		rm $UGROOT/../fe/man/*/*
	fi
	if test -d $UGROOT/../ns/man
	then
		rm $UGROOT/../ns/man/*/*
	fi
    echo "$0: old man pages removed!"
fi


#if no parameter is specified make the specified module
if test $# -eq 0 -o "$1" = "-new"
then
	# generate ug man pages
	echo "Generating ug-pages"
	cd $UGROOT;
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug *.doc

	# generate dev man pages
	echo "Generating dev-pages"
	cd $UGROOT/dev;
	doctext -quotefmt -mpath $UGROOT/man/man4 -ext 4 -heading ug/dev *.c *.h
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/dev *.doc

	# generate gm man pages
	echo "Generating gm-pages"
	cd $UGROOT/gm;
	doctext -quotefmt -mpath $UGROOT/man/man5 -ext 5 -heading ug/gm *.c *.h
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/gm *.doc

	# generate uggraph man pages
	echo "Generating uggraph-pages"
	cd $UGROOT/graphics/uggraph;
	doctext -quotefmt -mpath $UGROOT/man/man6 -ext 6 -heading ug/graphics/uggraph *.c *.h
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/graphics/uggraph *.doc

	# generate low man pages
	echo "Generating low-pages"
	cd $UGROOT/low;
	doctext -quotefmt -mpath $UGROOT/man/man7 -ext 7 -heading ug/low *.c *.h
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/low *.doc

	# generate dom man pages
	echo "Generating dom-pages"
	cd $UGROOT/dom;
	doctext -quotefmt -mpath $UGROOT/man/man4 -ext 5 -heading ug/dom *.c *.h
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/dom *.doc

	# generate dom/std man pages
	echo "  Generating std-pages"
	cd $UGROOT/dom/std;
	doctext -quotefmt -mpath $UGROOT/man/man5 -ext 5 -heading ug/dom/std *.c *.h
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/dom/std *.doc

	# generate np man pages
	echo "Generating np-pages"
	cd $UGROOT/np;
	doctext -quotefmt -mpath $UGROOT/man/man5 -ext 5 -heading ug/np *.c *.h
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/np *.doc
	echo "  Generating udm-pages"
	cd $UGROOT/np/udm;
	doctext -quotefmt -mpath $UGROOT/man/man5 -ext 5 -heading ug/np/udm *.c *.h
	echo "  Generating algebra-pages"
	cd $UGROOT/np/algebra;
	doctext -quotefmt -mpath $UGROOT/man/man5 -ext 5 -heading ug/np/algebra *.c *.h
	echo "  Generating procs-pages"
	cd $UGROOT/np/procs;
	for i in *.c *.h
	do
		doctext -quotefmt -ext 1 -heading ug/np/procs $i
		mv [a-z][a-z].1 $UGROOT/man/man1; 
		mv [a-z][a-z][a-z]*.1 $UGROOT/man/man1; 
		for j in *.1
		do 
			mv $j $UGROOT/man/man8/`echo $j|sed 's/\.1/\.8/'`;
		done
	done

	# generate ui man pages
	echo "Generating ui-pages"
	cd $UGROOT/ui;
	for i in *.c *.h
	do
		if test "$i" = "commands.c"
		then
	  		doctext -quotefmt -ext 1 -heading ug/ui $i
			mv [a-z]*.1 $UGROOT/man/man1;
			for j in *.1
			do 
				mv $j $UGROOT/man/man8/`echo $j|sed 's/\.1/\.8/'`;
			done
		else
	  		doctext -quotefmt -mpath $UGROOT/man/man9 -ext 9 -heading ug/ui $i
		fi
	done
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading ug/ui *.doc

	#generate administration man pages
	echo "Generating administration-pages"
	if test -d $UGROOT/..
	then
		cd $UGROOT/..
		doctext -quotefmt -mpath $UGROOT/man/man2 -ext 2 -heading admin *.doc
	fi
	cd $UGROOT/bin
	doctext -quotefmt -mpath $UGROOT/man/man2 -ext 2 -heading admin *.doc
	cd $UGROOT/doc
	doctext -quotefmt -mpath $UGROOT/man/man2 -ext 2 -heading admin *.doc

	# generate man page for simple
	cd $UGROOT/../simple
	echo "Generating simple-pages"
	doctext -quotefmt -mpath $UGROOT/man/man3 -ext 3 -heading simple *.doc

	#generate man pages for diff2d
	cd $UGROOT/../diff2d
	echo "Generating diff2d-pages"
	doctext -quotefmt -mpath $UGROOT/../diff2d/man/mana -ext a -heading diff2d *.doc
	cd $UGROOT/../diff2d/pclib
	doctext -quotefmt -mpath $UGROOT/../diff2d/man/manb -ext b -heading diff2d/pclib *.c *.h
	cd $UGROOT/../diff2d/appl
	doctext -quotefmt -mpath $UGROOT/../diff2d/man/manb -ext b -heading diff2d/appl *.c *.h

	#generate man pages for ns
	cd $UGROOT/../ns
	echo "Generating ns-pages"
	doctext -quotefmt -mpath $UGROOT/../ns/man/mana -ext a -heading ns *.doc
	cd $UGROOT/../ns/pclib
	doctext -quotefmt -mpath $UGROOT/../ns/man/mane -ext e -heading ns/pclib *.c *.h
	cd $UGROOT/../ns/appl2d
	doctext -quotefmt -mpath $UGROOT/../ns/man/mane -ext e -heading ns/appl2d *.c *.h
	cd $UGROOT/../ns/tpclib
	doctext -quotefmt -mpath $UGROOT/../ns/man/mane -ext e -heading ns/tpclib *.c *.h
	cd $UGROOT/../ns/tappl2d
	doctext -quotefmt -mpath $UGROOT/../ns/man/mane -ext e -heading ns/tappl2d *.c *.h
	cd $UGROOT/../ns/appl3d
	doctext -quotefmt -mpath $UGROOT/../ns/man/mane -ext e -heading ns/appl3d *.c *.h

	#generate man pages for sc
	cd $UGROOT/../sc
	echo "Generating sc-pages"
	doctext -quotefmt -mpath $UGROOT/../sc/man/mana -ext a -heading sc *.doc
	cd $UGROOT/../sc/pclib
	doctext -quotefmt -mpath $UGROOT/../sc/man/manc -ext c -heading sc/pclib *.c *.h
	cd $UGROOT/../sc/appl2d
	doctext -quotefmt -mpath $UGROOT/../sc/man/manc -ext c -heading sc/appl2d *.c *.h
	cd $UGROOT/../sc/appl3d
	doctext -quotefmt -mpath $UGROOT/../sc/man/manc -ext c -heading sc/appl3d *.c *.h

	#generate man pages for fe
	cd $UGROOT/../fe
	echo "Generating fe-pages"
	doctext -quotefmt -mpath $UGROOT/../fe/man/mana -ext a -heading fe *.doc
	cd $UGROOT/../fe/pclib
	doctext -quotefmt -mpath $UGROOT/../fe/man/mand -ext d -heading fe/pclib *.c *.h
	cd $UGROOT/../fe/appl
	doctext -quotefmt -mpath $UGROOT/../fe/man/mand -ext d -heading fe/appl *.c *.h

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

