#! /bin/sh
# compile script for the C programs used by FormCalc
# last modified 25 Mar 13 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/Wolfram/Mathematica/5.2/Executables/mcc

CC=${CC:-gcc}

CXX=${CXX:-g++}

MCC=${MCC:-mcc}

CFLAGS="${CFLAGS:--O3 -fomit-frame-pointer -ffast-math -Wall -Wextra} $@"

SRC="${SRC:-`dirname $0`}"

mathcmd=math
exe=
shopt -s nullglob > /dev/null 2>&1
set --
case `uname -s` in
Darwin)
	mathcmd=MathKernel
	set -- /Applications/Mathematica*/Contents/MacOS \
	       $HOME/Desktop/Mathematica*/Contents/MacOS ;;
CYG*)
	exe=.exe
	export nodosfilewarning=1
	set -- "$ProgramW6432/Wolfram Research/Mathematica"/* \
               "$PROGRAMFILES/Wolfram Research/Mathematica"/* ;;
esac

test -z "$DEST" && {
  for dir in "$@" ; do
    path=`cd "$dir"; pwd`:"$path"
  done
  mathcmd="`PATH=\"$PATH:$path\" which $mathcmd`"

  set -- `"$mathcmd" -run 'Print[$SystemID]; Exit[]' < /dev/null | tr '\r' ' ' | tail -1`

  test $? -ne 1 || {
    echo "Cannot determine Mathematica's system ID using $mathcmd" 1>&2
    exit 1
  }

  DEST="$1"
}

case "$DEST" in
Please)
	cat << _EOF_
I can't seem to run the Mathematica Kernel.

Maybe you have a buggy Mathematica installation or you're
out of licenses.

Please make sure the Mathematica Kernel can be run without
additional flags (e.g. for the location of the Mathematica
password file) and re-run $0.

_EOF_
	exit 1 ;;
Linux | MacOSX-x86)
	CFLAGS="$CFLAGS -m32"
	MCFLAGS="$MCFLAGS -b32" ;;
Linux-x86-64 | MacOSX-x86-64)
	CFLAGS="$CFLAGS -m64"
	MCFLAGS="$MCFLAGS -b64" ;;
esac

test -d "$DEST" || mkdir "$DEST" || {
  echo "Cannot create directory $DEST"
  exit 1
}

echo "Compiling for system type $DEST"

case "$CFLAGS" in
*-static*) MCFLAGS="$MCFLAGS -st" ;;
esac

t="$SRC/drivers/tools"
mcc="REALCC=\"$CC\" CC=\"$t/fcc\" \
REALCXX=\"$CXX\" CXX=\"$t/f++\" \
PATH=\""'$$PATH'":$t\" $MCC $MCFLAGS $CFLAGS"

cc="$CC $CFLAGS"

make -f - << _EOF_ || exit 1
all: $DEST/tform \
  $DEST/ReadForm$exe $DEST/ToForm$exe \
  $DEST/ToFortran$exe $DEST/ToC$exe \
  $DEST/ReadData$exe $DEST/reorder$exe

$DEST/tform: $SRC/FORM/$DEST/bin/tform$exe
	cp -p $SRC/FORM/$DEST/bin/tform$exe $DEST/

$DEST/ReadForm$exe: $SRC/FormCalc/ReadForm.tm
	$mcc -o $DEST/ReadForm$exe $SRC/FormCalc/ReadForm.tm
	-strip $DEST/ReadForm$exe

$DEST/ToForm$exe: $SRC/FormCalc/ToForm.c
	$cc -o $DEST/ToForm$exe $SRC/FormCalc/ToForm.c
	-strip $DEST/ToForm$exe

$DEST/ToFortran$exe: $SRC/FormCalc/ToFortran.c
	$cc -o $DEST/ToFortran$exe $SRC/FormCalc/ToFortran.c
	-strip $DEST/ToFortran$exe

$DEST/ToC$exe: $SRC/FormCalc/ToC.c
	$cc -o $DEST/ToC$exe $SRC/FormCalc/ToC.c
	-strip $DEST/ToC$exe

$DEST/ReadData$exe: $SRC/tools/ReadData.tm
	$mcc -o $DEST/ReadData$exe $SRC/tools/ReadData.tm
	-strip $DEST/ReadData$exe

$DEST/reorder$exe: $SRC/tools/reorder.c
	$cc -o $DEST/reorder$exe $SRC/tools/reorder.c
	-strip $DEST/reorder$exe
_EOF_


# Second, make the util library.

cd "$DEST"

trap "rm -f makefile" 0 1 2 3 15

"../$SRC/drivers/configure" --make="util.a clean"

