###########################################
## Python
###########################################

# 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

# Running 'make install' in parallel is a bad idea, so we use
# only 1 job.
# The "-i" option to ignore errors is crucial, especially in the
# case of upgrades.
sdh_make_install -i -j1

echo "Installing valgrind suppression file..."
if [ "$PKG_BASE" = "python2" ]; then
    SUPP_FILENAME="python.supp"
else
    SUPP_FILENAME="${PKG_BASE}.supp"
fi
sdh_install -T Misc/valgrind-python.supp "$SAGE_EXTCODE/valgrind/$SUPP_FILENAME"

if [ "$UNAME" = "Linux" ]; then
    export LD_LIBRARY_PATH="."
elif [ "$UNAME" = "Darwin" ]; then
    export DYLD_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

PYTHON_VERSION=$($PYTHON -c 'import sys; print("%d.%d" % sys.version_info[:2])')
if [ "$PKG_BASE" = "python2" ]; then
    PYTHON_LDVERSION="$PYTHON_VERSION"
    PYTHON_CONFIG_DIR="$SAGE_LOCAL/lib/python${PYTHON_VERSION}/config"
else
    PYTHON_ABIFLAGS=$($PYTHON -c 'import sys; print(sys.abiflags)')
    PYTHON_LDVERSION="$PYTHON_VERSION$PYTHON_ABIFLAGS"
    PYTHON_CONFIG_DIR="$SAGE_LOCAL/lib/python${PYTHON_VERSION}/config-${PYTHON_LDVERSION}"
fi

# On OS X with XCode 4, the presence of
# $SAGE_LOCAL/lib/python3.x/config/libpython3.x.a causes problems with
# GiNaC -- see #11967.  It is easiest to test the version of OS X; we
# delete this file if using OS X 10.6 or later (so `uname -r` returns
# x.y.z with x >= 10).
if [ "$UNAME" = "Darwin" ] && \
    [ `uname -r | cut '-d.' -f1` -gt 9 ]; then
    rm -f "${SAGE_DESTDIR}${PYTHON_CONFIG_DIR}/libpython${PYTHON_LDVERSION}.a"
elif [ "$UNAME" = "CYGWIN" ]; then
    # See http://trac.sagemath.org/ticket/20437
    ln -sf "${PYTHON_CONFIG_DIR}/libpython${PYTHON_LDVERSION}.dll.a" \
        "${SAGE_DESTDIR_LOCAL}/lib/libpython${PYTHON_LDVERSION}.dll.a"
fi
