#!/bin/bash

set -euo pipefail

debug() {
  if [[ "${DEBUG:-}" == "pyexec" ]]; then
    >&2 echo "[DEBUG=pyexec]" "$@"
  fi
}

if [[ -n "${VIRTUAL_ENV:-}" ]]; then
  debug "Detected VIRTUAL_ENV"
  i=python
elif [[ -r ".venv/bin/activate" ]]; then
  debug "Found .venv, activating"
  source .venv/bin/activate
  i=python
elif command -v python3 >/dev/null; then
  debug "Found python3 in path"
  i=python3
else
  debug "Defaulting to python"
  i=python
fi

exec "$i" "$@"

