Opens a larger view. Escape closes it.

home-server

sunshine-sync-steam.sh

#!/bin/bash
STEAMAPPS="${HOME}/.local/share/Steam/steamapps"
APPS_JSON="${HOME}/.config/sunshine/apps.json"
ASSETS_DIR="${HOME}/.config/sunshine/assets"
SKIP="Steam Linux Runtime|Proton|Steamworks|SteamVR|Steam Controller"

mkdir -p "$ASSETS_DIR"

APPS='[
    {
      "name": "Steam Big Picture",
      "image-path": "steam.png",
      "cmd": "steam -gamepadui -steamos3 -pipewire-dmabuf"
    }'

if [ -d "$STEAMAPPS" ]; then
  while IFS= read -r acf; do
    appid=$(awk -F'"' '/"appid"/{print $4; exit}' "$acf")
    name=$(awk -F'"' '/\t"name"\t/{print $4; exit}' "$acf")
    state=$(awk -F'"' '/"StateFlags"/{print $4; exit}' "$acf")

    (( (state & 4) == 0 )) && continue
    [ -z "$appid" ] || [ -z "$name" ] && continue
    echo "$name" | grep -qE "$SKIP" && continue

    # Download and cache box art as PNG
    IMG="$ASSETS_DIR/game_${appid}.png"
    if [ ! -f "$IMG" ]; then
      TMP="$ASSETS_DIR/game_${appid}.jpg"
      curl -sf -o "$TMP"         "https://steamcdn-a.akamaihd.net/steam/apps/${appid}/library_600x900.jpg" 2>/dev/null ||       curl -sf -o "$TMP"         "https://steamcdn-a.akamaihd.net/steam/apps/${appid}/header.jpg" 2>/dev/null
      if [ -f "$TMP" ]; then
        ffmpeg -i "$TMP" "$IMG" -y 2>/dev/null && rm "$TMP"
        echo "Downloaded art: $name"
      fi
    fi
    [ -f "$IMG" ] && IMG_PATH="$IMG" || IMG_PATH="steam.png"

    name_json=$(echo "$name" | sed 's/\\/\\\\/g; s/"/\\"/g')
    APPS="${APPS},
    {
      \"name\": \"${name_json}\",
      \"image-path\": \"${IMG_PATH}\",
      \"cmd\": \"sunshine-game.sh ${appid}\"
    }"
    echo "Added: $name (AppID: $appid)"
  done < <(find "$STEAMAPPS" -maxdepth 1 -name 'appmanifest_*.acf' 2>/dev/null)
fi

# Snapshot hash before writing
OLD_HASH=$(md5sum "$APPS_JSON" 2>/dev/null | cut -d" " -f1)

cat > "$APPS_JSON" << ENDJSON
{
  "env": {
    "PATH": "\$(PATH):\$(HOME)/.local/bin",
    "DISPLAY": ":0",
    "WAYLAND_DISPLAY": "gamescope-0"
  },
  "apps": ${APPS}
  ]
}
ENDJSON

NEW_HASH=$(md5sum "$APPS_JSON" | cut -d" " -f1)
COUNT=$(grep -c 'image-path' "$APPS_JSON")
echo "apps.json updated: $COUNT apps"

# Restart Sunshine only when app list actually changed
if [ -n "$OLD_HASH" ] && [ "$OLD_HASH" != "$NEW_HASH" ]; then
  echo "App list changed — restarting Sunshine"
  systemctl --user restart sunshine.service \
    && echo "Sunshine restarted" \
    || echo "Sunshine not running, skipping restart"
fi