###############################################################################
#
# FLINT Sage install script
#
###############################################################################

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

if [ "$SAGE_DEBUG" = "yes" ]; then
    echo "Building a debug version of FLINT."
    CFLAGS="-O0 -g $CFLAGS"; export CFLAGS
    FLINT_TUNE=" $FLINT_TUNE"; export FLINT_TUNE
    FLINT_CONFIGURE="--enable-assert $FLINT_CONFIGURE"
fi

cd src

echo "Configuring FLINT."
./configure \
    --disable-static \
    --prefix="$SAGE_LOCAL" \
    --with-gmp="$SAGE_LOCAL" \
    --with-mpfr="$SAGE_LOCAL" \
    --with-ntl="$SAGE_LOCAL" \
    $FLINT_CONFIGURE
if [ $? -ne 0 ]; then
    echo >&2 "Error: Failed to configure FLINT."
    exit 1
fi

echo "Building FLINT shared library."
$MAKE verbose
if [ $? -ne 0 ]; then
    echo >&2 "Error: Failed to build FLINT shared library."
    exit 1
fi

echo "Deleting old FLINT files."
rm -f $SAGE_LOCAL/lib/libflint*
rm -rf $SAGE_LOCAL/include/flint

echo "Installing new FLINT files."
$MAKE install
if [ $? -ne 0 ]; then
    echo >&2 "Error: Failed to install FLINT."
    exit 1
fi
