#!/bin/bash

arch="$1"
command="$2"
version="$3"
echo "Compiling $version..."


# Update the COMPILER.version file if needed.
TMP_VERSION=${STORM_ROOT}/COMPILER.version
DST_VERSION=${STORM_ROOT}/Compiler/COMPILER.version

echo "core.info" > ${TMP_VERSION}
echo "$version" >> ${TMP_VERSION}

if [[ ! -f ${DST_VERSION} ]]
then
    mv ${TMP_VERSION} ${DST_VERSION}
elif ! diff ${TMP_VERSION} ${DST_VERSION} > /dev/null
then
    mv ${TMP_VERSION} ${DST_VERSION}
else
    rm ${TMP_VERSION}
fi


cd $STORM_ROOT

if [[ "$STORM_USE_COMPAT" == "1" ]]
then
    compat="compat"
else
    compat=""
fi

mm release $compat $command -ne || { echo "Compilation ($arch) failed!"; exit 1; }

releasedir=release
if [[ $command == *64* ]]
then
    releasedir=release64
fi

if [[ $command == *mps* ]]
then
    echo "Done! Running test suite..."
    ${releasedir}/Test_mps --all || { echo "Tests failed ($arch)!"; exit 1; }
    echo "Success!"
elif [[ $command == *smm* ]]
then
    echo "Skipping test suite for SMM."
else
    echo "ERROR: Unknown garbage collector. Command line: ${command}"
    exit 1
fi
