#!/bin/sh

# Run the system python.
#
# This is primarily for use by the build toolchain so that it can continue
# using the system Python rather than Sage's Python, preventing conflicts
# that might otherwise occur, particularly in parallel builds.
#
# See https://trac.sagemath.org/ticket/18438

if [ -z "$SAGE_ORIG_PATH" ]; then
    # If not we're running from within sage-env just set the existing path
    SAGE_ORIG_PATH="$PATH"
fi

# In particular, it is invoked by "bootstrap -d" for sage-download-file,
# i.e., before a configure run, and by "sage-spkg", also for sage-download-file.
# So it needs to find a python that has the urllib module.
# For example, on Debian buster, the python3-minimal package does NOT provide it.
#
# See https://trac.sagemath.org/ticket/29090

# Trac #29890: Our first choice is "python", not "python3". This is to avoid
# a defect of sage_bootstrap on macOS regarding SSL URLs.

# Trac #30177: Also check for hashlib.sha1 to guard against broken python2
# from old homebrew installations.  Also check whether the current directory
# is accessible by this python; this is to guard on Cygwin against Pythons
# installed somewhere else in Windows.

# Trac #30008: Make it work even if the environment tries to sabotage UTF-8
# operation in Python 3.0.x-3.6.x by setting LC_ALL=C or similar.

if [ "$LC_ALL" = "C" -o "$LANG" = "C" -o "$LC_CTYPE" = "C" ]; then
    LC_ALL=$(locale -a | grep -E -i '^(c|en_us)[-.]utf-?8$' | head -n 1)
    LANG=$LC_ALL
    export LC_ALL
    export LANG
fi

PYTHONS="python python3 python3.8 python3.7 python2.7 python3.6 python2"
for PY in $PYTHONS; do
    PYTHON="$(PATH="$SAGE_ORIG_PATH" command -v $PY)"
    if [ -n "$PYTHON" ]; then
        if "$PYTHON" -c "import urllib; from hashlib import sha1; from os import listdir; listdir(\"$(pwd)\");" 2>/dev/null; then
            exec "$PYTHON" "$@"
        fi
    fi
done
echo >&2 "$0: error: none of $PYTHONS is a suitable Python with urllib"
exit 1
