#!/usr/bin/env bash
set -Eeuo pipefail
ROUTER_OPS_ROOT='/opt/router-ops'
FILES=(
  'lib/machine-git-count-contract.sh'
  'bin/router-machine-git-facts'
  'bin/router-step-machine-git-guard'
  'tests/test-machine-git-count-contract.sh'
  'policy/machine-git-count-contract-v1.md'
)
for rel in "${FILES[@]}"; do
  rm -f "$ROUTER_OPS_ROOT/$rel"
done
if git -C "$ROUTER_OPS_ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  git -C "$ROUTER_OPS_ROOT" add -A -- "${FILES[@]}"
  if ! git -C "$ROUTER_OPS_ROOT" diff --cached --quiet; then
    git -C "$ROUTER_OPS_ROOT" commit -m 'ROLLBACK_STEP_050M07R13B_HARDEN_MACHINE_GIT_COUNT_CONTRACT: remove Machine Git count contract tools'
    git -C "$ROUTER_OPS_ROOT" push origin "$(git -C "$ROUTER_OPS_ROOT" symbolic-ref --short HEAD)"
  fi
fi
printf 'ROLLBACK_COMPLETE=true\n'
