#!/usr/bin/env bash

# This script calls MLton.

set -e

dir=`dirname "$0"`
lib=`cd "$dir/../lib/mlton" && pwd`

declare -a rargs
case "$1" in
@MLton)
        shift
        while [ "$#" -gt 0 -a "$1" != "--" ]; do
                rargs[${#rargs[@]}]="$1"
                shift
        done
        if [ "$#" -gt 0 -a "$1" == "--" ]; then
                shift
        else
                echo '@MLton missing --'
                exit 1
        fi
        ;;
esac

EXE=""

doitMLton () {
    mlton_mlton="$lib/mlton-compile$EXE"
    if [ -x "$mlton_mlton" ]; then
        exec "$mlton_mlton" @MLton ram-slop 0.5 "${rargs[@]}" -- "$@"
    fi
}
doitSMLNJ () {
    smlnj='sml'
    if $smlnj -h >/dev/null 2>&1; then
        smlnj_heap_suffix=`echo 'TextIO.output (TextIO.stdErr, SMLofNJ.SysInfo.getHeapSuffix ());' | $smlnj 2>&1 1> /dev/null`
        mlton_smlnj_heap="$lib/mlton-smlnj.$smlnj_heap_suffix"
        if [ -s "$mlton_smlnj_heap" ]; then
            exec "$smlnj" @SMLload="$mlton_smlnj_heap" "$@"
        fi
    fi
}
doitPolyML () {
    mlton_polyml="$lib/mlton-polyml$EXE"
    if [ -x "$mlton_polyml" ]; then
        exec "$mlton_polyml" "$@"
    fi
}

doit () {
    doitMLton "$@"
    doitSMLNJ "$@"
    doitPolyML "$@"
    echo 'Unable to run MLton.  Check that lib is set properly.' >&2
    exit 1
}

CC="gcc"

# You may need to set 'GMP_INC_DIR' so the C compiler can find gmp.h.
GMP_INC_DIR=""
if [ -n "$GMP_INC_DIR" ]; then
gmpCCOpts="-cc-opt -I$GMP_INC_DIR"
fi
# You may need to set 'GMP_LIB_DIR' so the C compiler can find libgmp.
GMP_LIB_DIR=""
if [ -n "$GMP_LIB_DIR" ]; then
gmpLinkOpts="-link-opt -L$GMP_LIB_DIR -target-link-opt netbsd -Wl,-R$GMP_LIB_DIR"
fi

doit "$lib" \
        -ar-script "$lib/static-library"                         \
        -cc "$CC"                                                \
        -cc-opt '-std=gnu11 -fno-common'                         \
        -cc-opt '-O1 -fomit-frame-pointer -fno-strict-aliasing'  \
        -cc-opt '-w'                                             \
        -cc-opt-quote "-I$lib/include"                           \
        -link-opt '-lm -lgmp'                                    \
        $gmpCCOpts $gmpLinkOpts                                  \
        -llvm-llc-opt '-O2'                                      \
        -llvm-opt-opt '-mem2reg -O2'                             \
        -mlb-path-var 'SML_LIB $(LIB_MLTON_DIR)/sml'             \
        -target-as-opt amd64 '-m64'                              \
        -target-as-opt x86 '-m32'                                \
        -target-cc-opt alpha                                     \
                '-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d'   \
        -target-cc-opt amd64 '-m64'                              \
        -target-cc-opt aix '-maix64'                             \
        -target-cc-opt ia64-hpux "-mlp64"                        \
        -target-cc-opt ia64 "-mtune=itanium2"                    \
        -target-cc-opt sparc '-m32 -mcpu=v8 -Wa,-xarch=v8plusa'  \
        -target-cc-opt x86                                       \
                '-m32
                -fno-strength-reduce
                -fschedule-insns
                -fschedule-insns2
                -falign-functions=5
                -falign-jumps=2
                -falign-loops=2'                                 \
        -target-link-opt amd64 '-m64'                            \
        -target-link-opt alpha                                   \
                '-mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d'   \
        -target-link-opt aix '-maix64'                           \
        -target-link-opt ia64-hpux "-mlp64"                      \
        -target-link-opt linux '-Wl,-znoexecstack'               \
        -target-link-opt mingw                                   \
                '-lws2_32 -lkernel32 -lpsapi -lnetapi32 -lwinmm' \
        -target-link-opt mingw '-Wl,--enable-stdcall-fixup'      \
        -target-link-opt solaris '-lnsl -lsocket -lrt'           \
        -target-link-opt x86 '-m32'                              \
        -profile-exclude '\$\(SML_LIB\)'                         \
        "$@"
