#!/usr/bin/env bash
set -euo pipefail

CONFIG_FILE="${INTER_RP_SYNC_CONFIG:-./inter-rp-sync.env}"
if [[ -f "$CONFIG_FILE" ]]; then
  # shellcheck disable=SC1090
  source "$CONFIG_FILE"
fi

REMOTE200="${REMOTE200:-root@43.110.128.200}"
REMOTE200_ROOT="${REMOTE200_ROOT:-/root/inter-rp-runners}"
PEER_ROOT="${PEER_ROOT:-/root/inter-rp-aggregator/synced-from-200}"
SYNC_INTERVAL_SECS="${SYNC_INTERVAL_SECS:-60}"
MAX_SYNCS="${MAX_SYNCS:--1}"
RPS="${RPS:-routinator rpki-client}"

mkdir -p "$PEER_ROOT"

write_status() {
  local success="$1"
  local message="$2"
  export SYNC_SUCCESS="$success"
  export SYNC_MESSAGE="$message"
  export SYNC_REMOTE200="$REMOTE200"
  export SYNC_REMOTE200_ROOT="$REMOTE200_ROOT"
  python3 - <<'PY' >"$PEER_ROOT/sync-status.json"
import json, socket, datetime
print(json.dumps({
    "schemaVersion": 1,
    "success": __import__("os").environ["SYNC_SUCCESS"] == "true",
    "lastSyncAtRfc3339Utc": datetime.datetime.now(datetime.UTC).replace(microsecond=0).isoformat().replace('+00:00', 'Z'),
    "remoteHost": __import__("os").environ["SYNC_REMOTE200"],
    "remoteRoot": __import__("os").environ["SYNC_REMOTE200_ROOT"],
    "localHost": socket.gethostname(),
    "message": __import__("os").environ["SYNC_MESSAGE"],
}, indent=2))
PY
}

sync_once() {
  local rp_name
  local temp_root="$PEER_ROOT/.sync-tmp-$$"
  rm -rf "$temp_root"
  mkdir -p "$temp_root"
  for rp_name in $RPS; do
    mkdir -p "$temp_root/$rp_name"
    if ! rsync -aL --delete \
      --include='run-meta.json' \
      --include='result.ccr' \
      --include='vrps.csv' \
      --include='vaps.csv' \
      --include='stdout.log' \
      --include='stderr.log' \
      --exclude='*' \
      "$REMOTE200:$REMOTE200_ROOT/$rp_name/latest/" "$temp_root/$rp_name/latest/"; then
      echo "sync failed for $rp_name; clearing stale local latest" >&2
      rm -rf "$PEER_ROOT/$rp_name/latest" "$PEER_ROOT/$rp_name/latest.next" "$PEER_ROOT/$rp_name/latest.prev"
      return 1
    fi
    if [[ ! -d "$temp_root/$rp_name/latest" ]]; then
      echo "missing synced latest directory for $rp_name" >&2
      rm -rf "$PEER_ROOT/$rp_name/latest" "$PEER_ROOT/$rp_name/latest.next" "$PEER_ROOT/$rp_name/latest.prev"
      return 1
    fi
    rm -rf "$PEER_ROOT/$rp_name/latest.next" "$PEER_ROOT/$rp_name/latest.prev"
    mkdir -p "$PEER_ROOT/$rp_name"
    mv "$temp_root/$rp_name/latest" "$PEER_ROOT/$rp_name/latest.next"
    if [[ -e "$PEER_ROOT/$rp_name/latest" ]]; then
      mv "$PEER_ROOT/$rp_name/latest" "$PEER_ROOT/$rp_name/latest.prev"
    fi
    mv "$PEER_ROOT/$rp_name/latest.next" "$PEER_ROOT/$rp_name/latest"
    rm -rf "$PEER_ROOT/$rp_name/latest.prev"
  done
  rm -rf "$temp_root"
}

completed="0"
while true; do
  if sync_once; then
    write_status true "ok"
  else
    rm -rf "$PEER_ROOT/.sync-tmp-$$"
    write_status false "rsync failed"
  fi
  completed=$((completed + 1))
  if [[ "$MAX_SYNCS" =~ ^[0-9]+$ ]] && (( completed >= MAX_SYNCS )); then
    break
  fi
  sleep "$SYNC_INTERVAL_SECS"
done
