#!/usr/bin/env python3
# Grafischer Installer für Stargate SG-1 The Alliance: Redial (Linux, alle Distributionen).
# Startet aus dem entpackten Archiv stargate-redial-<Version>.tar, aus dem Git-Repo oder aus
# <Installationsordner>/scripts. Braucht Python 3.10+ und PySide6 (Qt 6) aus der Distribution.
# Copyright (c) 2026 Gate Wanderers - MIT-Lizenz
import os
import sys

if not sys.flags.isolated:
    # ohne PYTHONPATH, Benutzer-site und aktuellen Ordner neu starten (python3 -I)
    os.execv(sys.executable, [sys.executable, "-I", os.path.realpath(__file__), *sys.argv[1:]])

HERE = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(HERE, "installer"))

PYSIDE_HINT = {
    "de": "Der grafische Installer braucht PySide6 (Qt 6 für Python). Installieren mit:\n  {cmd}\n"
          "Ohne Oberfläche geht es auch im Terminal, siehe README (stargate-redial-build all).",
    "en": "The graphical installer needs PySide6 (Qt 6 for Python). Install it with:\n  {cmd}\n"
          "Without a GUI you can use the terminal instead, see the README (stargate-redial-build all).",
}


# unbekannte Distribution: „pip install --user“ scheitert bei „externally managed“ Python (PEP 668)
PYSIDE_VENV = {
    "de": "Der grafische Installer braucht PySide6 (Qt 6 für Python). Installiere es aus den Paketen deiner "
          "Distribution (meist „python3-pyside6“ oder „pyside6“). Gibt es dort keins, geht es mit einer "
          "eigenen Python-Umgebung:\n  python3 -m venv ~/.local/share/stargate-redial-venv\n"
          "  ~/.local/share/stargate-redial-venv/bin/pip install PySide6\n"
          "  ~/.local/share/stargate-redial-venv/bin/python3 {self}\n"
          "Ohne Oberfläche geht es auch im Terminal, siehe README (stargate-redial-build all).",
    "en": "The graphical installer needs PySide6 (Qt 6 for Python). Install it from your distribution's packages "
          "(usually “python3-pyside6” or “pyside6”). If there is none, use a Python environment of your own:\n"
          "  python3 -m venv ~/.local/share/stargate-redial-venv\n"
          "  ~/.local/share/stargate-redial-venv/bin/pip install PySide6\n"
          "  ~/.local/share/stargate-redial-venv/bin/python3 {self}\n"
          "Without a GUI you can use the terminal instead, see the README (stargate-redial-build all).",
}


def no_pyside():
    import shlex
    import shutil
    import subprocess
    from sgr_installer import deps, i18n
    lang = i18n.detect(None, os.environ)
    cmd = deps.pyside_command(deps.family(deps.read_os_release()))
    text = PYSIDE_HINT[lang].format(cmd=cmd) if cmd else PYSIDE_VENV[lang].format(
        self=shlex.quote(os.path.realpath(__file__)))
    print(text, file=sys.stderr)
    for tool, args in (("zenity", ["--error", "--no-markup", "--text", text]), ("kdialog", ["--error", text])):
        if shutil.which(tool) and (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
            subprocess.run([tool, *args], stdin=subprocess.DEVNULL, check=False)
            break
    return 2


def main():
    if sys.version_info < (3, 10):
        print("stargate-redial-installer: Python 3.10 oder neuer nötig / Python 3.10 or newer required", file=sys.stderr)
        return 2
    try:
        import PySide6.QtWidgets  # noqa: F401
    except ImportError:
        return no_pyside()
    from sgr_installer import app
    return app.main()


if __name__ == "__main__":
    sys.exit(main())
