#! /bin/csh
#
#            Build all of the reference pages for the "tools"
#
if ($#argv > 0) then
    if ("$1" == "-help") then
        echo "$0 [ arch ]"
	echo "Build the reference manual pages for the tools package."
    endif
endif
#
alias cd 'cd'
#
# 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
#
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) then
    (cd doc ; /bin/rm -f doctext doc2lt ; make BOPT=$OOPT ARCH=$LARCH doctext doc2lt >& /dev/null )
    if (! -e doc/doctext) then
        echo "Could not build the program to make reference manual pages"
        exit(0)
    endif
endif
#
#
# Build reference pages
#
if (! -e ref) mkdir ref
#
set HASPRUNE    = `bin/findprune $LARCH`
#
if ($HASPRUNE == 1) then
    find . \( -name libs -prune -o -name man -prune -o \
	      -name '*.old' -prune -o -name ref -prune \) -o \
              -type d -exec $TOOLS/bin/mkref $TOOLS \{\} \;
else
    find . -type d -exec $TOOLS/bin/mkref $TOOLS \{\} \;
endif
#
if (! -e docs/ref) mkdir docs/ref
# Form the tex input files:
# Not all csh's have pushd (ipsc2 is one), so we do this the hard way
set lastdir = `pwd`
set LS = "ls -1"
# Older IRIX OS's don't support "ls -1"
if ("$LARCH" == "IRIX") then
   set VERSION = `/bin/uname -r`
   set V2 = $VERSION:r
   set V2 = $V2:r
   if ("$V2" == "4") set LS = "ls"
endif
foreach dir (ref/ref?)
    cd $dir
    # Note that -1 is not required for some SYS V varients.
    $(LS) > .tmp
    set REF = $dir:t
    echo $dir | \
    awk '{if (NR == 1) { REF = $1 ;}else {printf("\\input ../%s/%s\n", REF, $1)}}' - .tmp > $TOOLS/docs/ref/$REF.tex
    /bin/rm -f .tmp
    cd $lastdir
end
