Chris@303: #!/bin/bash
Chris@303: 
Chris@303: # Disable shellcheck warnings for useless-use-of-cat. UUOC is good
Chris@303: # practice, not bad: clearer, safer, less error-prone.
Chris@303: # shellcheck disable=SC2002
Chris@303: 
Chris@303: sml="$VEXT_SML"
Chris@303: 
Chris@303: set -eu
Chris@303: 
Chris@303: mydir=$(dirname "$0")
Chris@303: program="$mydir/vext.sml"
Chris@303: 
Chris@314: hasher=
Chris@314: local_install=
Chris@314: if [ -w "$mydir" ]; then
Chris@314:     if echo | sha256sum >/dev/null 2>&1 ; then
Chris@314: 	hasher=sha256sum
Chris@314:         local_install=true
Chris@314:     elif echo | shasum >/dev/null 2>&1 ; then
Chris@314: 	hasher=shasum
Chris@314: 	local_install=true
Chris@314:     else
Chris@314:         echo "WARNING: sha256sum or shasum program not found" 1>&2
Chris@314:     fi
Chris@314: fi
Chris@314: 
Chris@314: if [ -n "$local_install" ]; then
Chris@314:     hash=$(echo "$sml" | cat "$program" - | $hasher | cut -c1-16)
Chris@314:     gen_sml=$mydir/.vext-$hash.sml
Chris@314:     gen_out=$mydir/.vext-$hash.bin
Chris@314:     trap 'rm -f $gen_sml' 0
Chris@314: else
Chris@314:     gen_sml=$(mktemp /tmp/vext-XXXXXXXX.sml)
Chris@314:     gen_out=$(mktemp /tmp/vext-XXXXXXXX.bin)
Chris@314:     trap 'rm -f $gen_sml $gen_out' 0
Chris@314: fi
Chris@314: 
Chris@314: if [ -x "$gen_out" ]; then
Chris@314:     exec "$gen_out" "$@"
Chris@314: fi
Chris@314: 
Chris@303: # We need one of Poly/ML, SML/NJ, or MLton. Since we're running a
Chris@303: # single-file SML program as if it were a script, our order of
Chris@314: # preference is based on startup speed, except in the local_install
Chris@314: # case where we retain a persistent binary.
Chris@303: 
Chris@303: if [ -z "$sml" ]; then
Chris@314:     if [ -n "$local_install" ] && mlton 2>&1 | grep -q 'MLton'; then
Chris@314: 	sml="mlton"
Chris@314:     elif sml -h 2>&1 | grep -q 'Standard ML of New Jersey'; then
Chris@303: 	sml="smlnj"
Chris@303:     # We would prefer Poly/ML to SML/NJ, except that Poly v5.7 has a
Chris@303:     # nasty bug that occasionally causes it to deadlock on startup.
Chris@303:     # That appears to be fixed in their repo, so we could promote it
Chris@303:     # up the order again at some point in future
Chris@303:     elif echo | poly -v 2>/dev/null | grep -q 'Poly/ML'; then
Chris@303: 	sml="poly"
Chris@303:     elif mlton 2>&1 | grep -q 'MLton'; then
Chris@303: 	sml="mlton"
Chris@303:     else cat 1>&2 <<EOF
Chris@303: 
Chris@303: ERROR: No supported SML compiler or interpreter found       
Chris@303: EOF
Chris@315: 	cat 1>&2 <<EOF
Chris@303: 
Chris@303:   The Vext external source code manager needs a Standard ML (SML)
Chris@303:   compiler or interpreter to run.
Chris@303: 
Chris@303:   Please ensure you have one of the following SML implementations
Chris@303:   installed and present in your PATH, and try again.
Chris@303: 
Chris@303:     1. Standard ML of New Jersey
Chris@303:        - often found in a distribution package called: smlnj
Chris@303:        - executable name: sml
Chris@303: 
Chris@303:     2. Poly/ML
Chris@303:        - often found in a distribution package called: polyml
Chris@303:        - executable name: poly
Chris@303: 
Chris@303:     3. MLton
Chris@303:        - often found in a distribution package called: mlton
Chris@303:        - executable name: mlton
Chris@303: 
Chris@303: EOF
Chris@303: 	exit 2
Chris@303:     fi
Chris@303: fi
Chris@303: 
Chris@303: arglist=""
Chris@303: for arg in "$@"; do
Chris@303:     if [ -n "$arglist" ]; then arglist="$arglist,"; fi
Chris@315:     if echo "$arg" | grep -q '["'"'"']' ; then
Chris@303: 	arglist="$arglist\"usage\""
Chris@303:     else
Chris@303: 	arglist="$arglist\"$arg\""
Chris@303:     fi
Chris@303: done
Chris@303: 
Chris@303: case "$sml" in
Chris@314:     poly)
Chris@314:         if [ -n "$local_install" ] && polyc --help >/dev/null 2>&1 ; then
Chris@314:             if [ ! -x "$gen_out" ]; then
Chris@314:                 polyc -o "$gen_out" "$program"
Chris@314:             fi
Chris@314: 	    "$gen_out" "$@"
Chris@314:         else
Chris@314:             echo 'use "'"$program"'"; vext ['"$arglist"'];' |
Chris@314:                 poly -q --error-exit
Chris@314:         fi ;;
Chris@303:     mlton)
Chris@314:         if [ ! -x "$gen_out" ]; then
Chris@315: 	    echo "[Precompiling Vext binary...]" 1>&2
Chris@314: 	    echo "val _ = main ()" | cat "$program" - > "$gen_sml"
Chris@314: 	    mlton -output "$gen_out" "$gen_sml"
Chris@314:         fi
Chris@314: 	"$gen_out" "$@" ;;
Chris@303:     smlnj)
Chris@303: 	cat "$program" | (
Chris@303: 	    cat <<EOF
Chris@303: val smlrun__cp = 
Chris@303:     let val x = !Control.Print.out in
Chris@303:         Control.Print.out := { say = fn _ => (), flush = fn () => () };
Chris@303:         x
Chris@303:     end;
Chris@303: val smlrun__prev = ref "";
Chris@303: Control.Print.out := { 
Chris@303:     say = fn s => 
Chris@303:         (if String.isSubstring " Error" s
Chris@303:          then (Control.Print.out := smlrun__cp;
Chris@303:                (#say smlrun__cp) (!smlrun__prev);
Chris@303:                (#say smlrun__cp) s)
Chris@303:          else (smlrun__prev := s; ())),
Chris@303:     flush = fn s => ()
Chris@303: };
Chris@303: EOF
Chris@303: 	    cat -
Chris@303: 	    cat <<EOF
Chris@303: val _ = vext [$arglist];
Chris@303: val _ = OS.Process.exit (OS.Process.success);
Chris@303: EOF
Chris@314:             ) > "$gen_sml"
Chris@314: 	CM_VERBOSE=false sml "$gen_sml" ;;
Chris@303:     *)
Chris@315: 	echo "ERROR: Unknown SML implementation name: $sml" 1>&2;
Chris@303: 	exit 2 ;;
Chris@303: esac
Chris@303: