#!/usr/bin/env bash
# Run one or more supervised Ralph iterations with Gitea state reconciliation.
# The canonical PRD remains authoritative; Gitea is updated before and after Ralph.
set -u
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
cd "$ROOT"

python3 scripts/ralph_gitea_sync.py sync
ralph_status=0
"$@" || ralph_status=$?
if [ "$ralph_status" -eq 0 ]; then
  python3 scripts/ralph_gitea_sync.py sync
else
  python3 scripts/ralph_gitea_sync.py sync --reset-in-progress
fi
exit "$ralph_status"
