#!/usr/bin/env bash
# Genmon bluetooth applet — PNG icon + status, click opens blueman-manager.
# Replaces blueman's StatusNotifier tray icon (which the panel renders too small);
# blueman-applet still runs headless for the agent — see bin/panel/install.sh.

NEXUS_ROOT="$(cd "$(dirname "$(realpath "$0")")/../.." && pwd)"
ICON_DIR="$NEXUS_ROOT/assets/panel-icons"

powered=$(bluetoothctl show 2>/dev/null | awk -F': ' '/Powered:/{print $2; exit}')
connected=$(bluetoothctl devices Connected 2>/dev/null | grep -c '^Device')

if [ "$powered" != "yes" ]; then
    echo "<img>${ICON_DIR}/bluetooth-disabled.png</img>"
    echo "<tool>Bluetooth: off</tool>"
elif [ "$connected" -gt 0 ]; then
    names=$(bluetoothctl devices Connected 2>/dev/null | sed 's/^Device [0-9A-F:]* //')
    echo "<img>${ICON_DIR}/bluetooth-active.png</img>"
    echo "<tool>Bluetooth: connected
${names}</tool>"
else
    echo "<img>${ICON_DIR}/bluetooth-online.png</img>"
    echo "<tool>Bluetooth: on (no devices connected)</tool>"
fi
echo "<click>blueman-manager</click>"
