cd src

if [ "$LINBOX_BLAS" != "" ]; then
    echo "Using environment variable LINBOX_BLAS=$LINBOX_BLAS"
else
    LINBOX_BLAS="$(pkg-config --libs blas cblas)"
    BLAS_CFLAGS="$(pkg-config --cflags blas cblas)"
    if [ "$BLAS_CFLAGS" != "" ]; then
        LINBOX_BLAS_CFLAGS="--with-blas-cflags=$BLAS_CFLAGS"
    else
        LINBOX_BLAS_CFLAGS=""
    fi
fi

echo "*************************************************"
echo "Using --with-blas-libs='$LINBOX_BLAS' '$LINBOX_BLAS_CFLAGS'"
echo "*************************************************"

FFLAS_FFPACK_CONFIGURE="$SAGE_CONFIGURE_FFLAS_FFPACK $FFLAS_FFPACK_CONFIGURE"

# If SAGE_FAT_BINARY is set, disable all processor-specific optimizations
if [ "$SAGE_FAT_BINARY" = yes ]; then
    FFLAS_FFPACK_CONFIGURE="--disable-sse --disable-sse2 --disable-sse3 --disable-ssse3 --disable-sse41 --disable-sse42 --disable-fma --disable-fma4 --disable-avx --disable-avx2 --disable-avx512f --disable-avx512dq --disable-avx512vl $FFLAS_FFPACK_CONFIGURE"
fi

# Need to use 'bash' for configure, see
# https://github.com/sagemath/sage/issues/23451
if [ -z "$CONFIG_SHELL" ]; then
    export CONFIG_SHELL=`command -v bash`
fi

# We disable openmp because of build failures, see
# https://github.com/sagemath/sage/issues/17635#comment:67
sdh_configure --with-default="$SAGE_LOCAL" --with-blas-libs="$LINBOX_BLAS" \
              "$LINBOX_BLAS_CFLAGS" --disable-static \
              --enable-precompilation $FFLAS_FFPACK_CONFIGURE
sdh_make

$MAKE autotune
if [ $? -ne 0 ]; then
    echo >&2 "Error tuning fflas-ffpack"
    exit 1
fi

sdh_make_install
