#!/usr/bin/env sh
# =============================================================================
# sr — launcher
#
# Finds a suitable Python 3 (>= 3.10) and runs the sr package.  Works in three
# layouts, in this order:
#
#   1. zipapp   this file *is* sr.pyz; python runs it directly
#   2. source   a sibling `sr/` package (development checkout)
#   3. install  the package is importable from site-packages
#
# Set SR_PYTHON to pin the interpreter, or SR_PYTHON_MIN to require a newer
# minimum version.  POSIX sh only: no bash-isms, so it also runs on macOS.
# =============================================================================
set -eu

SELF=${0%/*}
if [ "$SELF" = "$0" ]; then
    SELF=.
fi
SELF=$(cd "$SELF" 2>/dev/null && pwd -P)/$(basename "$0")
HERE=$(dirname "$SELF")
SR_PYTHON_MIN=${SR_PYTHON_MIN:-3.10}

_suitable() {
    # $1 = interpreter candidate; prints the path when it is new enough.
    command -v "$1" >/dev/null 2>&1 || return 1
    "$1" -c '
import sys
minimum = tuple(int(part) for part in sys.argv[1].split("."))
sys.exit(0 if sys.version_info[:2] >= minimum else 1)
' "$SR_PYTHON_MIN" >/dev/null 2>&1 && printf '%s\n' "$1"
}

find_python() {
    if [ -n "${SR_PYTHON:-}" ]; then
        found=$(_suitable "$SR_PYTHON" || true)
        if [ -n "$found" ]; then
            command -v "$found"
            return 0
        fi
        echo "sr: SR_PYTHON=$SR_PYTHON is not a Python >= $SR_PYTHON_MIN interpreter" >&2
        return 1
    fi
    for candidate in python3 python3.14 python3.13 python3.12 python3.11 python3.10 python; do
        found=$(_suitable "$candidate" || true)
        if [ -n "$found" ]; then
            command -v "$found"
            return 0
        fi
    done
    return 1
}

if ! PYTHON=$(find_python); then
    cat >&2 <<EOF
sr: no Python >= $SR_PYTHON_MIN interpreter found.

sr is a single-file Python application with no third-party dependencies.
Install Python 3.10 or newer, then re-run, or point sr at an interpreter:

    SR_PYTHON=/opt/python3.11/bin/python3 sr --version

See https://github.com/paulmann/sr-search-replace#installation
EOF
    exit 127
fi

# 1) zipapp: this very file is executable Python.
case "$SELF" in
    *.pyz) exec "$PYTHON" "$SELF" "$@" ;;
esac

# 2) source checkout: a sibling `sr/` package with __main__.py.
if [ -f "$HERE/sr/__main__.py" ]; then
    PYTHONPATH="$HERE${PYTHONPATH:+:$PYTHONPATH}"
    export PYTHONPATH
    exec "$PYTHON" -m sr "$@"
fi
if [ -f "$HERE/../sr/__main__.py" ]; then
    ROOT=$(cd "$HERE/.." && pwd -P)
    PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}"
    export PYTHONPATH
    exec "$PYTHON" -m sr "$@"
fi

# 3) installed package (pip / pipx / system).
if "$PYTHON" -c 'import sr' >/dev/null 2>&1; then
    exec "$PYTHON" -m sr "$@"
fi

cat >&2 <<EOF
sr: cannot find the sr package.

Looked for:
  1. a zipapp at $SELF
  2. a source tree at $HERE/sr/
  3. an installed 'sr' module for $PYTHON

Reinstall sr:
  curl -fsSL https://raw.githubusercontent.com/paulmann/sr-search-replace/main/scripts/install.sh | bash
  # or
  pipx install sr-search-replace
EOF
exit 127
