#!/usr/bin/env bash
set -euo pipefail
base="https://simplavende.com.br/suporte"
reconcile_tmp="$(mktemp)"
php_tmp="$(mktemp)"
trap 'rm -f "$reconcile_tmp" "$php_tmp"' EXIT
curl -fsSL "$base/OperationalHealthService-20260612.php.txt" -o "$php_tmp"
if ! head -n 5 "$php_tmp" | grep -q 'declare(strict_types=1)'; then
  echo 'hotfix_php_source=invalid' >&2
  exit 1
fi
if [ -f /opt/nvr/gateway/src/Domain/Operations/OperationalHealthService.php ]; then
  if [ "$(id -u)" -eq 0 ]; then
    install -o nvr -g nvr -m 0644 "$php_tmp" /opt/nvr/gateway/src/Domain/Operations/OperationalHealthService.php
  else
    sudo install -o nvr -g nvr -m 0644 "$php_tmp" /opt/nvr/gateway/src/Domain/Operations/OperationalHealthService.php
  fi
fi
curl -fsSL "$base/nvr-gateway-reconcile-20260612.sh" -o "$reconcile_tmp"
if [ "$(id -u)" -eq 0 ]; then
  bash "$reconcile_tmp"
else
  sudo -E bash "$reconcile_tmp"
fi
