#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 The Dodobian authors
#
# Run by labwc once it is up (labwc -s): the outputs exist now. Writes the per-output wallpaper
# defaults for xfdesktop and the panel layout for xfce4-panel, hands the session environment to
# D-Bus and systemd, and starts dodo-session.target (panel, desktop, kanshi, XDG autostart).
set -u
. /usr/share/dodo-session/common.sh

rt=$(runtime_dir) || exit 1
wall=/usr/share/backgrounds/dodo/wallpaper-lagoon-dodo-mouse.png

# xfdesktop keeps its wallpaper per output name and desktop; the package can't know the names,
# so the defaults are written here for the outputs present at login. Desktops come and go and
# xfdesktop numbers them across all monitors, so every desktop of a monitor shows the wallpaper
# of its desktop 0 (single-workspace-mode). A user's own choice (~/.config/xfce4/xfconf) wins
# per property.
layout=$(wlr-randr --json 2>/dev/null)
outputs=$(printf '%s' "$layout" | jq -r '.[] | select(.enabled) | .name' 2>/dev/null)
x="$rt/xdg/xfce4/xfconf/xfce-perchannel-xml"
mkdir -p "$x"
{
	printf '<?xml version="1.0" encoding="UTF-8"?>\n'
	printf '<!-- Generated at login by dodo-session for the outputs present. Do not edit. -->\n'
	printf '<channel name="xfce4-desktop" version="1.0">\n'
	printf '  <property name="desktop-icons" type="empty">\n'
	printf '    <property name="primary" type="bool" value="true"/>\n'
	printf '  </property>\n'
	printf '  <property name="backdrop" type="empty">\n'
	printf '    <property name="single-workspace-mode" type="bool" value="true"/>\n'
	printf '    <property name="single-workspace-number" type="int" value="0"/>\n'
	printf '    <property name="screen0" type="empty">\n'
	for o in $outputs; do
		valid_outputs "$o" || continue
		printf '      <property name="monitor%s" type="empty">\n' "$o"
		printf '        <property name="workspace0" type="empty">\n'
		printf '          <property name="last-image" type="string" value="%s"/>\n' "$wall"
		printf '          <property name="image-style" type="int" value="5"/>\n'
		printf '          <property name="color-style" type="int" value="0"/>\n'
		printf '        </property>\n'
		printf '      </property>\n'
	done
	printf '    </property>\n  </property>\n</channel>\n'
} > "$x/xfce4-desktop.xml"

# xfce4-panel places a panel by output name too: the full panels on the primary output, a bottom
# one on every other. The primary is the one libxfce4windowing picks for the desktop icons: the
# first name of XFW_PRIMARY_MONITOR present, else a laptop panel, else the output at 0,0, else the
# first.
panel_outputs=
for o in $outputs; do
	valid_outputs "$o" && panel_outputs="$panel_outputs $o"
done
primary=
for o in $(printf '%s' "${XFW_PRIMARY_MONITOR:-}" | tr ',' ' '); do
	case " $panel_outputs " in *" $o "*) primary=$o; break ;; esac
done
if [ -z "$primary" ]; then
	for o in $panel_outputs; do
		case $o in LVDS* | eDP* | PANEL) primary=$o; break ;; esac
	done
fi
if [ -z "$primary" ]; then
	origin=$(printf '%s' "$layout" | jq -r '.[] | select(.enabled and .position.x == 0 and .position.y == 0) | .name' 2>/dev/null)
	for o in $origin; do
		case " $panel_outputs " in *" $o "*) primary=$o ;; esac
	done
fi
if [ -z "$primary" ]; then
	for o in $panel_outputs; do primary=$o; break; done
fi
if [ -n "$primary" ]; then
	others=
	for o in $panel_outputs; do
		[ "$o" = "$primary" ] || others="$others $o"
	done
	# shellcheck disable=SC2086 # a list of checked output names
	"$DODO_LIB/panel-defaults" "$primary" $others > "$x/xfce4-panel.xml" \
		|| say "could not write the panel layout"
else
	say "no outputs: the panel starts with its own defaults"
fi

# D-Bus-activated services (xfconfd, portals) and systemd user units get the session's environment.
vars=
for v in WAYLAND_DISPLAY XDG_CURRENT_DESKTOP XDG_SESSION_TYPE XDG_CONFIG_DIRS XCURSOR_THEME \
	XCURSOR_SIZE QT_QPA_PLATFORM ELECTRON_OZONE_PLATFORM_HINT XKB_DEFAULT_LAYOUT \
	XKB_DEFAULT_VARIANT XKB_DEFAULT_OPTIONS XKB_DEFAULT_MODEL DODO_SESSION \
	XFW_PRIMARY_MONITOR; do
	eval "set=\${$v+x}"
	# shellcheck disable=SC2154 # set is assigned by the eval above
	[ "$set" = x ] && vars="$vars $v"
done
# shellcheck disable=SC2086 # a list of variable names
dbus-update-activation-environment --systemd $vars || say "could not export the environment"
# shellcheck disable=SC2086,SC2116 # echo puts the list of outputs on one line
say "outputs: $(echo $outputs); primary=${XFW_PRIMARY_MONITOR:-auto}"
systemctl --user start dodo-session.target || say "dodo-session.target failed to start"
