#!/usr/bin/env python3
"""mtest bootstrap — points the Python interpreter at the
scripts/_mtest package. See `python -m scripts._mtest --help` or
just `./scripts/mtest --help` for usage.

We use the venv's interpreter when available so the supervisor
can import the project package; falls back to the system Python
for the self-check + report subcommands that don't need imports.
"""

import os
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent
VENV_PY = REPO_ROOT / ".venv" / "bin" / "python"

if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

# Re-exec under the venv interpreter so `import mgf.hrb` works for
# tests and the env-fingerprint diagnostic. Skip the re-exec when
# we're already running the venv interpreter.
if VENV_PY.exists() and os.path.realpath(sys.executable) != os.path.realpath(str(VENV_PY)):
    os.execv(str(VENV_PY), [str(VENV_PY), str(Path(__file__)), *sys.argv[1:]])

from scripts._mtest.__main__ import main  # noqa: E402

if __name__ == "__main__":
    sys.exit(main())
