#!/usr/bin/env bash
# tt-gen — launcher for the tt-local-generator UI.
#
# Selects the system Python3 (/usr/bin/python3) explicitly so the app
# always gets the GTK4/GI bindings installed system-wide, regardless of
# any active virtualenv.
#
# Usage:
#   ./tt-gen            # launch the UI
#   ./tt-gen --help     # pass flags through to main.py

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Select the Python3 interpreter that has GTK4/GI bindings.
#
# Linux: GTK4 bindings are installed as system packages (python3-gi via apt) into
#        /usr/bin/python3's site-packages — venvs cannot see them.
#
# macOS: bin/setup_macos.sh creates .venv/ with --system-site-packages, which
#        inherits gi/PyGObject from Homebrew and adds requests/markovify via pip.
#        Use .venv/bin/python3 when it exists; fall back to the PATH python3
#        (e.g. /opt/homebrew/bin/python3) for users who haven't run setup yet.
if [[ "$(uname)" == "Darwin" ]]; then
    VENV_PYTHON="$SCRIPT_DIR/.venv/bin/python3"
    if [[ -x "$VENV_PYTHON" ]]; then
        PYTHON="$VENV_PYTHON"
    else
        PYTHON=$(command -v python3 2>/dev/null || echo "/opt/homebrew/bin/python3")
    fi
else
    PYTHON=/usr/bin/python3
fi

if [[ ! -x "$PYTHON" ]]; then
    echo "ERROR: $PYTHON not found." >&2
    if [[ "$(uname)" == "Darwin" ]]; then
        echo "       Run: ./bin/setup_macos.sh" >&2
    else
        echo "       Install python3 via your package manager." >&2
    fi
    exit 1
fi

# Sanity-check that GTK introspection bindings are available.
if ! "$PYTHON" -c "import gi" 2>/dev/null; then
    echo "ERROR: Python GObject introspection not found for $PYTHON." >&2
    if [[ "$(uname)" == "Darwin" ]]; then
        echo "       Run: ./bin/setup_macos.sh" >&2
    else
        echo "       Run: sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-4.0" >&2
    fi
    exit 1
fi

# macOS: tell GStreamer where Homebrew installed its plugins.
# Without these variables GTK4's GStreamer media backend can't find the
# H.264 decoder (avdec_h264) or the MP4 demuxer (qtdemux), so Gtk.Video
# shows a blank frame even though the file is present locally.
if [[ "$(uname)" == "Darwin" ]]; then
    BREW_PREFIX=$(brew --prefix 2>/dev/null || echo "/opt/homebrew")
    export GST_PLUGIN_PATH="${BREW_PREFIX}/lib/gstreamer-1.0"
    export GST_PLUGIN_SCANNER="${BREW_PREFIX}/libexec/gstreamer-1.0/gst-plugin-scanner"
fi

# GTK4 does not support gtk-modules (GTK3-only feature). KDE writes
# colorreload-gtk-module and window-decorations-gtk-module into
# ~/.config/gtk-4.0/settings.ini, which causes GTK4 to attempt loading
# those GTK3 .so files in-process and segfault. Clear the variable so
# GTK4 never tries to load any modules.
export GTK_MODULES=""

# Raise the open-file-descriptor limit so long TT-TV sessions (which cycle
# through many GStreamer pipelines) don't hit the default 1024-FD cap.
ulimit -n 65536 2>/dev/null || true

exec "$PYTHON" "$SCRIPT_DIR/app/main.py" "$@"
