"""vivo.py — desenvolvimento: abre o painel (demo) e reinicia sozinho quando um .py muda.
    python -m primo_voz.vivo
"""
from __future__ import annotations

import subprocess
import sys
import time
from pathlib import Path

AQUI = Path(__file__).resolve().parent


def marca() -> float:
    return max(f.stat().st_mtime for f in AQUI.glob("*.[ph][yt]*"))


def main() -> None:
    ultimo = 0.0
    p = None
    while True:
        m = marca()
        if m != ultimo:
            ultimo = m
            if p and p.poll() is None:
                p.terminate()
                p.wait()
            p = subprocess.Popen([sys.executable, "-m", "primo_voz.hud", "--demo"], cwd=str(AQUI.parent),
                                 stdin=subprocess.DEVNULL)
        time.sleep(0.7)


if __name__ == "__main__":
    main()
