#!/bin/sh set -u section() { printf '\n===== %s =====\n' "$1" } run_cmd() { printf '\n$ %s\n' "$*" "$@" 2>&1 || printf '[exit=%s]\n' "$?" } run_shell() { printf '\n$ %s\n' "$1" sh -c "$1" 2>&1 || printf '[exit=%s]\n' "$?" } section "system" run_cmd date run_cmd uname -a run_shell 'cat /etc/os-release 2>/dev/null || true' section "bluetooth binaries" run_shell 'for path in /bin/bluetoothctl /usr/bin/bluetoothctl /usr/sbin/bluetoothd /usr/libexec/bluetooth/bluetoothd /usr/bin/dbus-daemon /bin/dbus-daemon /usr/bin/dbus-send /bin/dbus-send /usr/bin/btmgmt /bin/btmgmt /usr/bin/hciconfig /bin/hciconfig /usr/sbin/rfkill /bin/rfkill; do if [ -e "$path" ]; then ls -l "$path"; fi; done' section "processes" run_shell 'pidof bluetoothd || true' run_shell 'pidof dbus-daemon || true' run_shell 'ps w | grep -E "bluetoothd|dbus-daemon|pipewire|wireplumber|bluealsa" | grep -v grep || true' section "dbus" run_shell 'ls -l /run/dbus/system_bus_socket /var/run/dbus/system_bus_socket 2>/dev/null || true' run_shell 'dbus-send --system --dest=org.freedesktop.DBus --type=method_call --print-reply / org.freedesktop.DBus.ListNames 2>/dev/null | head -n 40' section "bluetoothctl" run_shell 'timeout 8 bluetoothctl --version 2>/dev/null || bluetoothctl --version 2>/dev/null || true' run_shell 'timeout 8 bluetoothctl show' section "kernel" run_shell 'lsmod | grep -i -E "bluetooth|btusb|btmtk|hci_uart|rtl" || true' run_shell 'dmesg | grep -i -E "bluetooth|bluez|hci|rtl_bt|btusb|btmtk" | tail -n 120 || true' section "controllers" run_shell 'hciconfig -a' run_shell 'btmgmt info' run_shell 'rfkill list' section "sysfs" run_shell 'find /sys/class/bluetooth -maxdepth 2 -type f 2>/dev/null | head -n 50 || true' run_shell 'find /sys/class/rfkill -maxdepth 2 -type f 2>/dev/null | head -n 50 || true' section "firmware" run_shell 'find /lib/firmware -maxdepth 3 -type f 2>/dev/null | grep -i -E "rtlbt|bluetooth|mt76|mt79|mediatek" | head -n 120 || true' section "audio stack" run_shell 'ps w | grep -E "pipewire|wireplumber|pulseaudio|bluealsa" | grep -v grep || true' run_shell 'command -v wpctl >/dev/null 2>&1 && wpctl status || true' run_shell 'command -v pactl >/dev/null 2>&1 && pactl info || true' section "done" printf '\nDiagnostic complete.\n'