#!/bin/bash
# Install NexusOS panel applets — symlink scripts, restore genmon config, wire autostart.

set -e
NEXUS="$HOME/nexus-core"

BIN="$HOME/.local/bin"
AUTOSTART="$HOME/.config/autostart"
PANEL_CFG="$HOME/.config/xfce4/panel"

mkdir -p "$BIN" "$AUTOSTART" "$PANEL_CFG"

# Scripts
for script in network-applet.sh network-popup.py nexus_menu_base.py \
              nexus-applet.sh nexus-popup.py bluetooth-applet.sh; do
    ln -sf "$NEXUS/bin/panel/$script" "$BIN/$script"
done
chmod +x "$NEXUS/bin/panel/network-popup.py"   "$NEXUS/bin/panel/network-applet.sh" \
         "$NEXUS/bin/panel/nexus-popup.py"      "$NEXUS/bin/panel/nexus-applet.sh" \
         "$NEXUS/bin/panel/bluetooth-applet.sh"

# Autostart — popup launchers (symlinks) + nm-tray suppressors (regular files)
ln -sf "$NEXUS/management/autostart/nexus-network-popup.desktop" \
       "$AUTOSTART/nexus-network-popup.desktop"
ln -sf "$NEXUS/management/autostart/nexus-popup.desktop" \
       "$AUTOSTART/nexus-popup.desktop"
cp -f "$NEXUS/management/autostart/nm-tray.desktop"          "$AUTOSTART/nm-tray.desktop"
cp -f "$NEXUS/management/autostart/nm-tray-autostart.desktop" "$AUTOSTART/nm-tray-autostart.desktop"
# Same for nm-applet (network-manager-gnome), which ships its own systray icon
# from /etc/xdg/autostart — the network genmon (plugin-13) replaces it.
cp -f "$NEXUS/management/autostart/nm-applet.desktop"        "$AUTOSTART/nm-applet.desktop"
# Suppress blueman-applet autostart (both the system blueman.desktop and the
# user blueman-applet.desktop). blueman always forces its own tray icon, which
# the panel renders too small; the Bluetooth genmon (plugin-16) replaces it and
# opens blueman-manager on click (which provides the pairing agent on demand).
cp -f "$NEXUS/management/autostart/blueman.desktop"         "$AUTOSTART/blueman.desktop"
cp -f "$NEXUS/management/autostart/blueman-applet.desktop"  "$AUTOSTART/blueman-applet.desktop"


# ── Register the genmon applets into the XFCE panel ────────────────────────
# The network applet's genmon (plugin-13) was added by hand once; the Nexus
# (15) and Bluetooth (16) applets are wired up programmatically via xfconf so a
# fresh install picks them up. No-op on machines without XFCE (e.g. the Windows box).
# place = before|after — where to insert relative to the network applet (13).
register_genmon() {
    local id=$1 place=$2 anchor=13 panel=panel-1
    command -v xfconf-query >/dev/null 2>&1 || { echo "xfconf-query not found — skipping panel wiring."; return 0; }

    # Declare the plugin's type so the panel knows it's a Generic Monitor.
    xfconf-query -c xfce4-panel -p "/plugins/plugin-$id" -t string -s genmon --create

    mapfile -t ids < <(xfconf-query -c xfce4-panel -p "/panels/$panel/plugin-ids" 2>/dev/null | grep -E '^[0-9]+$')
    if [ ${#ids[@]} -eq 0 ]; then
        echo "Panel '$panel' has no plugin-ids array — add plugin-$id manually."; return 0
    fi
    for e in "${ids[@]}"; do [ "$e" = "$id" ] && { echo "genmon plugin-$id already in panel."; return 0; }; done

    local new=() ins=0
    for e in "${ids[@]}"; do
        [ "$e" = "$anchor" ] && [ "$place" = before ] && [ "$ins" = 0 ] && { new+=("$id"); ins=1; }
        new+=("$e")
        [ "$e" = "$anchor" ] && [ "$place" = after ] && [ "$ins" = 0 ] && { new+=("$id"); ins=1; }
    done
    [ "$ins" = 0 ] && new+=("$id")   # anchor absent — append

    local args=(); for v in "${new[@]}"; do args+=(-t int -s "$v"); done
    xfconf-query -c xfce4-panel -p "/panels/$panel/plugin-ids" --force-array "${args[@]}"
    echo "genmon plugin-$id registered."
}

register_genmon 15 before   # Nexus applet, left of the network applet
register_genmon 16 after    # Bluetooth applet, right of the network applet

# Genmon configs. These MUST be written with the panel down: genmon keeps its
# config in memory and rewrites genmon-N.rc when the panel exits, so a copy made
# while it's running is overwritten by the very reload meant to pick it up — a
# newly registered plugin has an empty in-memory config, which is how all three
# applets came back blank (Command=, UseLabel=1, "(genmon)" label).
if command -v xfce4-panel >/dev/null 2>&1; then
    echo "Restarting panel with applet configs…"
    xfce4-panel -q >/dev/null 2>&1 || true
    sleep 1
fi
for id in 13 15 16; do
    cp -f "$NEXUS/management/panel/genmon-$id.rc" "$PANEL_CFG/genmon-$id.rc"
done
if command -v xfce4-panel >/dev/null 2>&1; then
    setsid xfce4-panel >/dev/null 2>&1 < /dev/null &
    sleep 1
fi

# Start the popup daemon now so the first click works without a re-login.
# Pin to the system python3 explicitly: PyGObject (gi) is a system package, and
# install.sh is often run from an activated Promethean venv whose python3 lacks
# gi. At login/panel-click time the shebang resolves against the clean system
# PATH (like the network popup), so only this pre-launch needs the hard path.
if command -v xfce4-panel >/dev/null 2>&1; then
    pkill -f "bin/.*nexus-popup.py" 2>/dev/null || true
    setsid /usr/bin/python3 "$BIN/nexus-popup.py" >/dev/null 2>&1 < /dev/null &
fi

echo "Panel applets installed."
