#!/usr/bin/env bash
# MENTE launcher — one command, no install required.
#
# Usage: ./mente [run|demo|federated|peer|test|reset]
#        ./mente --help
#
# Defaults to interactive REPL if no subcommand is given.
set -euo pipefail

DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# Prefer python3.11+ if available, otherwise fall back.
PY="${MENTE_PYTHON:-}"
if [ -z "$PY" ]; then
  for cand in python3.13 python3.12 python3.11 python3; do
    if command -v "$cand" >/dev/null 2>&1; then
      PY="$cand"; break
    fi
  done
fi
if [ -z "$PY" ]; then
  echo "error: no python3 interpreter found. install python 3.11+ and retry." >&2
  exit 1
fi

# Check version >= 3.11.
ver=$("$PY" -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")')
maj=${ver%.*}; min=${ver#*.}
if [ "$maj" -lt 3 ] || { [ "$maj" -eq 3 ] && [ "$min" -lt 11 ]; }; then
  echo "error: python 3.11+ required; found $ver ($PY)." >&2
  exit 1
fi

export PYTHONPATH="$DIR/src${PYTHONPATH:+:$PYTHONPATH}"
exec "$PY" -u -m mente.cli "$@"
