#! /bin/csh
#
# Build the fortran to c library for the documented routines 
#
set WARN="-nomsgs"
set BOPT=O
if ($#argv > 0) then
    if ("$1" == "-help") then
        echo "$0 [ arch [-g] ]"
        echo "Build the fortran interface library"
	echo "If arch is specified, build for that architecture (used"
	echo "for cross-compiling)."
	echo " "
	echo "Define the symbol TOOLSNOX as YES if the system that you"
	echo "are building for does not have X11 (for example, many "
	echo "Intel i860 installations)."
	echo " "
	echo "This script is invoked by the tools install procedure and"
	echo "normally should not need to be called separately."
	echo " "
	echo "The -g option builds versions of the interface libraries with"
	echo "the debugging switch; this can be useful when debugging the"
	echo "interfaces.  It is left off by default to reduce the size of"
	echo "the interface libraries."
        exit 0;
    endif
    set LARCH = "$1"
    set CARCH = "`bin/tarch`"
    if ("$2" == "-g") set BOPT=g
    if ("$3" == "-warn") set WARN=""
else 
    set LARCH = "`bin/tarch`"
    set CARCH = "$LARCH"
endif
if ($?LARCH == 0) then
    exit(0)
endif
if ($?TOOLSDIR == 0) then
    set TOOLSDIR = `pwd`
endif
set TOOLS = $TOOLSDIR
alias cd 'cd' 
#
# We need to be more careful here.  If this isn't built for the corrent
# executable, then we need to rebuild it.  Perhaps we should just always
# do that...
#
# CARCH is the architecture of the system to run the programs to 
# build the interface programs; in the case of cross-compilation, it may
# be different from the system being compiled for.
set OOPT = O
if (! -e libs/libsO/$CARCH/system.a) then
    if (-e libs/libsg/$CARCH/system.a) then
	set OOPT = g
    else if (-e libs/libsOpg/$CARCH/system.a) then
	set OOPT = Opg
    else
	echo "Tools libraries must be built before Fortran interface."
  	exit(1);
    endif
endif
(cd doc ; make ARCH=$CARCH clean ; make ARCH=$CARCH BOPT=$OOPT doc.o )
(cd c2fort ; make ARCH=$CARCH clean ; make ARCH=$CARCH BOPT=$OOPT )
if (! -e $TOOLSDIR/c2fort/bfort) then
    echo "Please build $TOOLSDIR/c2fort/bfort"
    exit 1
endif
set HASPRUNE    = `bin/findprune $CARCH`
#
# Build fortran - 2 - c source
#
if (! -e fort) mkdir fort
if (! -e fort/$LARCH) mkdir fort/$LARCH
#
# Clean out the object files and source files (some versions don't support
# the X11 graphics).
(cd fort ; /bin/rm -f *.[co] >& /dev/null )
if ($HASPRUNE == 1) then
    find . \( -name libs -prune -o -name man -prune -o -name ref -prune -o \
	      -name '*.old' -prune \) -o -type d \
              -exec $TOOLS/bin/mkfort $TOOLS \{\} $WARN \;
else
    find . -type d -exec $TOOLS/bin/mkfort $TOOLS \{\} $WARN \;
endif
#
# Get the correct leftovers
unalias cp
if (-e comm/comm.h && -e solvers/svctx.h) then
    cat c2fort/lftcomm.c c2fort/lftslv.c > fort/leftover.c
else if (-e comm/comm.h) then
    cp c2fort/lftcomm.c fort/leftover.c
else
    cp c2fort/lftslv.c  fort/leftover.c
endif
cp c2fort/makefort   fort/makefile
#
# Build the library
# Making the g versions consumes a lot of space; for example, on a 
# sun4, just the interface libraries were 3.2MB.
# But making the O versions eliminates the error messages that are built
# into the macro versions.  
(cd fort ; make ARCH=$LARCH BOPT=$BOPT )
if (-e $TOOLSDIR/libs/libs$OOPT/$LARCH/toolsp4.a) then
 (cd fort ; make ARCH=$LARCH BOPT=$BOPT COMM=p4 )
endif
if (-e $TOOLSDIR/libs/libs$OOPT/$LARCH/toolspvm.a) then
 (cd fort ; make ARCH=$LARCH BOPT=$BOPT COMM=pvm )
endif
# Clean out the object files, leaving the source files in case we want to 
# look at them.
(cd fort ; /bin/rm -f *.[o] >& /dev/null )

