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

if [[ $SAGE_DEBUG = yes ]]; then
    CFLAGS="$CFLAGS -g -O0"
else
    CFLAGS="-g -O2 $CFLAGS"
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 "Configure scripts and/or makefiles might override these later."

export CC
export CFLAGS
export CPPFLAGS
export LDFLAGS

cd src/

echo "Configuring readline..."
./configure \
    --prefix="$SAGE_LOCAL" \
    --libdir="$SAGE_LOCAL/lib" \
    --with-curses \
    --enable-shared --disable-static
if [[ $? -ne 0 ]]; then
    echo >&2 "Error configuring readline."
    exit 1
fi

echo "Now building static and shared readline libraries..."
$MAKE
if [[ $? -ne 0 ]]; then
    echo >&2 "Error building readline."
    exit 1
fi

echo "Build succeedeed.  Deleting old readline headers and libs"
echo "before installing the new ones..."
# (Note; Actually also readline does this by itself, but doing it
# here, too, doesn't hurt either.)
rm -rf "$SAGE_LOCAL"/include/readline/ "$SAGE_LOCAL"/lib/libreadline.*
if [[ $? -ne 0 ]]; then
    echo >&2 "Error removing old version of readline."
    exit 1
fi

echo "Now installing the new readline headers and libraries..."
$MAKE install
if [[ $? -ne 0 ]]; then
    echo >&2 "Error installing readline."
    exit 1
fi

case "$UNAME" in
    Darwin)
        DYLIB_NAME=libreadline.dylib;;
    CYGWIN)
        # It is of course very lame that readline names the file .dll.a, but that's what it does.
        DYLIB_NAME=libreadline.dll.a;;
    OpenBSD)
        # Untested. (David Kirkby, 11th November 2010)
        # Extension changed from 6.1 to 6.2; still untested. (October 2011)
        # Extension changed from 6.2 to 6.3; still untested. (June 2014)
        DYLIB_NAME=libreadline.so.6.3;;
    FreeBSD)
        DYLIB_NAME=libreadline.so.6;;
    HP-UX)
        DYLIB_NAME=libreadline.sl.6;;
    *)  # e.g. "Linux" or "SunOS"
        DYLIB_NAME=libreadline.so
esac

# Make sure that the install worked, despite whatever the
# exit code of 'make' or 'make install' was:
if [[ -f "$SAGE_LOCAL/lib/$DYLIB_NAME" ]]; then
    echo "Fixing permissions of libraries..."
    chmod 755 "$SAGE_LOCAL"/lib/libreadline.*
    chmod 755 "$SAGE_LOCAL"/lib/libhistory.*
else
    # One or both of the readline libraries are missing, i.e.
    # haven't been installed.
    echo >&2 "Error: Readline's build claims to have finished, but $DYLIB_NAME was not installed.)"
    exit 1
fi
