#! /bin/csh
#
#               Build all of the "tools"
#
# Look at the arguments.  Try to identify -g -O -Opg and set the variables
# BOPT and PROFILE appropriately
#
# Unset commands that we will be using in case they've been aliased..
alias cd 'cd'
#
set BOPT    = "O"
set PROFILE = ""
if ($#argv > 0) then
switch ("$1")
case "-g":
  set BOPT = "g"
  shift argv
  breaksw
case "-O":
  set BOPT = "O"
  shift argv
  breaksw
case "-Opg":
  set BOPT = "O"
  set PROFILE = "pg"
  shift argv
  breaksw
case "-help":
  echo "$0 [ -g | -O | -Opg ] [ARCH value] [ libbuild | liball ] other_options"
  echo "Builds the libraries for debugging, optimized running, or profiling"
  echo "with optimized code.  If the first argument does not match, an"
  echo "unoptimized code is generated. ARCH value is used to specify the"
  echo "architecture to use.  libbuild or liball is used to a regular build"
  echo "or a complete, new installation."
  echo 'Other_options are passed to make.  A useful option is "COMM=p4"'
  echo ""
  echo "If you want to pass multiple arguments as a value of a name,"
  echo "enclose the whole thing in single quotes.  For example:"
  echo "$0 -O libbuid 'OPT="'"-DDEBUG_ALL -DDEBUG_TRACEBACK"'"'"
  exit 0
default:
endsw
endif
#
# Check that TOOLSDIR exists, or that the default is ok
if ($?TOOLSDIR == 0) then
    setenv TOOLSDIR  `pwd`
    if (! -e $TOOLSDIR/tools.h) then
	echo "setenv TOOLSDIR before running buildlib or install"
        exit 1
    endif
endif
set TOOLS = $TOOLSDIR 
#
#
# Look for ARCH name as lead argument
#
#echo $argv
if ($#argv > 1 && "$1" == "ARCH") then
    shift argv
    set ARCH = $1
    shift argv
else
    if ($?ARCH == 0) set ARCH = "`arch`"
endif
set LIBBUILD = libbuild
if ($#argv > 0 && ("$1" == "liball" || "$1" == "libbuild")) then
    # echo "Setting libbuild to $1"
    set LIBBUILD = $1
    shift argv
endif
#
set HASPRUNE    = `bin/findprune $ARCH`

if (! -e libs)                         mkdir libs
if (! -e libs/libs$BOPT$PROFILE)       mkdir libs/libs$BOPT$PROFILE
if (! -e libs/libs$BOPT$PROFILE/$ARCH) mkdir libs/libs$BOPT$PROFILE/$ARCH
#
# These have to be (partially ordered) since some test programs use other
# libraries.  Also, the arguments are echoed to .build (eventually, we'll
# use these to see if the library should be removed before doing the make).
# Some machines have hopeless make's.
#
set BASEMAKE = make
if ($ARCH == "symmetry") set BASEMAKE = gnumake
if ($ARCH == "tc2000") then
    set BASEMAKE = /usr/local/bin/gnumake
    if ($LIBBUILD == "libbuild") set LIBBUILD = lib
endif
#
# Check for use liball instead of libbuild
set MAKE = "$BASEMAKE ARCH=$ARCH BOPT=$BOPT PROFILE=$PROFILE $argv"
set COMM = ""
if ("$1" == "COMM=p4") then
    set COMM=p4
else if ("$1" == "COMM=pvm") then
    set COMM=pvm
else if ("$1" == "COMM=picl") then
    set COMM=picl
endif
#
# Check for programs use X11, then check for the directories.  If
# X11 is not available, then exit.
#grep -s basex11 *.[chf]
#if ($status == 0) then 
#    if (! -e /usr/include/X11) exit 0
#endif
#
# Build the libraries.  We don't do any ranlibs here
if ($HASPRUNE == 1) then
    find . \( -name libs -prune -o -name man -prune -o -name ref -prune -o \
              -name '*.old' -prune -o -name docs -prune \) -o -type d \
            -exec $TOOLS/bin/mklib $TOOLS \{\} $ARCH $MAKE $LIBBUILD \;
else
    find . -type d -exec $TOOLS/bin/mklib $TOOLS \{\} $ARCH $MAKE $LIBBUILD \;
endif
#
# Ranlib the libraries if necessary.  This isn't correct yet, since we
# need to specify the system ranlib.
#
# Not all csh's have pushd (ipsc2 is one), so we do this the hard way
set lastdir = `pwd`
cd libs/libs$BOPT$PROFILE/$ARCH
foreach FILE (*.a)
    if (-e /bin/ranlib) then
	/bin/ranlib $FILE
    else if (-e /usr/bin/ranlib) then
	/usr/bin/ranlib $FILE
    endif	
end
cd $lastdir
exit 0


