#!/usr/bin/env python3
"""
automaster.py - Wrapper script with Auto-Venv switching.
"""
import sys
import os
import warnings
from pathlib import Path

# Suppress annoying library warnings
warnings.filterwarnings("ignore", category=UserWarning, module="pkg_resources")
warnings.filterwarnings("ignore", message=".*pkg_resources is deprecated.*")

# --- Resource caps: keep the machine usable during long renders ---
# Cap native BLAS/OMP thread pools (numpy/torch/scipy/soundfile) to a low
# default; set before any heavy import and carried through the venv re-exec.
# Override any var explicitly to raise the ceiling.
os.environ.setdefault("AUTOMASTER_THREADS", "4")
for _v in ("OMP_NUM_THREADS", "MKL_NUM_THREADS", "OPENBLAS_NUM_THREADS",
           "NUMEXPR_NUM_THREADS", "VECLIB_MAXIMUM_THREADS"):
    os.environ.setdefault(_v, os.environ["AUTOMASTER_THREADS"])
try:
    os.nice(10)  # lower scheduling priority; persists across the re-exec
except (AttributeError, OSError):
    pass
# ------------------------------------------------------------------

# --- AUTO-VENV SWITCHER ---
script_dir = Path(__file__).parent.absolute()
project_root = script_dir.parent
venv_python = project_root / "venv310" / "bin" / "python"

if venv_python.exists() and sys.executable != str(venv_python):
    # Re-execute using the virtual environment's python
    os.execv(str(venv_python), [str(venv_python)] + sys.argv)
# --------------------------

# If we are here, we are in the correct environment
sys.path.insert(0, str(project_root))

from tools.automaster_app.main import main

if __name__ == "__main__":
    main()
