#!/bin/sh
#
# Copyright (C) 2026 SliTaz GNU/Linux - BSD License
# Author: Christophe Lincoln <pankso@slitaz.org>
# AI assistant: OpenCode
# Reviewer: Claude (Anthropic)
#
# lxtaz-dash-openbox-setup — install the Super -> lxtaz-dash keybind in Openbox.
# Usage: lxtaz-dash-openbox-setup [--system]
#   default: ~/.config/openbox/rc.xml   |   --system: /etc/xdg/openbox/rc.xml
# Idempotent: does nothing if a Super_L keybind to lxtaz-dash already exists.
RC="$HOME/.config/openbox/rc.xml"
[ "$1" = "--system" ] && RC="/etc/xdg/openbox/rc.xml"

if [ ! -f "$RC" ]; then
	echo "lxtaz-dash-openbox-setup: $RC not found, nothing to do." >&2
	exit 1
fi
if grep -q 'lxtaz-dash --toggle' "$RC"; then
	echo "lxtaz-dash-openbox-setup: keybind already present in $RC."
	exit 0
fi
cp -a "$RC" "$RC.bak-lxtaz-dash" || exit 1
# Insert before the </keyboard> closing tag
awk '
/<\/keyboard>/ && !done {
	print "    <keybind key=\"Super_L\">"
	print "      <action name=\"Execute\">"
	print "        <command>lxtaz-dash --toggle</command>"
	print "      </action>"
	print "    </keybind>"
	print "    <keybind key=\"Super_R\">"
	print "      <action name=\"Execute\">"
	print "        <command>lxtaz-dash --toggle</command>"
	print "      </action>"
	print "    </keybind>"
	done=1
}
{ print }
' "$RC.bak-lxtaz-dash" > "$RC" || exit 1
echo "lxtaz-dash-openbox-setup: Super keybind installed in $RC (backup: $RC.bak-lxtaz-dash)."
echo "Reload Openbox: openbox --reconfigure"
openbox --reconfigure 2>/dev/null
