#!/usr/bin/env bash
# large-files.sh - lists the biggest files above a size threshold.
# READ-ONLY: finds and prints, deletes nothing.
# Run: bash large-files.sh [path] [min] [top]
#   min accepts forms like 100M, 1G (find -size syntax, default 100M)

set -u

PATH_TO_SCAN="${1:-$HOME}"
MIN="${2:-100M}"
TOP="${3:-20}"

# GNU find wants lowercase k (uppercase K is invalid); normalize it.
case "$MIN" in
  *K) MIN="${MIN%K}k" ;;
esac

if [ ! -d "$PATH_TO_SCAN" ]; then
  echo "Path not found: $PATH_TO_SCAN" >&2
  exit 1
fi

echo "Scanning: $PATH_TO_SCAN"
echo "Threshold: $MIN per file"
echo "(read-only: large folders take a while)"
echo ""

found=$(find "$PATH_TO_SCAN" -type f -size "+$MIN" 2>/dev/null | head -n "$TOP")

if [ -z "$found" ]; then
  echo "(nothing above the threshold)"
else
  n=$(echo "$found" | wc -l | tr -d ' ')
  echo "Largest files (showing $n):"
  echo "$found" | while IFS= read -r f; do
    bytes=$(wc -c < "$f" 2>/dev/null || echo 0)
    size=$(awk -v b="$bytes" 'BEGIN {if (b>=1073741824) printf "%8.2f GB", b/1073741824; else if (b>=1048576) printf "%8.1f MB", b/1048576; else printf "%8.0f KB", b/1024}')
    printf '%s  %s\n' "$size" "$f"
  done
fi

echo ""
echo "Nothing was deleted or moved. Review each path before you remove anything yourself."