#! /bin/csh
#
#            Build all of the man pages for the "tools"
#
if ($#argv > 0) then
    if ("$1" == "-help") then
        echo "$0 [ arch ]"
	echo "Build the manual pages for the tools package.  Also builds"
	echo "the programs to generate the manual pages."
    endif
endif
#
# Make sure that we're at the top of the directory
if (! -e bmake) then
    echo "$0 must be run from the top of the tools directory"
    exit(0)
endif
#
# Unset commands that we will be using in case they've been aliased..
alias cd 'cd'
#
set LARCH = "`bin/tarch`"
if ($?LARCH == 0) then
    exit(0)
endif

if ($?TOOLSDIR == 0) then
    set TOOLSDIR = `pwd`
endif
set TOOLS = $TOOLSDIR 
set MANDIRS = "1 2 3 4 5 6 7 8"
#
# Actually BUILD the document program.  There is a potential problem here:
# if the executables exist, but aren't for this architecture, we may have
# a problem.  We should probably put the program in an executable tree
# somewhere (by architecture)
#
set OOPT = O
if (! -e libs/libsO/$LARCH/system.a) then
    if (-e libs/libsg/$LARCH/system.a) then
	set OOPT = g
    else if (-e libs/libsOpg/$LARCH/system.a) then
	set OOPT = Opg
    else
	echo "Tools libraries must be built before manual pages"
  	exit(1);
    endif
endif
if (! -e doc/doctext || ! -e doc/doc2lt) then
    (cd doc ; /bin/rm -f doctext doc2lt *.o ; make BOPT=$OOPT ARCH=$LARCH doctext doc2lt >& /dev/null )
    if (! -e doc/doctext) then
	# We try again, this time allowing the error messages to escape...
       (cd doc ; /bin/rm -f doctext doc2lt *.o ; make BOPT=$OOPT ARCH=$LARCH doctext doc2lt )
        echo "Could not build the program to make manual pages"
        exit(0)
    endif
endif
#
# Build manpages and summary files
#
if (! -e man) mkdir man
if (! -e docs/rsum) mkdir docs/rsum
#
# Add the names for the sections that xman will use.
# WARNING: Generic "man" implementations "know" which directories are used
# and don't look in any others!
#
/bin/rm -f man/mandesc
cat > man/mandesc <<.
r(r)Introduction
1(1)Sparse Matrix Routines
2(2)Vector routines
3(3)Simplified Solvers
4(4)Iterative Methods
5(5)High level Communications
6(6)Low level communications
7(7)System calls
8(8)Miscellaneous
b(b)BLAS
x(x)X Window System tools
n(n)Nonlinear Solvers
no default sections
.
#
/bin/rm -f $TOOLS/docs/rsum/rsum?.tmp >& /dev/null
set HASPRUNE    = `bin/findprune $LARCH`
if ($HASPRUNE == 1) then
    find . \( -name libs -prune -o -name man -prune -o \
	      -name '*.old' -prune \) -o -type d \
	      -exec $TOOLS/bin/mkman $TOOLS \{\} \;
else
    find . -type d -exec $TOOLS/bin/mkman $TOOLS \{\} \;
endif
#
# The introduction files are special
if ( ! -e $TOOLS/man/manr ) then
    mkdir $TOOLS/man/manr
endif
$TOOLS/doc/doctext -mpath $TOOLS/man/manr -ext r readme bin/readme
#
# Generate the summary files
/bin/rm -f $TOOLS/docs/rsum/rsum?.tex
foreach file ($TOOLS/docs/rsum/rsum?.tmp)
    sort $file > $file:r.tex
    /bin/rm -f $file
end
#
# Create the whatis database
# Note that this might not work, since the program that builds the whatis
# database uses undocumented assumptions about the form of a man page
# (rather than just looking for \n.SH NAME\n[.]*\n, it seems to look for
# \n.TH [.] \n.SH NAME\n[.]*\n.SH, so that if the next formatting
# command is NOT .SH after NAME, it will generate garbage.  This seems
# like a poor design.
unalias cd
if (-e /usr/etc/catman) then
    (cd man ; /usr/etc/catman -w -M . )
endif
