#!/usr/bin/bash
#
# ups/discover.sh — probe USB UPS units and NUT state on the local host
#
# Read-only. Prints everything needed to configure NUT. No changes made.
#
# Usage (run ON the target host via remote.sh):
#   PROX_HOST=pfv-tsys1 bash tests/remote.sh prox-file ups/discover.sh
#
set -uo pipefail

echo "======================================================"
echo "  UPS / NUT Discovery on $(hostname)"
echo "======================================================"

# --- 1. USB UPS devices ------------------------------------------------------
echo ""
echo "--- [1/5] USB UPS devices (lsusb) ---"
lsusb 2>/dev/null | grep -iE "UPS|American Power|Tripp|APC" || echo "  (no UPS devices found in lsusb)"

echo ""
echo "--- [2/5] UPS detail (vendor/product/serial/model) ---"
# Common UPS vendor IDs: 051d (APC), 09ae (Tripp Lite), 0463 (Eaton),
# 06da (MGE), 0764 (Cyber Power)
for vid in 051d 09ae 0463 06da 0764; do
  while read -r bus dev pid; do
    [ -n "$bus" ] || continue
    echo "  --- $bus:$dev ($vid:$pid) ---"
    lsusb -v -s "${bus}:${dev}" 2>/dev/null \
      | grep -iE "iManufacturer|iProduct|iSerial|bcdDevice" \
      | sed 's/^/    /'
  done < <(lsusb 2>/dev/null | awk -v v="$vid" '$0~v{split($2,a,":"); split($4,b,":"); print a[1], b[1], $6}')
done

# --- 2. sysfs paths (for udev rules) -----------------------------------------
echo ""
echo "--- [3/5] sysfs device paths + serials ---"
for d in /sys/bus/usb/devices/*; do
  man=$(cat "$d/manufacturer" 2>/dev/null)
  prod=$(cat "$d/product" 2>/dev/null)
  ser=$(cat "$d/serial" 2>/dev/null)
  vid=$(cat "$d/idVendor" 2>/dev/null)
  pid=$(cat "$d/idProduct" 2>/dev/null)
  if echo "$man $prod" | grep -qiE "apc|tripp|power conversion|ups|eaton|mge|cyber power"; then
    # Resolve stable ID_PATH for udev pinning
    path=$(udevadm info -q property -p "$d" 2>/dev/null | awk -F= '/^ID_PATH=/{print $2}')
    echo "  $d"
    echo "    vendor=$vid product=$pid"
    echo "    manufacturer=$man"
    echo "    product=$prod"
    echo "    serial=$ser"
    echo "    ID_PATH=$path"
  fi
done

# --- 3. HID device nodes -----------------------------------------------------
echo ""
echo "--- [4/5] HID device nodes ---"
ls -la /dev/hidraw* /dev/usb/hiddev* 2>/dev/null || echo "  (no hidraw/hiddev nodes)"

# --- 4. NUT install state ----------------------------------------------------
echo ""
echo "--- [5/5] NUT install + service state ---"
if dpkg -l nut-server nut-client 2>/dev/null | grep -q '^ii'; then
  echo "  NUT installed:"
  dpkg -l nut-server nut-client 2>/dev/null | awk '/^ii/{print "    "$2" "$3}'
else
  echo "  NUT not installed (apt: nut-server nut-client)"
fi

echo ""
echo "  Services:"
for svc in nut-driver nut-server nut-monitor; do
  printf "    %-14s " "$svc:"
  systemctl is-active "$svc" 2>/dev/null || true
done

echo ""
echo "  Existing config:"
# shellcheck disable=SC2012 # ls -la is intentional for human-readable listing
ls -la /etc/nut/ 2>/dev/null | sed 's/^/    /' || echo "    (no /etc/nut)"

echo ""
echo "======================================================"
echo "  Discovery complete."
echo "======================================================"
