#!/bin/bash

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

updateversion=yes
if [[ $# -gt 3 ]]
then
    updateversion="$4"
fi

if [[ $updateversion == yes ]]
then
    echo "core.info" > ${STORM_ROOT}/Compiler/COMPILER.version
    echo "$version" >> ${STORM_ROOT}/Compiler/COMPILER.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
