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

export CFLAGS="-O2 -g -fPIC $CFLAGS"

cd src/


build()
{
    ./configure --prefix="$SAGE_LOCAL"
    if [ $? -ne 0 ]; then
        echo >&2 "Error configuring termcap"
        exit 1
    fi

    $MAKE CFLAGS="$CFLAGS" install
    if [ $? -ne 0 ]; then
        echo >&2 "Error building termcap"
        exit 1
    fi
}

build

# If we cannot link programs against -lncurses, then symlink
# libtermcap.a to libncurses.a.  This is to support the PARI and
# Python build scripts when /usr/lib/libncurses.so exists but
# cannot be linked (e.g. because we are cross-compiling).
# See #12725.
if testcflags.sh -lncurses >/dev/null; then
    echo "Good, we can link against libncurses on your system."
else
    echo "We cannot link against libncurses on your system,"
    echo "making a symbolic link libncurses.a -> libtermcap.a"
    echo "(to work around bugs in the PARI and Python build scripts)"
    cd "$SAGE_LOCAL/lib" && ln -s libtermcap.a libncurses.a
    if [ $? -ne 0 ]; then
        echo >&2 "Error making symbolic link libncurses.a -> libtermcap.a"
        exit 1
    fi
fi
