#!/usr/bin/env bash

cd src

for patch in ../patches/*.patch; do
    [ -r "$patch" ] || continue  # Skip non-existing or non-readable patches
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Error applying '$patch'"
        exit 1
    fi
done

# The git head of arb now honors LDFLAGS; The following workaround can
# be removed in arb >= 2.8 when it is released
export EXTRA_SHARED_FLAGS=$LDFLAGS

./configure --disable-static --prefix="$SAGE_LOCAL" --with-flint="$SAGE_LOCAL" \
    --with-gmp="$SAGE_LOCAL" --with-mpfr="$SAGE_LOCAL"
if [ $? -ne 0 ]; then
    echo >&2 "Error configuring arb."
    exit 1
fi

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

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