#!/bin/sh
#
#	printed-manual  -- produce a printed manual.
#
#	Usage:	cd /grass5.0/man	(for example)
#		setenv PRINTER foo	(optional)
#		sh utilities/printed-manual [option]
#
#	Options: print		- send output to a printer (default)
#		 source		- produce a file full of troff .so commands
#		 soelim		- produce portable troff source
#		 postscript	- produce PostScript output
#
#	Note that on some architectures, troff et al won't be found within
#	the rather limited PATH listed below; feel free to alter or remove
#	the line as appropriate.
#
#
PATH=/usr/ucb:/bin:/usr/bin ; export PATH
PRINTER=${PRINTER-oscar} ; export PRINTER
PRINTHOST=${PRINTHOST-zorro}
PSHOST=${PSHOST-melissa}

if [ "$1" != "" ]; then
    mode=$1
else
    mode=print
fi

index=man.index
tempfile1=/tmp/out1.$$
tempfile2=/tmp/out2.$$
rm -f $tempfile1 $tempfile2

outfile=/tmp/soelim.$$
outfile2=$HOME/manual.$$
rm -f $outfile $outfile2

#	First stage:
#	- collect names of all man pages in man1 and man2.
#	- ASSUME and REQUIRE that man page names be unique within these
#	  two chapters, i.e. there is no man1/foo and man2/foo !!!!
#	- sort the names
#	- use dirname etc. to create the first part of our soelim file

echo "Processing sections 1 and 2."

for file in man[12]/*
do
    basename $file >> $tempfile1
done
sort -u $tempfile1 > $tempfile2


#	Put six blank pages at the very beginning to make room
#	for table of contents, cover page, intro sections, etc.

echo ".pn +7"				>> $outfile
echo ".so utilities/man.version"	>> $outfile
echo ".so utilities/man.russ"		>> $outfile

for name in `cat $tempfile2`
do
    file=man[12]/$name
    dir=`dirname $file`

    echo ".rr ]Z"			>> $outfile
    if [ -f man1/$name ]; then
	echo ".nr ]Z 1"			>> $outfile
    else
	echo ".nr ]Z 2"			>> $outfile
    fi
    echo ".so $dir/.class-header"	>> $outfile
    echo ".so" $file			>> $outfile
    echo ".so $dir/.class-notice"	>> $outfile
done

#	Now we're done with the 'main' and 'alpha' man pages, and we
#	can proceed with the remaining sections.

#	Section 3 - shell scripts
echo "Processing section 3."

#	Skip three pages, for ch3intro
echo ".pn +3"				>> $outfile
echo ".so man3/.class-header"		>> $outfile

for name in man3/*
do
    echo ".rr ]Z"			>> $outfile
    echo ".nr ]Z 3"			>> $outfile
    echo ".so $name"			>> $outfile
    echo ".so man3/.class-notice"	>> $outfile
done


#	Section 5 - Miscellaneous and File Formats
echo "Processing section 5."

echo ".pn +9"				>> $outfile
echo ".so man5/.class-header"		>> $outfile

for name in man5/*
do
    echo ".rr ]Z"			>> $outfile
    echo ".nr ]Z 5"			>> $outfile
    echo ".so $name"			>> $outfile
    echo ".so man5/.class-notice"	>> $outfile
done

#	Final stage ... run troff on the entire mess."
echo "Starting troff processes -- output mode is $mode."

case $mode in
    source)
	echo "OK -- output is in $outfile."
	exit 0 ;;
    soelim)
	soelim $outfile > man.out
	echo "Saved output in man.out" ;;
    troff)
	soelim $outfile | tbl -TX | troff -t 2> $index > man.out
	sort $index > /tmp/$index.$$
	echo ".so utilities/man.russ"		> $index
	echo ".PX"				>> $index
	/bin/cat /tmp/$index.$$			>> $index
	/bin/rm /tmp/$index.$$
	echo "Saved index in $index"
	echo "Saved output in man.out" ;;
    postscript|ps)
	soelim $outfile | tbl -TX | troff -t \
	    | rsh $PSHOST '/export/newsprint/bin/pl --e /bin/cat  --r :opost' \
	    > man.ps
	echo "Saved output in man.ps" ;;
    print|*)
	soelim $outfile | tbl -TX | troff -t > $outfile2
	echo "Printing to $PRINTER from $PRINTHOST."
	lpr -s -t $outfile2 ;;
esac
    

echo "Done."
rm -f $tempfile1 $tempfile2 $outfile
exit 0
