#! /bin/sh
# compile script for the C programs used by FormCalc
# last modified 18 Sep 06 th

# If this compile script doesn't work on your system, check these things:
#
# a) You need gcc.  If that's not installed on your system, change all
#    "gcc"s to "cc"s, but have your fingers crossed: not all ccs will
#    compile MathLink programs without errors.  You may need to change
#    CFLAGS to e.g. relax strict ANSI compliance.
#
# b) You need mcc.  mcc is a script which compiles MathLink programs.
#    Such programs have the extension .tm.  If you have Mathematica,
#    you should also have mcc.  If mcc is not on the path, specify it
#    in the environment variable MCC, e.g.
#    setenv MCC /opt/mathematica/Executables/Linux/mcc

CC=${CC:-gcc}
export CC

MCC=${MCC:-mcc}

CFLAGS="${CFLAGS:--O3 -fomit-frame-pointer -ffast-math} $*"

for dir in /Applications/Mathematica*/Contents/MacOS \
           $HOME/Desktop/Mathematica*/Contents/MacOS ; do
  if [ -x "$dir/MathKernel" ] ; then
    PATH=$PATH:"$dir"
    break
  fi
done

set -- `MathKernel -run 'Print[$SystemID]; Exit[]' < /dev/null | tail -1`
if [ $# -ne 1 ] ; then
  echo "Cannot determine system type"
  exit 1
fi

[ -d $1 ] || mkdir $1
if [ $? -ne 0 ] ; then
  echo "Cannot create directory $1"
  exit 1
fi

echo "Compiling for system type $1"

make -f - << _EOF_ || exit 1
all: $1/ReadForm $1/ToForm $1/ToFortran $1/ReadData $1/reorder

$1/ReadForm: FormCalc/ReadForm.tm
	CC=tools/fcmcc $MCC $CFLAGS -o $1/ReadForm FormCalc/ReadForm.tm
	strip $1/ReadForm

$1/ToForm: FormCalc/ToForm.c
	$CC $CFLAGS -o $1/ToForm FormCalc/ToForm.c
	strip $1/ToForm

$1/ToFortran: FormCalc/ToFortran.c
	$CC $CFLAGS -o $1/ToFortran FormCalc/ToFortran.c
	strip $1/ToFortran

$1/ReadData: tools/ReadData.tm
	CC=tools/fcmcc $MCC $CFLAGS -o $1/ReadData tools/ReadData.tm
	strip $1/ReadData

$1/reorder: tools/reorder.c
	$CC $CFLAGS -o $1/reorder tools/reorder.c
	strip $1/reorder
_EOF_


# Second, make the util library.

cd drivers
trap "rm -f makefile util.a" 0 1 2 3 9 15
[ -f ../$1/util.a ] && mv -f ../$1/util.a .
./configure util.a
mv -f util.a ../$1/

