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

cd src

if [ "$SAGE_DEBUG" = "yes" ]; then
    echo "Building a debug version of IML."
    export CFLAGS="-O0 -g $CFLAGS"
else
    export CFLAGS="-O3 -g $CFLAGS"
fi

if [ "$SAGE64" = "yes" ]; then
    echo "Building a 64-bit version of IML."
    CFLAGS="-m64 $CFLAGS"
fi

./configure \
    --prefix="$SAGE_LOCAL" \
    --libdir="$SAGE_LOCAL/lib" \
    --enable-shared \
    --with-default="$SAGE_LOCAL" \
    --with-cblas="$(pkg-config --libs cblas)" \
    --with-cblas-include="$(pkg-config --cflags cblas)" \
    "$IML_CONFIGURE"
if [ $? -ne 0 ]; then
    echo >&2 "Error configuring IML."
    exit 1
fi

$MAKE
if [ $? -ne 0 ]; then
    echo >&2 "Error building IML."
    exit 1
fi

$MAKE install
if [ $? -ne 0 ]; then
    echo >&2 "Error installing IML."
    exit 1
fi
