#!/usr/bin/env bash

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

# Let the user set an environment variable CFLAG64 to
# indicate the C compiler flag for 64-bit builds. By
# default this will be -m64. IBM's compiler on AIX
# and HP's on HP-UX do NOT use -m64.
if [ -z "$CFLAG64" ] ; then
    CFLAG64=-m64
fi

# There is no C++ code in GSL, so no need to do likewise
# with CXXFLAG64.
if [ "$SAGE64" = "yes" ]; then
    echo "Building a 64-bit version of the GNU Scientific Library (GSL)"
    CFLAGS="$CFLAGS $CFLAG64"
    CPPFLAGS="$CPPFLAGS $CFLAG64" ; export CPPFLAGS
    LDFLAGS="$LDFLAGS $CFLAG64" ; export LDFLAGS
fi

if [ "$SAGE_DEBUG" = "yes" ] ; then
    CFLAGS="-g -O0 $CFLAGS" # No optimisation, aids debugging.
else
    CFLAGS="-g -O2 $CFLAGS" # Normal optimisation.
fi

export CFLAGS

cd src

echo "Applying patches to upstream sources..."
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


echo "Configuring GSL..."
./configure --prefix="$SAGE_LOCAL" --libdir="$SAGE_LOCAL/lib"
if [ $? -ne 0 ]; then
    echo >&2 "Error configuring GSL."
    exit 1
fi

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

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