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

cd src

if [ "$SAGE_DEBUG" = "yes" ]; then
    echo "Building a debug version of BoehmGC."
    export CFLAGS="-O0 -g $CFLAGS"
else
    export CFLAGS="-O2 -g $CFLAGS"
fi

if [ "$SAGE64" = "yes" ]; then
    echo "Building a 64-bit version of BoehmGC."
    export CFLAGS="-m64 $CFLAGS"
fi

CONFIGURE_FLAGS="--enable-large-config"
if [ "$UNAME" = "CYGWIN" ]; then
    # See https://trac.sagemath.org/ticket/22694
    CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-threads=posix --enable-handle-fork --enable-shared --disable-static"
    # Force use of mmap on Cygwin https://trac.sagemath.org/ticket/23973
    export CFLAGS="$CFLAGS -DUSE_MMAP -DUSE_MUNMAP"
fi

./configure --prefix="$SAGE_LOCAL" --libdir="$SAGE_LOCAL/lib" $CONFIGURE_FLAGS

if [ $? -ne 0 ]; then
   echo >&2 "Error configuring BoehmGC."
   exit 1
fi

$MAKE

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

$MAKE install

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