#!/usr/bin/env sh

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

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

# Use newer version of config.guess and config.sub (see Trac #19714)
cp "$SAGE_ROOT"/config/config.* config

# The following prevents autotools to regenerate its files.
touch aclocal.m4 configure Makefile.in config.h.in
touch src/platform.hpp.in

CPPFLAGS="$CPPFLAGS -Wno-long-long"
export CPPFLAGS

./configure --prefix="$SAGE_LOCAL" --libdir="$SAGE_LOCAL"/lib
if [ $? -ne 0 ]; then
    echo >&2 "Failed to configure zeromq"
    exit 1
fi

$MAKE
if [ $? -ne 0 ]; then
    echo >&2 "Failed to build zeromq"
    exit 1
fi

$MAKE -j1 install
if [ $? -ne 0 ]; then
    echo >&2 "Failed to install zeromq"
    exit 1
fi
