#!/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


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

# We are setting LDFLAGS so that we pick up Sage's readline
LDFLAGS="-L$SAGE_LOCAL/lib $LDFLAGS"
export LDFLAGS

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
}


build

cd "$SAGE_LOCAL/lib"

# If we are upgrading from Python-2.6, remove compiled Python files.
# We do not care about still older Python versions, since upgrades
# from such old Sage versions are not supported anyway.
if [ -d python2.6 ]; then
    rm -rf "$SAGE_ROOT"/devel/sage-*/build
fi

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

# Remove previous Python installs
rm -rf python2.6

# On OS X with XCode 4, the presence of
# $SAGE_LOCAL/lib/python/config/libpython2.7.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/python2.7/config/libpython2.7.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."
    # Check if we are on Debian or one of its derivatives:
    if { command -v dpkg && ! command -v dpkg-architecture; } >/dev/null; then
        echo >&2 "You may have to install 'dpkg-architecture'"
        echo >&2 "which is part of the Debian package 'dpkg-dev'."
        echo >&2 "Try installing it by typing"
        echo >&2 "    sudo apt-get install dpkg-dev"
        echo >&2 "and rerun 'make'."
    fi
    exit 1
fi
