#!/usr/bin/env bash
###########################################
## Python
###########################################

CUR=`pwd`

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

# 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

# PATCH
for patch in ../patches/*.patch; do
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Error: Patch \"$patch\" failed to apply."
        exit 1
    fi
done

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

if [ `uname` = "Darwin" ]; then
    PYTHON_CONFIGURE="--disable-toolbox-glue $PYTHON_CONFIGURE"
fi

if [ "$SAGE64" = yes ]; then
    echo "64 bit build of Python enabled"
    export CC="$CC -m64"
fi

# 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
if [ "$UNAME" = SunOS ]; then
    export CFLAGS="-D__C99FEATURES__ $CFLAGS"
fi


build()
{
    ./configure --prefix="$SAGE_LOCAL" --libdir="$SAGE_LOCAL/lib" \
        --enable-unicode=ucs4 --enable-shared $PYTHON_CONFIGURE

    if [ $? -ne 0 ]; then
        echo >&2 "Error configuring Python."
        exit 1
    fi

    $MAKE
    if [ $? -ne 0 ]; then
        echo >&2 "Error building Python."
        exit 1
    fi

    # Remove old libraries
    rm -f "$SAGE_LOCAL"/lib/libpython*

    # 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.
    $MAKE -i -j1 install
    if [ $? -ne 0 ]; then
        echo >&2 "Error installing Python."
        exit 1
    fi

    echo "Installing valgrind suppression file..."
    mkdir -p "$SAGE_EXTCODE/valgrind"
    cp Misc/valgrind-python.supp "$SAGE_EXTCODE/valgrind/python.supp"
    if [ $? -ne 0 ]; then
        echo >&2 "Error installing valgrind suppression file."
        exit 1
    fi
}


build

# Make symbolic link (after removing old link first)
rm -f "$SAGE_LOCAL/bin/python"
ln -s python3 "$SAGE_LOCAL/bin/python"
if [ $? -ne 0 ]; then
    echo >&2 "Error creating symbolic link"
    exit 1
fi

PYTHON_VERSION=$("$SAGE_LOCAL/bin/python" -c 'import sys; print("%d.%d" % sys.version_info[:2])')

# On OS X with XCode 4, the presence of
# $SAGE_LOCAL/lib/python/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_LOCAL/lib/python$PYTHON_VERSION/config/libpython${PYTHON_VERSION}.a"
elif [ "$UNAME" = "CYGWIN" ]; then
    # See http://trac.sagemath.org/ticket/20437
    ln -sf "python/config/libpython${PYTHON_VERSION}.dll.a" "$SAGE_LOCAL/lib/libpython${PYTHON_VERSION}.dll.a"
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
for module in ctypes math hashlib crypt readline socket ; 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

