#!/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 define the environment variable CFLAG64 to
# something if their C compiler needs a compiler flag other than
# -m64 to build 64-bit objects. This would be the case with IBM's
# compiler on AIX and HP's compiler on HP-UX. But -m64 works
# with both GCC and SunStudio.
if [ -z $CFLAG64 ] ; then
   CFLAG64="-m64"
fi

# Create a 64-bit build if the environment variable SAGE64 is set to
# "yes".
if [ "x$SAGE64" = xyes ] ; then
   echo "Creating a 64-bit version of GNU patch"
   CFLAGS="$CFLAGS $CFLAG64"
   export CFLAGS
   LDFLAGS="$LDFLAGS $CFLAG64"
   export LDFLAGS
fi

# Disable debugging information on AIX, as this causes link errors. See:
# http://www.ibm.com/developerworks/forums/thread.jspa?threadID=348558
# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46072
if [ "x$UNAME" = xAIX ] ; then
   CFLAGS="$CFLAGS -g0"
   export CFLAGS
fi

cd src

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

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

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

if [ "$UNAME" = "CYGWIN" ] ; then
    cp ../patch.exe.manifest "$SAGE_LOCAL/bin/"
fi

# Sanity check that we have the correct version of patch
# in our PATH.
if ! patch --version | grep >/dev/null 'patch 2\.7\.5'; then
    echo >&2 "Cannot find the patch program we just installed"
    exit 1
fi
