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

usage() {
  cat <<'EOF'
Usage:
  ./scripts/periodic/run_apnic_ours_parallel_round_remote.sh \
    --run-root <path> \
    --round-id <round-XXX> \
    --kind <snapshot|delta> \
    --ssh-target <user@host> \
    --remote-root <path> \
    [--scheduled-at <RFC3339>] \
    [--skip-sync]
EOF
}

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
RUN_ROOT=""
ROUND_ID=""
KIND=""
SSH_TARGET="${SSH_TARGET:-root@47.77.183.68}"
REMOTE_ROOT=""
SCHEDULED_AT=""
SKIP_SYNC=0

while [[ $# -gt 0 ]]; do
  case "$1" in
    --run-root) RUN_ROOT="$2"; shift 2 ;;
    --round-id) ROUND_ID="$2"; shift 2 ;;
    --kind) KIND="$2"; shift 2 ;;
    --ssh-target) SSH_TARGET="$2"; shift 2 ;;
    --remote-root) REMOTE_ROOT="$2"; shift 2 ;;
    --scheduled-at) SCHEDULED_AT="$2"; shift 2 ;;
    --skip-sync) SKIP_SYNC=1; shift 1 ;;
    -h|--help) usage; exit 0 ;;
    *) echo "unknown argument: $1" >&2; usage; exit 2 ;;
  esac
done

[[ -n "$RUN_ROOT" && -n "$ROUND_ID" && -n "$KIND" && -n "$REMOTE_ROOT" ]] || { usage >&2; exit 2; }
[[ "$KIND" == "snapshot" || "$KIND" == "delta" ]] || { echo "--kind must be snapshot or delta" >&2; exit 2; }

LOCAL_OUT="$RUN_ROOT/rounds/$ROUND_ID/ours"
REMOTE_REPO="$REMOTE_ROOT/repo"
REMOTE_OUT="$REMOTE_ROOT/rounds/$ROUND_ID/ours"
REMOTE_WORK_DB="$REMOTE_ROOT/state/ours/work-db"
REMOTE_RAW_STORE="$REMOTE_ROOT/state/ours/raw-store.db"
REMOTE_REPO_BYTES="$REMOTE_ROOT/state/ours/repo-bytes.db"

mkdir -p "$LOCAL_OUT"

if [[ "$SKIP_SYNC" -eq 0 ]]; then
  ssh "$SSH_TARGET" "mkdir -p '$REMOTE_ROOT'"
  rsync -a --delete \
    --exclude target \
    --exclude .git \
    "$ROOT_DIR/" "$SSH_TARGET:$REMOTE_REPO/"
  ssh "$SSH_TARGET" "mkdir -p '$REMOTE_REPO/target/release' '$REMOTE_OUT' '$REMOTE_ROOT/state/ours'"
  rsync -a "$ROOT_DIR/target/release/rpki" "$SSH_TARGET:$REMOTE_REPO/target/release/"
else
  ssh "$SSH_TARGET" "mkdir -p '$REMOTE_OUT' '$REMOTE_ROOT/state/ours'"
fi

ssh "$SSH_TARGET" \
  REMOTE_REPO="$REMOTE_REPO" \
  REMOTE_OUT="$REMOTE_OUT" \
  REMOTE_WORK_DB="$REMOTE_WORK_DB" \
  REMOTE_RAW_STORE="$REMOTE_RAW_STORE" \
  REMOTE_REPO_BYTES="$REMOTE_REPO_BYTES" \
  KIND="$KIND" \
  ROUND_ID="$ROUND_ID" \
  SCHEDULED_AT="$SCHEDULED_AT" \
  'bash -s' <<'EOS'
set -euo pipefail

cd "$REMOTE_REPO"
mkdir -p "$REMOTE_OUT"

if [[ "$KIND" == "snapshot" ]]; then
  rm -rf "$REMOTE_WORK_DB" "$REMOTE_RAW_STORE" "$REMOTE_REPO_BYTES"
fi
mkdir -p "$(dirname "$REMOTE_WORK_DB")"

started_at_iso="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
started_at_ms="$(python3 - <<'PY'
import time
print(int(time.time() * 1000))
PY
)"

ccr_out="$REMOTE_OUT/result.ccr"
cir_out="$REMOTE_OUT/result.cir"
report_out="$REMOTE_OUT/report.json"
run_log="$REMOTE_OUT/run.log"
meta_out="$REMOTE_OUT/round-result.json"

set +e
env RPKI_PROGRESS_LOG=1 RPKI_PROGRESS_SLOW_SECS=0 target/release/rpki \
  --db "$REMOTE_WORK_DB" \
  --raw-store-db "$REMOTE_RAW_STORE" \
  --repo-bytes-db "$REMOTE_REPO_BYTES" \
  --tal-path tests/fixtures/tal/apnic-rfc7730-https.tal \
  --ta-path tests/fixtures/ta/apnic-ta.cer \
  --ccr-out "$ccr_out" \
  --report-json "$report_out" \
  --cir-enable \
  --cir-out "$cir_out" \
  --cir-tal-uri "https://rpki.apnic.net/repository/apnic-rpki-root-iana-origin.cer" \
  >"$run_log" 2>&1
exit_code=$?
set -e

finished_at_iso="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
finished_at_ms="$(python3 - <<'PY'
import time
print(int(time.time() * 1000))
PY
)"

python3 - <<'PY' "$meta_out" "$ROUND_ID" "$KIND" "$SCHEDULED_AT" "$started_at_iso" "$finished_at_iso" "$REMOTE_WORK_DB" "$REMOTE_RAW_STORE" "$exit_code" "$started_at_ms" "$finished_at_ms"
import json, sys
(
    path,
    round_id,
    kind,
    scheduled_at,
    started_at,
    finished_at,
    work_db,
    raw_store,
    exit_code,
    start_ms,
    end_ms,
) = sys.argv[1:]
with open(path, "w", encoding="utf-8") as fh:
    json.dump(
        {
            "roundId": round_id,
            "kind": kind,
            "scheduledAt": scheduled_at or None,
            "startedAt": started_at,
            "finishedAt": finished_at,
            "durationMs": int(end_ms) - int(start_ms),
            "remoteWorkDbPath": work_db,
            "remoteRawStoreDbPath": raw_store,
            "exitCode": int(exit_code),
        },
        fh,
        indent=2,
    )
PY

exit "$exit_code"
EOS

rsync -a "$SSH_TARGET:$REMOTE_OUT/" "$LOCAL_OUT/"
echo "$LOCAL_OUT"
