######################################################################
### Sanity check
######################################################################

if [ -z "$SAGE_LOCAL" ]; then
    echo >&2 "Error: SAGE_LOCAL undefined - exiting..."
    echo >&2 "Maybe run 'sage -sh'?"
    exit 1
fi

######################################################################
### Skip building ATLAS on specific systems
######################################################################

if [ "$UNAME" = "Darwin" -a -z "$SAGE_ATLAS_ARCH" ]; then
    echo "System-wide accelerate framework is used on Darwin; skipping ATLAS test suite."
    exit 0
fi

if [ ! -z "$SAGE_ATLAS_LIB" ]; then
    echo "SAGE_ATLAS_LIB is set to \"$SAGE_ATLAS_LIB\"; skipping ATLAS test suite."
    exit 0
fi


######################################################################
### check and collect timings
######################################################################

make_check()
{
    # make sure everything builds correctly
    $MAKE check
    if [ $? -ne 0 ]; then
        echo >&2 "Error: The ATLAS self-tests failed."
        exit 1
    else
        echo "The ATLAS self-tests successfully passed."
    fi
}


make_time()
{
    # collect some timings
    $MAKE time
    if [ $? -ne 0 ]; then
        echo >&2 "Error: The ATLAS timing data failed to be collected."
        exit 1
    else
        echo "The ATLAS timing data was successfully collected."
    fi
}

cd src/ATLAS-build
make_check
make_time
