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

usage() {
  cat <<'EOF'
Usage:
  ./scripts/periodic/run_apnic_rpki_client_round_remote.sh \
    --run-root <path> \
    --round-id <round-XXX> \
    --kind <snapshot|delta> \
    --ssh-target <user@host> \
    --remote-root <path> \
    [--scheduled-at <RFC3339>] \
    [--rpki-client-bin <local path>] \
    [--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=""
RPKI_CLIENT_BIN="${RPKI_CLIENT_BIN:-/home/yuyr/dev/rpki-client-9.7/build-m5/src/rpki-client}"
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 ;;
    --rpki-client-bin) RPKI_CLIENT_BIN="$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; }
[[ -x "$RPKI_CLIENT_BIN" ]] || { echo "rpki-client binary not executable: $RPKI_CLIENT_BIN" >&2; exit 2; }

LOCAL_OUT="$RUN_ROOT/rounds/$ROUND_ID/rpki-client"
REMOTE_REPO="$REMOTE_ROOT/repo"
REMOTE_BIN_DIR="$REMOTE_ROOT/bin"
REMOTE_BIN="$REMOTE_BIN_DIR/rpki-client"
REMOTE_OUT="$REMOTE_ROOT/rounds/$ROUND_ID/rpki-client"
REMOTE_CACHE="$REMOTE_ROOT/state/rpki-client/cache"
REMOTE_STATE_OUT="$REMOTE_ROOT/state/rpki-client/out"
REMOTE_STATE_ROOT="$REMOTE_ROOT/state/rpki-client"

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_BIN_DIR' '$REMOTE_OUT' '$REMOTE_STATE_ROOT'"
  rsync -a "$RPKI_CLIENT_BIN" "$SSH_TARGET:$REMOTE_BIN"
else
  ssh "$SSH_TARGET" "mkdir -p '$REMOTE_BIN_DIR' '$REMOTE_OUT' '$REMOTE_STATE_ROOT'"
fi

ssh "$SSH_TARGET" \
  REMOTE_ROOT="$REMOTE_ROOT" \
  REMOTE_BIN="$REMOTE_BIN" \
  REMOTE_OUT="$REMOTE_OUT" \
  REMOTE_CACHE="$REMOTE_CACHE" \
  REMOTE_STATE_OUT="$REMOTE_STATE_OUT" \
  REMOTE_STATE_ROOT="$REMOTE_STATE_ROOT" \
  KIND="$KIND" \
  ROUND_ID="$ROUND_ID" \
  SCHEDULED_AT="$SCHEDULED_AT" \
  'bash -s' <<'EOS'
set -euo pipefail

cd "$REMOTE_ROOT"
mkdir -p "$REMOTE_OUT"

if [[ "$KIND" == "snapshot" ]]; then
  rm -rf "$REMOTE_CACHE" "$REMOTE_STATE_OUT" "$REMOTE_STATE_ROOT/ta" "$REMOTE_STATE_ROOT/.ta"
fi
mkdir -p "$REMOTE_CACHE" "$REMOTE_STATE_OUT" "$REMOTE_STATE_ROOT/ta" "$REMOTE_STATE_ROOT/.ta"
chmod -R 0777 "$REMOTE_STATE_ROOT"

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"
run_log="$REMOTE_OUT/run.log"
meta_out="$REMOTE_OUT/round-result.json"

set +e
(
  cd "$REMOTE_STATE_ROOT"
  LD_LIBRARY_PATH="$REMOTE_ROOT/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" \
    "$REMOTE_BIN" -vv -t "../../repo/tests/fixtures/tal/apnic-rfc7730-https.tal" -d "cache" "out"
) >"$run_log" 2>&1
exit_code=$?
set -e

if [[ -f "$REMOTE_STATE_OUT/rpki.ccr" ]]; then
  cp "$REMOTE_STATE_OUT/rpki.ccr" "$ccr_out"
fi
if [[ -f "$REMOTE_STATE_OUT/openbgpd" ]]; then
  cp "$REMOTE_STATE_OUT/openbgpd" "$REMOTE_OUT/openbgpd"
fi

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_CACHE" "$REMOTE_STATE_OUT" "$exit_code" "$started_at_ms" "$finished_at_ms"
import json, sys
(
    path,
    round_id,
    kind,
    scheduled_at,
    started_at,
    finished_at,
    cache_path,
    out_path,
    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),
            "remoteCachePath": cache_path,
            "remoteOutPath": out_path,
            "exitCode": int(exit_code),
        },
        fh,
        indent=2,
    )
PY

exit "$exit_code"
EOS

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