#!/usr/bin/env python3
"""
whisper_launcher.py — WhisperLiveKit startup wrapper

Applies two compatibility fixes before importing WhisperLiveKit:

  1. FFmpeg path — adds imageio-ffmpeg's bundled binary to PATH so
     WhisperLiveKit's ffmpeg_manager can find ffmpeg without a system
     install.  If imageio-ffmpeg is absent it falls back to the system PATH.

  2. torchaudio shim — diart/audio.py calls torchaudio.set_audio_backend()
     at import time, which was removed in torchaudio 2.x.  We inject a no-op
     before WhisperLiveKit (and therefore diart) is imported.
"""

import os
import sys

# ── Fix 1: make ffmpeg available via imageio-ffmpeg bundled binary ────────────

try:
    import imageio_ffmpeg  # type: ignore
    _ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe()
    _ffmpeg_dir = os.path.dirname(_ffmpeg_exe)
    os.environ["PATH"] = _ffmpeg_dir + os.pathsep + os.environ.get("PATH", "")
    print(f"[Launcher] ffmpeg: {_ffmpeg_exe}")
except ImportError:
    print("[Launcher] WARNING: imageio-ffmpeg not installed — system ffmpeg must be in PATH.")
    print("[Launcher]          Run:  pip install imageio-ffmpeg")
except Exception as exc:
    print(f"[Launcher] WARNING: could not locate imageio-ffmpeg binary: {exc}")

# ── Fix 2: diart torchaudio compatibility shim ────────────────────────────────

try:
    import torchaudio  # type: ignore
    if not hasattr(torchaudio, "set_audio_backend"):
        torchaudio.set_audio_backend = lambda backend: None
        print("[Launcher] Patched torchaudio.set_audio_backend (diart compatibility)")
except ImportError:
    pass  # torchaudio not present; WhisperLiveKit will report the error itself

# ── Start WhisperLiveKit ──────────────────────────────────────────────────────

from whisperlivekit.cli import main  # type: ignore
sys.exit(main())
