#!/bin/bash
# /usr/bin/pocketds-plasma-defaults
#
# Per-user Plasma 6 defaults for the Pocket DS, applied once at first
# KDE session login. Idempotent: re-running is cheap, and once the
# marker file is written we stop touching the user's config so
# manual customisations stick.
#
# What it sets:
#
#   1. ~/.config/kglobalshortcutsrc  [kwin]
#        Window Fullscreen=F11,F11,Make Window Fullscreen
#
#      Stock Plasma 6 ships the kwin "Window Fullscreen" action with
#      no global shortcut bound; on a handheld where most apps expect
#      F11=fullscreen, bind it kwin-side so it works against any
#      focused window. /etc/skel ships this for fresh accounts, but
#      existing accounts predating the package upgrade still need
#      patching — that's what this autostart handles.
#
#   2. ~/.config/plasma-org.kde.plasma.desktop-appletsrc
#        kickoff → kickerdash  +  [Shortcuts] global=Meta
#
#      The dual-DSI Pocket DS works better with a true fullscreen
#      launcher (Application Dashboard, plugin=org.kde.plasma.kickerdash)
#      than a popup that lands off-screen or on the wrong panel. Swap
#      the panel's kickoff applet to kickerdash and give the widget
#      its own Meta global shortcut, which goes through kglobalaccel
#      (the live-reloadable path, unlike kwinrc[ModifierOnlyShortcuts]).
#
#   3. ~/.config/kwinrc  [ModifierOnlyShortcuts] Meta=
#        deleted if it's still the stock activateLauncherMenu binding.
#
#      Stock Plasma 6 writes that binding to user kwinrc once the
#      launcher widget has been opened. If both that AND the
#      kickerdash widget shortcut from (2) are bound to Meta, every
#      tap fires the launcher twice — open→close — and the visible
#      result is "Meta does nothing". Clear the kwinrc side so only
#      the kglobalaccel widget shortcut remains.
#
# Why a marker file:
#
#   We want each setting to land exactly once. After the user has
#   customised their layout, re-applying our defaults on every login
#   would clobber their changes. The .pocketds-plasma-defaults-applied
#   marker is written on success; subsequent runs short-circuit out.

set -u

MARKER="$HOME/.config/.pocketds-plasma-defaults-applied"
APPLETSRC="$HOME/.config/plasma-org.kde.plasma.desktop-appletsrc"
SHORTCUTS="$HOME/.config/kglobalshortcutsrc"
KWINRC="$HOME/.config/kwinrc"

# Already applied? Nothing to do.
[ -f "$MARKER" ] && exit 0

# Plasma session config doesn't exist yet (first-ever login still
# spinning up). Bail; xdg autostart will fire us again next session.
[ -f "$APPLETSRC" ] || exit 0


# ── 1. F11 → Window Fullscreen ───────────────────────────────────────
want_f11="F11,F11,Make Window Fullscreen"
cur_f11=$(kreadconfig6 --file "$SHORTCUTS" --group kwin --key "Window Fullscreen" 2>/dev/null || true)
if [ "$cur_f11" != "$want_f11" ]; then
    kwriteconfig6 --file "$SHORTCUTS" --group kwin --key "Window Fullscreen" "$want_f11"
    # Notify kglobalaccel live so the current session picks up F11
    # without waiting for the next login. Qt::Key_F11 = 16777268.
    dbus-send --session --type=method_call \
        --dest=org.kde.kglobalaccel /kglobalaccel \
        org.kde.KGlobalAccel.setForeignShortcut \
        array:string:"kwin","Window Fullscreen","KWin","Make Window Fullscreen" \
        array:int32:16777268 >/dev/null 2>&1 || true
fi


# ── 2. kickoff → kickerdash + widget Meta shortcut ───────────────────
# In-place swap is a no-op if there's no kickoff left.
sed -i 's|^plugin=org\.kde\.plasma\.kickoff$|plugin=org.kde.plasma.kickerdash|' "$APPLETSRC"

# Locate the kickerdash applet to set its [Shortcuts] global=Meta.
# Walk back from the plugin= line to the nearest enclosing
# [Containments][N][Applets][M] section header.
kdash_ln=$(grep -nE '^plugin=org\.kde\.plasma\.kickerdash$' "$APPLETSRC" 2>/dev/null | head -1 | cut -d: -f1 || true)
if [ -n "$kdash_ln" ]; then
    hdr=$(awk -v end="$kdash_ln" '/^\[Containments\]\[[0-9]+\]\[Applets\]\[[0-9]+\]$/{h=$0} NR==end{print h; exit}' "$APPLETSRC")
    cid=$(echo "$hdr" | sed -nE 's|.*\[Containments\]\[([0-9]+)\].*|\1|p')
    aid=$(echo "$hdr" | sed -nE 's|.*\[Applets\]\[([0-9]+)\].*|\1|p')
    if [ -n "$cid" ] && [ -n "$aid" ]; then
        cur_meta=$(kreadconfig6 --file "$APPLETSRC" \
            --group Containments --group "$cid" \
            --group Applets      --group "$aid" \
            --group Shortcuts    --key   global 2>/dev/null || true)
        if [ "$cur_meta" != "Meta" ]; then
            kwriteconfig6 --file "$APPLETSRC" \
                --group Containments --group "$cid" \
                --group Applets      --group "$aid" \
                --group Shortcuts    --key   global "Meta"
        fi
    fi
fi


# ── 3. Clear kwinrc[ModifierOnlyShortcuts] Meta if stock ─────────────
cur_kwin_meta=$(kreadconfig6 --file "$KWINRC" \
    --group ModifierOnlyShortcuts --key Meta 2>/dev/null || true)
if [ "$cur_kwin_meta" = "org.kde.plasmashell,/PlasmaShell,org.kde.PlasmaShell,activateLauncherMenu" ]; then
    kwriteconfig6 --file "$KWINRC" \
        --group ModifierOnlyShortcuts --key Meta --delete
    # KWin reads [ModifierOnlyShortcuts] on reconfigure in 6.x.
    qdbus-qt6 org.kde.KWin /KWin reconfigure >/dev/null 2>&1 || true
fi


touch "$MARKER"
exit 0
