#!/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

if [ -z $CFLAG64 ]; then
    CFLAG64=-m64
fi

# Compile for 64-bit if SAGE64 is set to 'yes':
if [ $SAGE64 = yes ]; then
    echo "Building a 64-bit version of Readline."
    CFLAGS="$CFLAGS $CFLAG64"
    CPPFLAGS="$CPPFLAGS $CFLAG64"
    # Some packages may need LDFLAGS and/or ABI set here.
    LDFLAGS="$LDFLAGS $CFLAG64"
fi

DEBUG_CONFIGURE_FLAG=''
if [ "$SAGE_DEBUG" = yes ]; then
    CFLAGS="-O0 -g $CFLAGS"
    DEBUG_CONFIGURE_FLAG='--with-debug'
else
    DEBUG_CONFIGURE_FLAG='--without-debug'
fi


echo "The following environment variables will be exported:"
echo "Using CC=$CC"
echo "Using CFLAGS=$CFLAGS"
echo "Using CPPFLAGS=$CPPFLAGS"
echo "Using LDFLAGS=$LDFLAGS"
echo

export CFLAGS
export CPPFLAGS
export LDFLAGS

cd src

echo "Applying patches to upstream source..."
for patch in ../patches/*.patch; do
    [ -r "$patch" ] || continue
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Error applying '$patch'."
        exit 1
    fi
done


# Ncurses cannot build narrow and wide (unicode, --enable-widec)
# versions in one go. Need to compile twice. Note that Python's curses
# module will want the wide version, and readline the narrow version.

###################################################
mkdir narrow
cd narrow
ln -s ../configure .

echo "Configuring ncurses (narrow)..."
./configure \
    --prefix="$SAGE_LOCAL" \
    --libdir="$SAGE_LOCAL/lib" \
    --with-termlib \
    --with-shared \
    --without-normal \
    --without-ada \
    --disable-rpath-hack \
    --enable-overwrite \
    --with-pkg-config-libdir="$SAGE_LOCAL/lib/pkgconfig" \
    --enable-pc-files \
    "$DEBUG_CONFIGURE_FLAG"
if [ $? -ne 0 ]; then
    echo >&2 "Error configuring ncurses (narrow)."
    exit 1
fi

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

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

cd ..  # leave narrow

###################################################
mkdir wide
cd wide
ln -s ../configure .

echo "Configuring ncurses (wide)..."
./configure \
    --prefix="$SAGE_LOCAL" \
    --libdir="$SAGE_LOCAL/lib" \
    --with-termlib \
    --with-shared \
    --enable-widec \
    --without-normal \
    --without-ada \
    --disable-rpath-hack \
    --enable-overwrite \
    --with-pkg-config-libdir="$SAGE_LOCAL/lib/pkgconfig" \
    --enable-pc-files \
    "$DEBUG_CONFIGURE_FLAG"
if [ $? -ne 0 ]; then
    echo >&2 "Error configuring ncurses (wide)."
    exit 1
fi

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

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

cd ..  # leave wide
