#!/usr/bin/env python3
import os, shutil, subprocess, sys

APP_URL = "https://retrochat.org/app.html"
home = os.path.expanduser("~")
profile = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.join(home, ".config")), "retrochat-2010")
os.makedirs(profile, exist_ok=True)

browsers = [
    "google-chrome-stable",
    "google-chrome",
    "chromium-browser",
    "chromium",
    "brave-browser",
    "microsoft-edge-stable",
    "microsoft-edge",
]

for b in browsers:
    path = shutil.which(b)
    if path:
        subprocess.Popen(
            [
                path,
                f"--app={APP_URL}",
                f"--user-data-dir={profile}",
                "--window-size=1060,720",
                "--class=RetroChat",
                "--no-first-run",
            ],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            start_new_session=True,
        )
        sys.exit(0)

xdg = shutil.which("xdg-open")
if xdg:
    subprocess.Popen([xdg, APP_URL], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True)
    sys.exit(0)

print(f"RetroChat: Please open {APP_URL} in your browser.")
