#!/usr/bin/env bash
# Launcher for mcconsole — runs main.py regardless of the caller's CWD.
# Sources live in mcconsole-src/ when deployed (see ./deploy) or src/ locally.
set -euo pipefail
# Canonicalize first so invocation via a symlink (e.g. /usr/local/bin/mcconsole →
# /usr/local/lib/mcconsole/mcconsole) resolves DIR to the real install tree.
SOURCE="$(readlink -f "${BASH_SOURCE[0]}")"
DIR="$(cd "$(dirname "$SOURCE")" && pwd)"

# Require Python >= 3.10 (mirrors requires-python in pyproject.toml; bash can't
# read the TOML without extra deps and the project is stdlib-only).
PY=""
for cand in python3 python; do
    if command -v "$cand" >/dev/null 2>&1; then
        PY="$cand"
        break
    fi
done
if [ -z "$PY" ]; then
    echo "mcconsole: no python interpreter found on PATH (need Python >= 3.10)" >&2
    exit 1
fi
if ! "$PY" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 10) else 1)'; then
    ver="$("$PY" -c 'import sys; print("%d.%d.%d" % sys.version_info[:3])' 2>/dev/null || echo unknown)"
    echo "mcconsole: Python >= 3.10 required, found $ver ($PY)" >&2
    exit 1
fi

for sub in mcconsole-src src; do
    if [ -f "$DIR/$sub/main.py" ]; then
        exec "$PY" "$DIR/$sub/main.py" "$@"
    fi
done
echo "mcconsole: main.py not found in $DIR/{mcconsole-src,src}" >&2
exit 1
