#!/bin/bash
set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/../../lib/logging.sh"

log "Installing ydotool (pacman)..."
sudo pacman -S --noconfirm --needed ydotool

log "Installing OpenDeck (AUR)..."
# opendeck is not in the official Arch repos; install the native package from
# the AUR instead of Flatpak so it runs as a plain `opendeck` binary.
# --answerdiff None / --answerclean All suppress interactive PKGBUILD/clean prompts.
yay -S --answerdiff None --answerclean All --noconfirm opendeck

log "Wiring ydotoold + OpenDeck into Hyprland autostart..."
AUTOSTART="$HOME/Dotfiles/desktopenvs/hyprlua/hypr/usr/autostart.lua"
if grep -q "ydotoold\|opendeck\|OpenDeck" "$AUTOSTART"; then
    skip "ydotoold/OpenDeck already present in autostart.lua — skipping."
else
    # Insert before the closing end) so entries appear inside the on("hyprland.start") block
    sed -i 's|^end)$|    hl.exec_cmd("ydotoold")\n    hl.exec_cmd("opendeck")\nend)|' "$AUTOSTART"
    log "Autostart entries added."
fi

log "OpenDeck + ydotool installed."
