"""Linux runtime setup — must run before importing pystray."""
from __future__ import annotations

import os
import sys
from pathlib import Path


def _prepend_env_path(name: str, paths: list[Path]) -> None:
    existing = os.environ.get(name, "")
    parts = [str(path) for path in paths if path.is_dir()]
    if not parts:
        return
    joined = os.pathsep.join(parts)
    os.environ[name] = joined if not existing else f"{joined}{os.pathsep}{existing}"


def configure_linux_runtime() -> None:
    if sys.platform != "linux":
        return

    os.environ.setdefault("PYSTRAY_BACKEND", "gtk")

    lib_dirs = [
        Path("/usr/lib/x86_64-linux-gnu"),
        Path("/usr/lib64"),
        Path("/usr/lib"),
        Path("/lib/x86_64-linux-gnu"),
    ]
    girepo_dirs = [
        Path("/usr/lib/x86_64-linux-gnu/girepository-1.0"),
        Path("/usr/lib64/girepository-1.0"),
        Path("/usr/lib/girepository-1.0"),
    ]
    _prepend_env_path("LD_LIBRARY_PATH", lib_dirs)
    _prepend_env_path("GI_TYPELIB_PATH", girepo_dirs)
