#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 The Dodobian authors
#
# panel-defaults PRIMARY [OUTPUT...] : print the xfce4-panel channel (xfconf XML) of the Dodo
# layout. The primary output gets two panels: a top one (Applications, Places, screenshot, tray,
# sound, clock) and a bottom one (show desktop, the windows of that output, its desktops). Every
# other output gets the bottom one only, with the clock after its desktops, at the edge, as a second monitor's
# taskbar in Windows. The tray and the sound plugin stay on the primary: both are one per panel
# process (a second sound plugin shows no button). xfce4-panel places a panel by output name, which a package can't
# know, so startup writes this at login for the outputs present; xfconf takes it as the defaults,
# and a user's own settings win per property.
set -u
. /usr/share/dodo-session/common.sh

[ $# -gt 0 ] || { say "usage: panel-defaults PRIMARY [OUTPUT...]"; exit 2; }
for o in "$@"; do
	valid_outputs "$o" || { say "not an output name: $o"; exit 2; }
done

panel_dir=$DODO_DEFAULTS/panel

prop() { printf '%s<property name="%s" type="%s" value="%s"/>\n' "$1" "$2" "$3" "$4"; }
ids() {
	printf '%s<property name="%s" type="array">\n' "$1" "$2"
	shift 2
	for i in "$@"; do
		printf '          <value type="int" value="%s"/>\n' "$i"
	done
	printf '        </property>\n'
}

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-panel" version="1.0">\n'
prop '  ' configver int 2

# Panel and plugin ids: output n (from 0, the primary first) has panels 2n+1 (top, the primary
# only) and 2n+2 (bottom) and plugins 20n+1 to 20n+20, the primary's first.
printf '  <property name="panels" type="array">\n'
printf '    <value type="int" value="1"/>\n'
n=0
for o in "$@"; do
	printf '    <value type="int" value="%s"/>\n' $((2 * n + 2))
	n=$((n + 1))
done
panel() { # id position size output plugin-id...
	printf '    <property name="panel-%s" type="empty">\n' "$1"
	prop '      ' position string "$2"
	prop '      ' length uint 100
	prop '      ' position-locked bool true
	prop '      ' size uint "$3"
	prop '      ' icon-size uint 16
	prop '      ' output-name string "$4"
	shift 4
	ids '      ' plugin-ids "$@"
	printf '    </property>\n'
}
n=0
for o in "$@"; do
	p=$((20 * n))
	if [ $n -eq 0 ]; then
		panel $((2 * n + 1)) 'p=6;x=0;y=0' 28 "$o" \
			$((p + 1)) $((p + 2)) $((p + 3)) $((p + 4)) $((p + 5)) $((p + 6)) $((p + 7))
		panel $((2 * n + 2)) 'p=10;x=0;y=0' 30 "$o" $((p + 11)) $((p + 12)) $((p + 13)) $((p + 14))
	else
		panel $((2 * n + 2)) 'p=10;x=0;y=0' 30 "$o" \
			$((p + 11)) $((p + 12)) $((p + 13)) $((p + 14)) $((p + 15))
	fi
	n=$((n + 1))
done
printf '  </property>\n'

clock() {
	printf '    <property name="plugin-%s" type="string" value="clock">\n' "$1"
	prop '      ' mode uint 2
	prop '      ' digital-layout uint 3
	prop '      ' digital-time-format string '%d.%m  %H:%M'
	printf '    </property>\n'
}
printf '  <property name="plugins" type="empty">\n'
n=0
for o in "$@"; do
	p=$((20 * n))
	if [ $n -eq 0 ]; then
		# Applications: its settings are in xfce4/whiskermenu/defaults.rc, which says why
		printf '    <property name="plugin-%s" type="string" value="whiskermenu"/>\n' $((p + 1))
		printf '    <property name="plugin-%s" type="string" value="directorymenu"/>\n' $((p + 2))
		printf '    <property name="plugin-%s" type="string" value="separator">\n' $((p + 3))
		prop '      ' expand bool true
		prop '      ' style uint 0
		printf '    </property>\n'
		printf '    <property name="plugin-%s" type="string" value="launcher">\n' $((p + 4))
		printf '      <property name="items" type="array">\n'
		printf '        <value type="string" value="%s/dodo-screenshot.desktop"/>\n' "$panel_dir"
		printf '      </property>\n'
		printf '    </property>\n'
		printf '    <property name="plugin-%s" type="string" value="systray">\n' $((p + 5))
		prop '      ' square-icons bool true
		printf '    </property>\n'
		printf '    <property name="plugin-%s" type="string" value="pulseaudio"/>\n' $((p + 6))
		clock $((p + 7))
	else
		clock $((p + 15))
	fi

	printf '    <property name="plugin-%s" type="string" value="showdesktop"/>\n' $((p + 11))
	# The windows of this output only, of its current desktop
	printf '    <property name="plugin-%s" type="string" value="tasklist">\n' $((p + 12))
	prop '      ' include-all-monitors bool false
	prop '      ' include-all-workspaces bool false
	prop '      ' grouping uint 0
	prop '      ' show-labels bool true
	printf '    </property>\n'
	printf '    <property name="plugin-%s" type="string" value="separator">\n' $((p + 13))
	prop '      ' expand bool true
	prop '      ' style uint 0
	printf '    </property>\n'
	printf '    <property name="plugin-%s" type="string" value="pager"/>\n' $((p + 14))
	n=$((n + 1))
done
printf '  </property>\n'
printf '</channel>\n'
