#!/bin/bash

# -e  Exit immediately if a command exits with a non-zero status.
# -u  Treat unset variables as an error when substituting.
set -e -u

# actual python binary that we'll call
PYTHON=/usr/bin/python

# only actually print debug info if $PYTHON_STRIPPED_ENV_DEBUG is defined
function debug {
    if [ ! -z ${PYTHON_STRIPPED_ENV_DEBUG:-} ]; then
        echo "[python-stripped-env] $1"
    fi
}

debug "which python: $(which python)"

# unset all $LD_* environment variables
for env_var in $(env | egrep '^LD_' | cut -f1 -d'='); do
    debug "Unsetting \$$env_var (value was: ${!env_var})"
    unset $env_var
done

debug "All \$PYTHON* environment variables that matter are ignored by using 'python -E'"
debug "Found defined \$PYTHON* environment variables:"
for env_var in $(env | grep '^PYTHON' | cut -f1 -d'=' | grep -v PYTHON_STRIPPED_ENV_DEBUG); do
    debug "> \$$env_var was: ${!env_var}"
done

debug "$PYTHON -E $(echo "$@")"
$PYTHON -E "$@"
