#!/bin/bash
# =============================================================================
# PokeCord-Worlds.sh - the Linux / Steam Deck launcher (owner, 2026-10-06: a Steam Deck tester asked for a .sh).
# Opens the live game in Chrome (or Chromium) as a fullscreen app window: no address bar, no tabs. No game code here;
# it is the same site as the browser, the Discord Activity and the Windows app.
#
# Steam Deck: in Desktop Mode, right-click this file -> Properties -> Permissions -> "Is executable" (or run
# `chmod +x PokeCord-Worlds.sh`), then Steam -> Add a Game -> Add a Non-Steam Game -> Browse (file type: All files)
# -> pick this file. In its Properties, do NOT force a Proton/compatibility tool: it is a Linux script.
# In Game Mode, set its controller layout to "Gamepad" so the game sees a controller, not a mouse.
# =============================================================================
URL="https://pokecord-worlds.azimpathan.com"
FLAGS=(--app="$URL" --start-fullscreen --no-first-run)

# Chrome first (it gives the Deck its 3D view; Firefox there had WebGL 2 switched off), then Chromium,
# each as a Flatpak (Discover's) or a normal install; else the default browser
#
# Controllers (2026-10-06: the Deck's controller was not detected): a Flatpak browser's sandbox hides /run/udev, so
# Chrome can't see the Steam Input controller. The usual Steam Deck fix (the same as for Xbox Cloud Gaming in Chrome)
# is to let the browser read it; done here every launch (it is per-user, only for that browser, and harmless to repeat).
if command -v flatpak >/dev/null 2>&1 && flatpak info com.google.Chrome >/dev/null 2>&1; then
    flatpak --user override --filesystem=/run/udev:ro com.google.Chrome
    exec flatpak run com.google.Chrome "${FLAGS[@]}"
elif command -v flatpak >/dev/null 2>&1 && flatpak info org.chromium.Chromium >/dev/null 2>&1; then
    flatpak --user override --filesystem=/run/udev:ro org.chromium.Chromium
    exec flatpak run org.chromium.Chromium "${FLAGS[@]}"
elif command -v google-chrome >/dev/null 2>&1; then
    exec google-chrome "${FLAGS[@]}"
elif command -v chromium >/dev/null 2>&1; then
    exec chromium "${FLAGS[@]}"
else
    exec xdg-open "$URL"
fi
