#!/usr/bin/env bash
set -euo pipefail

ROOT="${1:-.}"
DRYRUN="${DRYRUN:-1}"   # DRYRUN=0 to actually delete

shopt -s nullglob

declare -A best_ver best_file

# Extract base + version from armbian-*.deb
extract_base_ver() {
  local f="$1" bn base ver
  bn="$(basename -- "$f")"

  [[ "$bn" == armbian-bsp-cli-*.deb ]] || return 1

  base="${bn%%_*}"                 # before first underscore
  ver="${bn#*_}"; ver="${ver%%_*}" # between first and second underscore

  [[ -n "$base" && -n "$ver" ]] || return 1
  printf '%s\t%s\n' "$base" "$ver"
}

# First pass: find newest version per base (across ALL subfolders)
while IFS= read -r -d '' f; do
  read -r base ver < <(extract_base_ver "$f") || continue

  if [[ -z "${best_ver[$base]:-}" ]]; then
    best_ver["$base"]="$ver"
    best_file["$base"]="$f"
  else
    if dpkg --compare-versions "$ver" gt "${best_ver[$base]}"; then
      best_ver["$base"]="$ver"
      best_file["$base"]="$f"
    fi
  fi
done < <(find "$ROOT" -type f -name 'armbian-bsp-*.deb' -print0)

echo "Keeping newest armbian-bsp-* package per base (recursive):"
for base in "${!best_file[@]}"; do
  echo "  $base -> ${best_ver[$base]} ($(basename -- "${best_file[$base]}"))"
done
echo

# Second pass: remove older versions
echo "Removing older armbian-bsp-* packages:"
while IFS= read -r -d '' f; do
  read -r base ver < <(extract_base_ver "$f") || continue
  if [[ "${best_file[$base]}" != "$f" ]]; then
    if [[ "$DRYRUN" == "1" ]]; then
      echo "  DRYRUN rm -f -- $f"
    else
      rm -f -- "$f"
      echo "  rm -f -- $f"
    fi
  fi
done < <(find "$ROOT" -type f -name 'armbian-bsp-*.deb' -print0)

if [[ "$DRYRUN" == "1" ]]; then
  echo
  echo "Dry-run mode. To actually delete:"
  echo "  DRYRUN=0 $0 \"$ROOT\""
fi
