#! /bin/sh
prefix="/usr/pkg"
exec_prefix="${prefix}"
BINDIR="${exec_prefix}/bin"
LINK="c++"
LIBDIR="${exec_prefix}/lib"
LIBS=""
CFLAGS="-O2 -I/usr/pkg/include -I/usr/X11R7/include -I/usr/include -I/usr/pkg/include/freetype2 -I/usr/X11R7/include"

DEFAULT_COMPILER="${BINDIR}/poly"
COMPILER="${DEFAULT_COMPILER}"

# Extra options for Windows.  config.status sets these conditionals to either "" or "#".

EXTRALDFLAGS=""
##EXTRALDFLAGS="-Wl,-u,WinMain"
#@ARCHINTERPRET64_TRUE@EXTRALDFLAGS="-Wl,-u,WinMain"
##EXTRALDFLAGS="-Wl,-u,_WinMain@16 -Wl,--large-address-aware"
##EXTRALDFLAGS="-Wl,-u,_WinMain@16 -Wl,--large-address-aware"
##EXTRALDFLAGS+=" -mwindows"
#EXTRALDFLAGS+=" -mconsole"

#SUFFIX="obj"
SUFFIX="o"

# Msys passes the Windows TEMP in temp (lower case)
# On other systems allow TMPDIR to override /tmp.
#TEMPORARYDIR="${temp:-/tmp}"
TEMPORARYDIR="${TMPDIR:-/tmp}"

TMPOBJFILE="${TEMPORARYDIR}/polyobj.$$.$SUFFIX"
trap 'rm -f "$TMPOBJFILE"' 0

compile()
{
    echo "val () = use (List.nth(CommandLine.arguments(), 2)); val () = PolyML.export(List.nth(CommandLine.arguments(), 3), main);" | "${COMPILER}" -q --error-exit  "$1" "$2"
}

link()
{
    if [ X"$2" = "X" ]
    then
        "${LINK}" ${EXTRALDFLAGS} ${CFLAGS} "$1" "-L${LIBDIR}" "-Wl,-rpath,${LIBDIR}" -lpolymain -lpolyml ${LIBS}
    else
        "${LINK}" ${EXTRALDFLAGS} ${CFLAGS} "$1" -o "$2" "-L${LIBDIR}" "-Wl,-rpath,${LIBDIR}" -lpolymain -lpolyml ${LIBS}
    fi
}

printhelp()
{
    echo "Usage: polyc [OPTION]... [SOURCEFILE]"
    echo Compile and link a Standard ML source file with Poly/ML.
    echo
    echo "   -b poly      Use 'poly' as compiler instead of ${DEFAULT_COMPILER}"
    echo "   -c           Compile but do not link.  The object file is written to the source file with .$SUFFIX extension."
    echo "   -o output    Write the executable file to 'output'"
    echo "   --help       Write this text and exit"
    exit
}

usage()
{
    echo "$1"
    echo "Usage: polyc [OPTION]... [SOURCEFILE]"
    exit 1
}

checkml()
{
    extension="${1##*.}"
    case "$extension" in
        sml|ML)
             return 0 ;;
        o|obj)
             return 1;;
        *)
             test -r "$1" && file -b "$1" | grep -q text ;;
    esac
}

sourcefile=""
outputfile=""
compileonly="no"

while [ $# -gt 0 ]
do
    case "$1" in
        --help)
            printhelp ;;
        -b)
            shift
            [ $# -eq 0 ] && usage "Expected file name after -b"
            COMPILER="$1";;
        -c) compileonly="yes";;
        -o)
            shift
            [ $# -eq 0 ] && usage "Expected file name after -o"
            outputfile="$1";;
        *)
            [ X"$sourcefile" = "X" ] || usage "Only one source file name allowed"
            sourcefile="$1";;
    esac
    shift
done

[ X"$sourcefile" = "X" ] && usage "No input files"
[ -r "$sourcefile" ] || usage "Error: $sourcefile: No such file"

case "$compileonly" in
     yes)
	 if [ "x$outputfile" = "x" ]; then
	     basename="${sourcefile##*/}"
             outputfile="${basename%.*}.o"
	 fi
         compile "$sourcefile" "$outputfile"
         ;;
     no)
         if checkml "$sourcefile"
         then
             compile "$sourcefile" "$TMPOBJFILE" && link "$TMPOBJFILE" "$outputfile"
         else
             link "$sourcefile" "$outputfile"
         fi
         ;;
esac
