#!/usr/bin/env bash

# This script calls MLton.

set -e

dir=`dirname "$0"`
lib=`cd "$dir/../lib" && pwd`
eval `"$lib/platform"`
gcc='gcc'
case "$HOST_OS" in
mingw)
	exe='.exe'
;;
hpux)
        ia64hpux="-mlp64"
;;
*)
	exe=''
;;
esac
mlton="$lib/mlton-compile$exe"

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"
else
        mlton_smlnj_heap=""
fi

mlton_polyml="$lib/mlton-polyml$exe"

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

doitMLton () {
    exec "$mlton" @MLton ram-slop 0.5 "${rargs[@]}" -- "$@"
}
doitSMLNJ () {
    exec "$smlnj" @SMLload="$mlton_smlnj_heap" "$@"
}
doitPolyML () {
    exec "$mlton_polyml" "$@"
}

doit () {
        if [ -x "$mlton" ]; then
            doitMLton "$@"
        elif [ -s "$mlton_smlnj_heap" ]; then
            doitSMLNJ "$@"
        elif [ -x "$mlton_polyml" ]; then
            doitPolyML "$@"
        fi
        echo 'Unable to run MLton.  Check that lib is set properly.' >&2
        exit 1
}

# For align-{functions,jumps,loops}, we use -m for now instead of
# -f because old gcc's will barf on -f, while newer ones only warn
# about -m.  Someday, when we think we won't run into older gcc's,
# these should be changed to -f.

# You may need to add a line with -cc-opt 'I/path/to/gmp.h' so the
# C compiler can find gmp.h
# You may need to add a line with -link-opt '-L/path/to/libgmp' so
# that the linker can find libgmp.

# The darwin linker complains (loudly) about non-existent library
# search paths.
darwinLinkOpts=''
if [ -d '/usr/local/lib' ]; then
        darwinLinkOpts="$darwinLinkOpts -L/usr/local/lib"
fi
if [ -d '/opt/local/lib' ]; then
        darwinLinkOpts="$darwinLinkOpts -L/opt/local/lib"
fi
if [ -d '/sw/lib' ]; then
        darwinLinkOpts="$darwinLinkOpts -L/sw/lib"
fi

doit "$lib" \
        -ar-script "$lib/static-library"                         \
        -cc "$gcc"                                               \
        -cc-opt-quote "-I$lib/include"                           \
        -cc-opt '-O1 -fno-common'                                \
        -cc-opt '-fno-strict-aliasing -fomit-frame-pointer -w'   \
        -link-opt '-lm -lgmp'                                    \
        -mlb-path-map "$lib/mlb-path-map"                        \
        -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 darwin                                    \
                '-I/usr/local/include
                 -I/opt/local/include
                 -I/sw/include'                                  \
        -target-cc-opt freebsd '-I/usr/local/include'            \
        -target-cc-opt netbsd '-I/usr/pkg/include'               \
        -target-cc-opt openbsd '-I/usr/local/include'            \
        -target-cc-opt aix '-maix64'                             \
        -target-cc-opt ia64 "$ia64hpux -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 darwin "$darwinLinkOpts"                \
        -target-link-opt freebsd '-L/usr/local/lib/'             \
        -target-link-opt aix '-maix64'                           \
        -target-link-opt ia64 "$ia64hpux"                        \
        -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 netbsd                                  \
                '-Wl,-R/usr/pkg/lib -L/usr/pkg/lib/'             \
        -target-link-opt openbsd '-L/usr/local/lib/'             \
        -target-link-opt solaris '-lnsl -lsocket -lrt'           \
        -target-link-opt x86 '-m32'                              \
        -profile-exclude '\$\(SML_LIB\)'                         \
        "$@"
