#!/usr/bin/env python3
"""
Ondine Windows Launcher
Ensures proper GI/GStreamer paths before launching the main app.
"""
import os
import sys

def setup_windows_env():
    """Set up environment variables for bundled MSYS2 runtime."""
    if getattr(sys, 'frozen', False):
        # Running as PyInstaller bundle
        bundle_dir = sys._MEIPASS
    else:
        bundle_dir = os.path.dirname(os.path.abspath(__file__))

    # Set GStreamer plugin path
    gst_plugin_path = os.path.join(bundle_dir, 'lib', 'gstreamer-1.0')
    if os.path.isdir(gst_plugin_path):
        os.environ['GST_PLUGIN_PATH'] = gst_plugin_path
        os.environ['GST_PLUGIN_SYSTEM_PATH'] = gst_plugin_path

    # Set GDK pixbuf loaders
    pixbuf_dir = os.path.join(bundle_dir, 'lib', 'gdk-pixbuf-2.0')
    if os.path.isdir(pixbuf_dir):
        loaders_cache = os.path.join(pixbuf_dir, '2.10.0', 'loaders.cache')
        if os.path.isfile(loaders_cache):
            os.environ['GDK_PIXBUF_MODULE_FILE'] = loaders_cache

    # Set XDG data dirs for icon themes
    share_dir = os.path.join(bundle_dir, 'share')
    if os.path.isdir(share_dir):
        os.environ['XDG_DATA_DIRS'] = share_dir

    # Set GI typelib path
    typelib_dir = os.path.join(bundle_dir, 'lib', 'girepository-1.0')
    if os.path.isdir(typelib_dir):
        os.environ['GI_TYPELIB_PATH'] = typelib_dir

    # GTK settings for Windows
    os.environ['GTK_CSD'] = '1'  # Client-side decorations


if __name__ == '__main__':
    setup_windows_env()

    # Import and run the main app
    sys.path.insert(0, os.path.join(
        os.path.dirname(os.path.abspath(__file__)), '..', 'src'))
    from main import main
    sys.exit(main())
