#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 The Dodobian authors
#
# panel-defaults OUTPUT... : print the xfce4-panel channel (xfconf XML) of the Dodo layout, two
# panels on every output named: a top one (Applications, Places, screenshot, tray, sound, clock)
# and a bottom one (show desktop, the windows of that output, its desktops). 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 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) has panels 2n+1 (top) and 2n+2 (bottom) and plugins
# 20n+1 to 20n+20.
printf '  <property name="panels" type="array">\n'
n=0
for o in "$@"; do
	printf '    <value type="int" value="%s"/>\n' $((2 * n + 1)) $((2 * n + 2))
	n=$((n + 1))
done
n=0
for o in "$@"; do
	p=$((20 * n))
	printf '    <property name="panel-%s" type="empty">\n' $((2 * n + 1))
	prop '      ' position string 'p=6;x=0;y=0'
	prop '      ' length uint 100
	prop '      ' position-locked bool true
	prop '      ' size uint 28
	prop '      ' icon-size uint 16
	prop '      ' output-name string "$o"
	ids '      ' plugin-ids $((p + 1)) $((p + 2)) $((p + 3)) $((p + 4)) $((p + 5)) $((p + 6)) $((p + 7))
	printf '    </property>\n'
	printf '    <property name="panel-%s" type="empty">\n' $((2 * n + 2))
	prop '      ' position string 'p=10;x=0;y=0'
	prop '      ' length uint 100
	prop '      ' position-locked bool true
	prop '      ' size uint 30
	prop '      ' icon-size uint 16
	prop '      ' output-name string "$o"
	ids '      ' plugin-ids $((p + 11)) $((p + 12)) $((p + 13)) $((p + 14))
	printf '    </property>\n'
	n=$((n + 1))
done
printf '  </property>\n'

printf '  <property name="plugins" type="empty">\n'
n=0
for o in "$@"; do
	p=$((20 * n))
	# 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))
	printf '    <property name="plugin-%s" type="string" value="clock">\n' $((p + 7))
	prop '      ' mode uint 2
	prop '      ' digital-layout uint 3
	prop '      ' digital-time-format string '%d.%m  %H:%M'
	printf '    </property>\n'

	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'
