cd src

if [ "$SAGE64" = "yes" ]; then
   echo "64 bit build"
   CFLAGS="-m64 $CFLAGS "; export CFLAGS
   CXXFLAGS="-m64 $CXXFLAGS "; export CXXFLAGS
   CPPFLAGS="-m64 $CPPFLAGS "; export CPPFLAGS
   LDFLAGS="-m64 $LDFLAGS"; export LDFLAGS
fi

if [ "$LINBOX_BLAS" != "" ]; then
    echo "Using environment variable LINBOX_BLAS=$LINBOX_BLAS"
elif [ "$UNAME" = "CYGWIN" ]; then
    # TODO: we should install a suitable blas.pc on Cygwin
    echo "Using system-wide Cygwin LAPACK BLAS."
    if [ ! -f "/usr/lib/libblas.a" ]; then
        echo >&2 "*************************************************"
        echo >&2 "*"
        echo >&2 "* Error: On Cygwin you must install the standard LAPACK Cygwin package"
        echo >&2 "* via the Cygwin setup.exe program in the 'Math' category."
        echo >&2 "*"
        echo >&2 "*************************************************"
        exit 1
    fi
    LINBOX_BLAS="-lblas"
    LINBOX_BLAS_CFLAGS=""
else
    LINBOX_BLAS="$(pkg-config --libs cblas)"
    BLAS_CFLAGS="$(pkg-config --cflags 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 "*************************************************"

# 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 $FFLAS_FFPACK_CONFIGURE"
fi

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

# We disable openmp because of build failures, see
# http://trac.sagemath.org/ticket/17635#comment:67
sdh_configure --with-default="$SAGE_LOCAL" --with-blas-libs="$LINBOX_BLAS" \
              "$LINBOX_BLAS_CFLAGS" --disable-static \
              --enable-precompilation --disable-openmp $FFLAS_FFPACK_CONFIGURE
sdh_make

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

sdh_make_install
