"""Launcher ensuring Tcl/Tk resources are available before starting ICRS."""

from __future__ import annotations

import os
import shutil
import subprocess
import sys
from pathlib import Path


def _copy_tcl_runtime(venv_root: Path) -> tuple[Path, Path] | None:
    """Copy Tcl/Tk directories from the base interpreter into the venv if needed."""

    base_prefix = Path(getattr(sys, "base_prefix", sys.prefix))
    base_tcl_dir = base_prefix / "tcl"
    if not base_tcl_dir.exists():
        return None

    tcl_src = base_tcl_dir / "tcl8.6"
    tk_src = base_tcl_dir / "tk8.6"
    if not tcl_src.exists() or not tk_src.exists():
        return None

    target_root = venv_root / "tcl"
    tcl_dest = target_root / "tcl8.6"
    tk_dest = target_root / "tk8.6"

    if not tcl_dest.exists():
        shutil.copytree(tcl_src, tcl_dest, dirs_exist_ok=True)
    if not tk_dest.exists():
        shutil.copytree(tk_src, tk_dest, dirs_exist_ok=True)

    return tcl_dest, tk_dest


def main() -> int:
    venv_root = Path(sys.prefix)
    tcl_paths = _copy_tcl_runtime(venv_root)

    env = os.environ.copy()
    if tcl_paths:
        env.setdefault("TCL_LIBRARY", str(tcl_paths[0]))
        env.setdefault("TK_LIBRARY", str(tcl_paths[1]))

    return subprocess.call([sys.executable, "main.py"], env=env)


if __name__ == "__main__":
    raise SystemExit(main())
