#! /bin/csh
#
#               Build all of the "tools" example programs
#
# Look at the arguments.  Try to identify -g -O -Opg and set the variables
# BOPT and PROFILE appropriately
#
set BOPT    = "O"
set TOLIB   = "libs/libsO"
set PROFILE = ""
if ($#argv > 0) then
switch ("$1")
case "-g":
  set BOPT = "g"
  set TOLIB = "libs/libsg"
  shift argv
  breaksw
case "-O":
  set BOPT = "O"
  set TOLIB = "libs/libsO"
  shift argv
  breaksw
case "-Opg":
  set BOPT = "O"
  set TOLIB = "libs/libsOpg"
  set PROFILE = "pg"
  shift argv
  breaksw
case "-help":
  echo "$0 [ -g | -O | -Opg ] [ARCH value] other_options"
  echo "Builds the examples for debugging, optimized running, or profiling"
  echo "with optimized code.  If the first argument does not match, an"
  echo "optimized (-O) code is generated."
  exit 0
default:
endsw
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
#
if ($?TOOLSDIR == 0) then
    setenv TOOLSDIR  `pwd`
    if (! -e $TOOLSDIR/tools.h) then
	echo "setenv TOOLSDIR before running buildex"
        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) then
  set ARCH = "`./bin/tarch`"
endif
#
# Check for the libraries
set TOLIB = "$TOLIB/$ARCH"
if (! -e $TOLIB/tools.a || ! -e $TOLIB/system.a) then
    echo "Libraries not available for the selected architecture"
    echo "Please do a"
    echo "        bin/install $ARCH -libs $BOPT$PROFILE"
    echo "and then re-run $0"
    exit(1)
endif
#
#
set BASEMAKE = make
set LIBBUILD = libbuild
set PRUNE    = " "
if ($ARCH == "symmetry") set BASEMAKE = gnumake
if ($ARCH == "tc2000") then
    set BASEMAKE = /usr/local/bin/gnumake
    set LIBBUILD = lib
endif
set HASPRUNE    = `bin/findprune $ARCH`

set MAKE = "$BASEMAKE ARCH=$ARCH BOPT=$BOPT PROFILE=$PROFILE $argv"
#
# Save the arguments in a file where we can compare with them
# build examples
#
if ($HASPRUNE == 1) then
    find . \( -name libs -prune -o -name man -prune -o \
	      -name '*.old' -prune \) \
	-o -type d -exec $TOOLS/bin/mkex $TOOLS \{\} $ARCH $MAKE  \;
else 
    find . -type d -exec $TOOLS/bin/mkex $TOOLS \{\} $ARCH $MAKE  \;
endif
exit 0
