# What the desktop is made of, started once labwc is up. Everything logs into the session log.
# With ~/.droiddeck-desktop-debug present, the panel and the file manager also log what they
# say to the compositor about their windows (shell, layer, output), capped so it stays readable.
run() {
  local tag=$1; shift
  if [ -f "$HOME/.droiddeck-desktop-debug" ]; then
    # Everything it says goes to ~/.cache/droiddeck/<tag>.log; the window-related protocol
    # traffic also goes into the session log, capped.
    mkdir -p "$HOME/.cache/droiddeck"
    WAYLAND_DEBUG=1 "$@" 2>&1 | tee "$HOME/.cache/droiddeck/$tag.log" | awk -v t="[$tag]" '!/^\[[0-9:.]+\] +\{/ || (c<120 && /layer_s|xdg_toplevel|fullscreen|wl_output|xdg_output|configure\(/) {print t, $0; c++; fflush()}' &
  else
    "$@" 2>&1 | sed "s/^/[$tag] /" &
  fi
}
# The output at the app's real size, in case rc.xml's mode did not take.
[ -n "${BL_WIDTH:-}" ] && wlr-randr --output WL-1 --custom-mode "${BL_WIDTH}x${BL_HEIGHT}" 2>&1 | sed 's/^/[wlr-randr] /'
# In debug runs, ground truth on the network from inside the session: name lookup, then a
# fetch over IPv4 and over IPv6, so a browser's "no internet" can be placed.
if [ -f "$HOME/.droiddeck-desktop-debug" ]; then
  (
    echo "[net] getent: $(getent hosts example.com 2>&1 | head -2 | tr '\n' ' ')"
    for f in 4 6; do
      echo "[net] curl -$f: $(curl -$f -sS -m 8 -o /dev/null -w '%{http_code} %{remote_ip}' https://example.com 2>&1)"
    done
  ) &
fi
# In debug runs Firefox (started from the panel or the menu, so it inherits this) writes its
# own network log: name lookups, connects, HTTP transactions, the link/offline decisions.
if [ -f "$HOME/.droiddeck-desktop-debug" ]; then
  mkdir -p "$HOME/.cache/droiddeck"
  export MOZ_LOG="nsHttp:3,nsSocketTransport:3,nsHostResolver:4,NetworkLinkService:5,NetworkConnectivityService:5,nsIOService:5"
  export MOZ_LOG_FILE="$HOME/.cache/droiddeck/firefox-net.log"
fi
swaybg -m fill -i /usr/share/backgrounds/steamdeck.png &
# Desktop icons (pcmanfm-qt manages the desktop surface) and the panel with the app menu.
run pcmanfm-qt pcmanfm-qt --desktop --profile lxqt
run lxqt-panel lxqt-panel
/usr/lib/xdg-desktop-portal-wlr >/dev/null 2>&1 &
# The desktop was asked for with Steam in it (the front end's "Steam Desktop UI", Big Picture's
# "Switch to Desktop"): the client's desktop UI, as the menu's Steam entry opens it. A running
# client just gets the request; on a software desktop it goes back to the app as a Steam session.
[ "${BL_DESKTOP_STEAM:-0}" = 1 ] && run steam /usr/local/bin/droiddeck-steam-launch desktop
# Remote runs, for driving the desktop from a debugger: with ~/.droiddeck-remote-run present (only
# the app or root can create it), each script dropped into ~/.cache/droiddeck/run/ is run once,
# inside the session with its environment, its output tagged into the desktop log.
if [ -f "$HOME/.droiddeck-remote-run" ]; then
  mkdir -p "$HOME/.cache/droiddeck/run"
  (
    while sleep 1; do
      for f in "$HOME"/.cache/droiddeck/run/*.sh; do
        [ -f "$f" ] || continue
        mv -f "$f" "$f.running" || continue
        ( echo "[run] $(basename "$f") started"; bash "$f.running" 2>&1 | sed "s/^/[run] /"; echo "[run] $(basename "$f") exited"; rm -f "$f.running" ) &
      done
    done
  ) &
fi
