#!/bin/bash
# Subspace audit + attribution deploy (Ray's order 2026-10-16). Run ONLY on Ray's go. Rollback: bash this script rollback
set -euo pipefail
D=/var/www/data.finestoflines.net/subspace_2026; TS=pre-audit-20261016
if [ "${1:-}" = rollback ]; then
  for f in api.php api-content.php img.php api-sites.php; do [ -f "$D/$f.$TS" ] && cp -a "$D/$f.$TS" "$D/$f" && echo "rolled back $f"; done
  php -l $D/api.php >/dev/null && echo "ROLLBACK OK"; exit 0
fi
for f in api.php api-content.php img.php api-sites.php; do php -l "$D/$f.new" >/dev/null; done
for f in api.php api-content.php img.php api-sites.php; do cp -a "$D/$f" "$D/$f.$TS"; cp "$D/$f.new" "$D/$f"; chown root:root "$D/$f"; chmod 644 "$D/$f"; echo "deployed $f (backup $f.$TS)"; done
echo "EFFECT: $(curl -s -o /dev/null -w '%{http_code}' https://data.finestoflines.net/subspace_2026/api-sites.php/site/inkwell/structure) on api-sites; audit rows now: $(mysql -udata -pncc1701D -N -e 'SELECT COUNT(*) FROM subspace_2026.audit_log' 2>/dev/null)"
