#!/usr/bin/env bash
set -euo pipefail
ROOT="${1:-/srv/project-frankenstein}"
REQUIRED=(repos service catalog incoming releases/pfes releases/tools recovery/git-bundles tmp artifacts/irreplaceable artifacts/canonical artifacts/expensive artifacts/quarantine artifacts/scratch)
[[ -f "$ROOT/.pf-storage-root.json" ]] || { echo 'missing storage-root marker' >&2; exit 1; }
for d in "${REQUIRED[@]}"; do
  [[ -d "$ROOT/$d" ]] || { echo "missing directory: $ROOT/$d" >&2; exit 1; }
done
read -r total used avail pct mount < <(df -P "$ROOT" | awk 'NR==2 {print $2,$3,$4,$5,$6}')
used_pct=${pct%%%}
free_pct=$((100-used_pct))
echo "root=$ROOT mount=$mount used=$pct free_percent=$free_pct"
if (( free_pct < 15 )); then
  echo 'FAIL: less than 15 percent free' >&2
  exit 1
fi
for repo in "$ROOT"/repos/*.git; do
  [[ -d "$repo" ]] || continue
  echo "checking $repo"
  git --git-dir="$repo" fsck --full
 done
echo 'repository layout verification passed'
