# It is best to unset these environment variables, as they might confuse
# the Python installer.
unset PYTHONHOME
unset PYTHONPATH

# Prevent use of the system hg and svn as it might make the installation fail
export HAS_HG=no
export SVNVERSION=no

cd src

if [ "$SAGE_DEBUG" = "yes" ]; then
    echo "Building Python with pydebug"
    PYTHON_CONFIGURE="$PYTHON_CONFIGURE --with-pydebug"
fi

# pymalloc screws with valgrind, so let's disable it
if [ "$SAGE_VALGRIND" = "yes" ]; then
    echo "Building Python without pymalloc"
    PYTHON_CONFIGURE="$PYTHON_CONFIGURE --without-pymalloc"
fi

# Use EXTRA_CFLAGS for user-defined CFLAGS since Python puts its own
# default flags like -O3 after CFLAGS but before EXTRA_CFLAGS.
# We also disable warnings about unused variables/functions which are
# common in Cython-generated code.
export EXTRA_CFLAGS="`testcflags.sh -Wno-unused` $CFLAGS"
unset CFLAGS

if [ "$UNAME" = Darwin ]; then
    PYTHON_CONFIGURE="--disable-toolbox-glue $PYTHON_CONFIGURE"

    mkdir "../include"
    if [ -n "$OPENSSL_INCLUDE" ];  then
        # If the user explicitely states where to get the openssl
        # includes, use that.
        cp -rp "$OPENSSL_INCLUDE" "../include"
        export CFLAGS="-I../include"
    else
        # Otherwise try using homebrew version
        brew_openssl="/usr/local/opt/openssl/include"
        if [ -d "$brew_openssl" ]; then
            export LDFLAGS="$LDFLAGS -L/usr/local/opt/openssl/lib"
            export CPPFLAGS="$CPPFLAGS -I/usr/local/opt/openssl/include"
            export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/opt/openssl/lib/pkgconfig"
        fi
    fi

    if [ $MACOSX_VERSION -ge 16 ]; then
        echo "OS X 10.$[$MACOSX_VERSION-4] Building with clang."
        CC=clang
    fi
elif [ "$UNAME" = SunOS ]; then
    # Enable some C99 features on Solaris. This in particular enables
    # the isinf() and isfinite() functions. It works both for C and
    # C++ code (which is not true for -std=c99).  See
    # http://trac.sagemath.org/sage_trac/ticket/14265
    export CFLAGS="-D__C99FEATURES__ $CFLAGS"
fi

# Remove old symbolic link: it is not needed and its presence can
# interfere with the Python build.
rm -f "$SAGE_LOCAL/lib/python"

# Remove old libraries. We really need to do this before building Python
# since Python tries to import some modules (e.g. ctypes) at build-time.
# We need to make sure that the old installed libraries in local/lib are
# not used for that. See https://trac.sagemath.org/ticket/24605
rm -f "$SAGE_LOCAL"/lib/lib"$PKG_BASE"*


if [ "$PKG_BASE" = "python2" ]; then
    PYTHON_CONFIGURE="$PYTHON_CONFIGURE --enable-unicode=ucs4"
else
    # Note: --without-ensurepip ensures that setuptools+pip are *not* installed
    # automatically when installing python3. They will be installed instead by
    # the separate setuptools and pip packages; see
    # https://trac.sagemath.org/ticket/23398
    PYTHON_CONFIGURE="$PYTHON_CONFIGURE --without-ensurepip"
fi

sdh_configure --enable-shared $PYTHON_CONFIGURE

# Make sure -L. is placed before -L$SAGE_LOCAL/lib so that python and extension
# modules are linked with the right libpython; we pass this in at make time
# only, since we don't want -L. to be saved as one of the default LDFLAGS
# used for building third-party extension modules
sdh_make LDFLAGS="-L. $LDFLAGS"

if [ "$UNAME" = "Darwin" ]; then
    export DYLD_LIBRARY_PATH="."
else
    export LD_LIBRARY_PATH="."
fi

# When building on a case-insensitive filesystem (on any OS, not just Windows)
# the Python executable is output to the build directory as 'python.exe'
if [ -f "python.exe" ]; then
    PYTHON="./python.exe"
else
    PYTHON="./python"
fi

# Make sure extension modules were built correctly.
# All these modules are important and if any one
# fails to build, Sage will not work.

echo "Testing importing of various modules..."
import_errors=false
test_modules="ctypes math hashlib crypt readline socket"
if [ "$UNAME" = "Darwin" ]; then
    test_modules="$test_modules _scproxy"
fi

for module in $test_modules; do
    if $PYTHON -c "import $module"; then
        echo "$module module imported OK"
    else
        echo >&2 "$module module failed to import"
        import_errors=true
    fi
done

if $import_errors; then
    echo >&2 "Error: One or more modules failed to import."
    exit 1
fi
