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

repo_root="$(git rev-parse --show-toplevel)"
cd "$repo_root"

echo "Agrarian repository storage audit"
echo "Repository: $repo_root"
echo

echo "Project directory sizes:"
du -sh .git Content Source Config Docs Scripts Data 2>/dev/null || true
if [ "${AGRARIAN_AUDIT_FULL_TREE:-0}" = "1" ]; then
  echo
  echo "Full working tree size:"
  du -sh . 2>/dev/null || true
else
  echo
  echo "Full working tree size skipped. Set AGRARIAN_AUDIT_FULL_TREE=1 to include generated folders."
fi
echo

echo "Git object database:"
git count-objects -vH
echo

echo "Largest tracked files in HEAD:"
largest_tracked="$(mktemp)"
git ls-tree -r -l HEAD | sort -k4 -nr > "$largest_tracked"
head -n 25 "$largest_tracked" | awk '{ printf "%10s  %s\n", $4, $5 }'
rm -f "$largest_tracked"
echo

if git lfs version >/dev/null 2>&1; then
  echo "Largest Git LFS objects:"
  largest_lfs="$(mktemp)"
  git lfs ls-files -s |
    sed -E 's/^.*\(([0-9.]+) ([KMGT]?B)\)$/\1\2 &/' |
    sort -hr > "$largest_lfs"
  head -n 25 "$largest_lfs" | sed -E 's/^[0-9.]+[KMGT]?B //'
  rm -f "$largest_lfs"
else
  echo "Git LFS is not installed."
fi
echo

echo "Generated/local directories:"
for path in Binaries Intermediate Saved DerivedDataCache .vs Builds; do
  if [ -e "$path" ]; then
    du -sh "$path" 2>/dev/null || true
  fi
done
echo

cat <<'NOTE'
Policy reminder:
- Do not store packaged builds, raw DEM/lidar/GIS datasets, DerivedDataCache, or
  generated terrain tile caches in GitHub.
- Use Git for source, scripts, metadata, docs, and curated project assets.
- Use DevBox or future object storage for large artifacts and source data.
NOTE
