#!/bin/bash
# /tools/debug-rpi
# Temporary R&D tool to discover the differences between
# the rpi kernel fork vs mainline.

set -eu

TS="$(date -u +%Y%m%dT%H%M%SZ 2>/dev/null || echo no_date)"
OUTROOT="/tmp/rpi5-debug-$TS"
mkdir -p "$OUTROOT"

log() { echo "== $*" >>"$OUTROOT/00-status.txt"; }

# Try to find/mount the boot partition (usually mmcblk0p1, vfat)
BOOTMNT="/mnt/boot"
mkdir -p "$BOOTMNT"
if ! mountpoint -q "$BOOTMNT" 2>/dev/null; then
  for dev in /dev/mmcblk0p1 /dev/mmcblk0p* /dev/sd*a1 /dev/sd*a*; do
    [ -b "$dev" ] || continue
    if mount -t vfat "$dev" "$BOOTMNT" 2>/dev/null; then
      log "Mounted boot partition: $dev at $BOOTMNT"
      break
    fi
  done
fi

# Basic system info
{
  echo "timestamp=$TS"
  uname -a
  echo
  echo "--- cmdline ---"
  cat /proc/cmdline 2>/dev/null || true
  echo
  echo "--- consoles ---"
  cat /proc/consoles 2>/dev/null || true
  echo
  echo "--- devices model ---"
  cat /proc/device-tree/model 2>/dev/null | tr -d '\000' || true
  echo
  echo "--- chosen bootargs (DT) ---"
  cat /proc/device-tree/chosen/bootargs 2>/dev/null | tr -d '\000' || true
} >"$OUTROOT/00-system.txt" 2>/dev/null || true

# Kernel logs
dmesg >"$OUTROOT/01-dmesg.txt" 2>/dev/null || true
dmesg -n 8 2>/dev/null || true

# Modules + net + usb + pci snapshots
lsmod >"$OUTROOT/02-lsmod.txt" 2>/dev/null || true
cat /proc/interrupts >"$OUTROOT/03-interrupts.txt" 2>/dev/null || true
cat /proc/iomem >"$OUTROOT/04-iomem.txt" 2>/dev/null || true

ip link show >"$OUTROOT/05-ip-link.txt" 2>/dev/null || true
ip addr show >"$OUTROOT/06-ip-addr.txt" 2>/dev/null || true
cat /proc/net/dev >"$OUTROOT/07-proc-net-dev.txt" 2>/dev/null || true

# sysfs enumeration clues (these are key for “RP1 over PCIe didn’t appear” cases)
find /sys/bus/pci/devices -maxdepth 2 -type f -name vendor -o -name device -o -name class \
  >"$OUTROOT/10-pci-find.txt" 2>/dev/null || true
ls -l /sys/bus/pci/devices >"$OUTROOT/11-pci-ls.txt" 2>/dev/null || true

ls -l /sys/bus/usb/devices >"$OUTROOT/12-usb-ls.txt" 2>/dev/null || true
ls -l /sys/class/net >"$OUTROOT/13-net-class.txt" 2>/dev/null || true
for i in /sys/class/net/*; do
  [ -d "$i" ] || continue
  IF="$(basename "$i")"
  {
    echo "### $IF"
    cat "$i/address" 2>/dev/null || true
    cat "$i/operstate" 2>/dev/null || true
    cat "$i/speed" 2>/dev/null || true
    cat "$i/carrier" 2>/dev/null || true
    echo
  } >>"$OUTROOT/14-net-details.txt" 2>/dev/null || true
done

# Try to capture the live FDT blob if exposed (super useful)
if [ -r /sys/firmware/fdt ]; then
  dd if=/sys/firmware/fdt of="$OUTROOT/20-live.dtb" bs=1M 2>/dev/null || true
  log "Saved /sys/firmware/fdt -> 20-live.dtb"
fi

# Dump DT as a filesystem tree (even without dtc this is valuable)
if [ -d /proc/device-tree ]; then
  (cd /proc/device-tree && find . -maxdepth 4 -print) >"$OUTROOT/21-dt-tree.txt" 2>/dev/null || true
  # pull a few high-signal nodes if present
  for p in \
    /proc/device-tree/soc \
    /proc/device-tree/axi \
    /proc/device-tree/pcie* \
    /proc/device-tree/*pcie* \
    /proc/device-tree/*rp1* \
    /proc/device-tree/chosen \
    /proc/device-tree/aliases
  do
    [ -e "$p" ] || continue
    OUTP="$(echo "$p" | sed 's#/#_#g')"
    (cd "$p" 2>/dev/null && find . -maxdepth 2 -type f -print) >"$OUTROOT/22${OUTP}_files.txt" 2>/dev/null || true
  done
fi

# Focused dmesg greps for the likely failure areas
grep -Ei "rp1|pcie|pci|xhci|usb|dwc|genet|ether|lan|mdio|phy|firmware|dtb|of:" "$OUTROOT/01-dmesg.txt" \
  >"$OUTROOT/30-dmesg-focus.txt" 2>/dev/null || true

# Copy out to boot partition if mounted
if mountpoint -q "$BOOTMNT" 2>/dev/null; then
  DEST="$BOOTMNT/rpi5-debug-$TS"
  mkdir -p "$DEST"
  cp -a "$OUTROOT/." "$DEST/" 2>/dev/null || true
  sync 2>/dev/null || true
  log "Copied bundle to $DEST"
else
  log "Boot partition not mounted; bundle left at $OUTROOT"
fi

echo "Debug bundle created at $OUTROOT (and copied to boot if mounted)."
exit 0
