#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later
#
# pocketds-gamescope-session — SteamOS-style session entry point for the
# AYANEO Pocket DS `steamos` image profile.
#
# This is the Exec= target of the wayland-session desktop file the
# display manager (plasmalogin) autologins into on the steamos profile
# (see 15-pocketds-steam-session.conf). It does two things:
#
#   1. Acts as a session *dispatcher*: reads the user's last
#      "gaming vs desktop" choice (written by steamos-session-select)
#      and, when the user asked for the desktop, hands off to the stock
#      Plasma Wayland session instead of gamescope. This is what makes
#      Steam Big Picture's "Switch to Desktop" — and the desktop's
#      "Return to Gaming Mode" launcher — actually work: each writes the
#      selection then logs out, and this dispatcher runs again on the
#      autologin that follows.
#
#   2. In gaming mode, launches the Flip-DS gamescope fork holding the
#      TOP panel (DSI-1) for Steam Big Picture, and starts Steam in its
#      gamepad UI inside it (pocketds-steam already adds
#      -gamepadui -steamdeck -steampal).
#
# Everything device-specific below can be overridden without rebuilding
# the RPM by dropping shell assignments in
# /etc/pocketds-steam/gamescope-session.conf (sourced if present).

set -u

log() { echo "pocketds-gamescope-session[$$]: $*" >&2 || :; }

# ── session selection (gaming | desktop) ────────────────────────────
STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/pocketds-steam"
SEL_FILE="$STATE_DIR/session"
mkdir -p "$STATE_DIR" 2>/dev/null || :

mode="gaming"
if [ -r "$SEL_FILE" ]; then
    mode="$(cat "$SEL_FILE" 2>/dev/null || echo gaming)"
fi

start_plasma() {
    # The display manager launched us from a wayland-session whose
    # DesktopNames=gamescope; force the Plasma identity back so
    # OnlyShowIn=KDE autostarts (pocketds-plasma-defaults, the
    # touchscreen mapper, …) still fire. startplasma-wayland also sets
    # XDG_CURRENT_DESKTOP=KDE itself, but set it here too for the phase
    # before it takes over.
    export XDG_SESSION_DESKTOP=KDE
    export XDG_CURRENT_DESKTOP=KDE
    export DESKTOP_SESSION=plasma
    # SteamOS-style "Return to Gaming Mode" shortcut on the desktop. An
    # executable copy, so Plasma trusts it; refreshed when the packaged
    # entry changes.
    local desktop_dir rtg=/usr/share/applications/pocketds-return-to-gaming.desktop
    desktop_dir="$(xdg-user-dir DESKTOP 2>/dev/null || echo "$HOME/Desktop")"
    if [ -r "$rtg" ] && ! cmp -s "$rtg" "$desktop_dir/${rtg##*/}"; then
        install -D -m 0755 "$rtg" "$desktop_dir/${rtg##*/}" || :
    fi
    # startplasma-wayland is the same binary plasma.desktop's Exec runs.
    exec /usr/bin/startplasma-wayland
}

if [ "$mode" = "desktop-once" ]; then
    printf 'gaming\n' > "$SEL_FILE" 2>/dev/null || :
    log "one-shot desktop selection — starting Plasma (Wayland)"
    start_plasma
fi
if [ "$mode" = "desktop" ]; then
    log "session selection is 'desktop' — starting Plasma (Wayland)"
    start_plasma
fi

# ── sanity: is the Steam client actually installed for this user? ──
# The steamos image pre-seeds /home/pocketds at build time, so this is
# normally present. If it isn't (fresh user, wiped $STEAMROOT), give
# the bootstrap one synchronous chance — then, if there is STILL no
# client (e.g. no Wi-Fi configured yet on a non-preseeded image), fall
# back to the Plasma desktop instead of handing gamescope a Steam
# launcher that exits immediately. The old behaviour was a black
# screen followed by a silent session exit and a greeter loop; the
# desktop at least lets the user connect Wi-Fi, after which the
# pocketds-steam-bootstrap user unit completes and the next login (or
# "Return to Gaming Mode") lands in Steam.
STEAM_BIN="${HOME}/.local/share/Steam/steamrtarm64/steam"
if [ ! -x "$STEAM_BIN" ] && command -v pocketds-steam-bootstrap >/dev/null 2>&1; then
    log "steam client missing — running bootstrap before starting gamescope"
    pocketds-steam-bootstrap || :
fi
if [ ! -x "$STEAM_BIN" ]; then
    log "no steam client after bootstrap attempt — falling back to Plasma"
    start_plasma
fi

# ── gaming mode: gamescope + Steam Big Picture ──────────────────────
# Defaults tuned for the Pocket DS panel layout (see
# pocketds-userspace/kwinoutputconfig.json):
#   DSI-1 — 1080x1920 native, mounted rotated → 1920x1080 landscape,
#           the main gaming panel. Touch: "generic ft5x06 (44)".
#   DSI-2 — 768x1024 bottom companion panel. Touch:
#           "Goodix Capacitive TouchScreen".
OUTPUT="DSI-1"
# gamescope's internal (pre-rotation) resolution — landscape logical
# size of the rotated top panel.
WIDTH=1920
HEIGHT=1080
REFRESH=120
# Panel is physically rotated 90° vs. its native portrait scanout. The
# DPU can't rotate 90° at scanout, so gamescope rotates in the
# compositor, where "left" is upright on this panel ("right" renders
# upside down).
ORIENTATION="left"
# Bottom-panel touchscreen — kept OFF the top compositor so a thumb on
# the lower screen doesn't drive the Big Picture cursor.
IGNORE_TOUCH="Goodix Capacitive TouchScreen"
# Bottom-panel DRM lease for a Smithay-mode flip-companion. Left off by
# default: the currently packaged flip-companion is a KWin-script Plasma
# app and can't consume the lease, and a single-output gamescope already
# leaves DSI-2 untouched. Set LEASE_BOTTOM=1 (and point COMPANION_CMD at
# a lease-aware companion) once that lands.
LEASE_BOTTOM=0
LEASE_CONNECTOR="DSI-2"
# Extra flags appended verbatim to the gamescope command line.
EXTRA_ARGS=""

# Local overrides.
if [ -r /etc/pocketds-steam/gamescope-session.conf ]; then
    # shellcheck disable=SC1091
    . /etc/pocketds-steam/gamescope-session.conf
fi

GAMESCOPE_ARGS=(
    --prefer-output "$OUTPUT"
    -W "$WIDTH" -H "$HEIGHT"
    -r "$REFRESH"
    --force-orientation "$ORIENTATION"
    --xwayland-count 2
    --steam
)
[ -n "$IGNORE_TOUCH" ] && GAMESCOPE_ARGS+=(--ignore-touch-device "$IGNORE_TOUCH")
[ "$LEASE_BOTTOM" = "1" ] && GAMESCOPE_ARGS+=(--lease-connector "$LEASE_CONNECTOR")
# shellcheck disable=SC2206
[ -n "$EXTRA_ARGS" ] && GAMESCOPE_ARGS+=($EXTRA_ARGS)

# Tell Steam it's in a gamescope session (enables the in-client power
# menu / "Switch to Desktop", matching SteamOS behaviour).
export STEAM_DISPLAY_TRANSFORM_HINT=1
export gamescope_session=1

log "starting gamescope: ${GAMESCOPE_ARGS[*]} -- pocketds-steam"
exec gamescope "${GAMESCOPE_ARGS[@]}" -- /usr/bin/pocketds-steam -steamos3
