#!/bin/sh

# This script calls MLton.

set -e

lib='lib unset'
gcc='gcc'
mlton="$lib/mlton-compile"
world="$lib/world.mlton"
nj='sml'
njHeap="$lib/mlton.x86-linux"

rargs=""
case "$1" in
@MLton)
	shift
	while [ $# -gt 0 -a "$1" != "--" ]; do
		rargs="$rargs $1"
		shift
	done
	shift
	;;
esac

# If $mlton is executable and $world exists and is not older than
# $njHeap (which exists), then use MLton, otherwise use SML/NJ.
doit () {
	if [ -x "$mlton" -a -s "$world" -a ! "$njHeap" -nt "$world" ]; then
		exec "$mlton" @MLton load-world "$world" $rargs -- "$@"
	elif [ -s "$njHeap" ]; then
		exec "$nj" @SMLload="$njHeap" "$@"
	fi
	echo 'Unable to run MLton.  Check that lib is set properly.' >&2
	exit 1
}

# You may need to add -L/path/to/libgmp before the "$@" so that the linker
# can find the gmp.

# -mcpu=pentiumpro is the same as -mcpu=i686

doit "$lib" \
	-cc "$gcc" \
	-ccopt '-malign-functions=5
		-malign-jumps=2 
		-fno-strict-aliasing
		-fno-strength-reduce
		-fomit-frame-pointer
		-fschedule-insns 
		-fschedule-insns2
		-malign-loops=2
		-mcpu=pentiumpro
		-w' \
	-lm \
	"$@"
