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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT_DIR"

RIR=""
OUT_DIR=""
TAL_PATH=""
TA_PATH=""
BASE_VALIDATION_TIME=""
DELTA_COUNT=""
DELTA_INTERVAL_SECS=""
HTTP_TIMEOUT_SECS=""
RSYNC_TIMEOUT_SECS=""
RSYNC_MIRROR_ROOT=""
MAX_DEPTH=""
MAX_INSTANCES=""
TRUST_ANCHOR=""
NO_BUILD=0
KEEP_DB=0
CAPTURE_INPUTS_ONLY=0
BIN_DIR="target/release"
PROGRESS_LOG="${RPKI_PROGRESS_LOG:-1}"
PROGRESS_SLOW_SECS="${RPKI_PROGRESS_SLOW_SECS:-30}"

usage() {
  cat <<'EOF'
Usage:
  ./scripts/replay_bundle/run_live_bundle_record_sequence.sh \
    --rir <name> \
    --tal-path <path> \
    --ta-path <path> \
    [--out-dir <path>] \
    [--base-validation-time <rfc3339>] \
    [--delta-count <n>] \
    [--delta-interval-secs <n>] \
    [--http-timeout-secs <n>] \
    [--rsync-timeout-secs <n>] \
    [--rsync-mirror-root <path>] \
    [--max-depth <n>] \
    [--max-instances <n>] \
    [--trust-anchor <name>] \
    [--bin-dir <path>] \
    [--no-build] \
    [--keep-db] \
    [--capture-inputs-only]
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --rir) RIR="${2:?}"; shift 2 ;;
    --out-dir) OUT_DIR="${2:?}"; shift 2 ;;
    --tal-path) TAL_PATH="${2:?}"; shift 2 ;;
    --ta-path) TA_PATH="${2:?}"; shift 2 ;;
    --base-validation-time) BASE_VALIDATION_TIME="${2:?}"; shift 2 ;;
    --delta-count) DELTA_COUNT="${2:?}"; shift 2 ;;
    --delta-interval-secs) DELTA_INTERVAL_SECS="${2:?}"; shift 2 ;;
    --http-timeout-secs) HTTP_TIMEOUT_SECS="${2:?}"; shift 2 ;;
    --rsync-timeout-secs) RSYNC_TIMEOUT_SECS="${2:?}"; shift 2 ;;
    --rsync-mirror-root) RSYNC_MIRROR_ROOT="${2:?}"; shift 2 ;;
    --max-depth) MAX_DEPTH="${2:?}"; shift 2 ;;
    --max-instances) MAX_INSTANCES="${2:?}"; shift 2 ;;
    --trust-anchor) TRUST_ANCHOR="${2:?}"; shift 2 ;;
    --bin-dir) BIN_DIR="${2:?}"; shift 2 ;;
    --no-build) NO_BUILD=1; shift ;;
    --keep-db) KEEP_DB=1; shift ;;
    --capture-inputs-only) CAPTURE_INPUTS_ONLY=1; shift ;;
    --help|-h) usage; exit 0 ;;
    *) echo "unknown argument: $1" >&2; usage >&2; exit 2 ;;
  esac
done

if [[ -z "$RIR" || -z "$TAL_PATH" || -z "$TA_PATH" ]]; then
  usage >&2
  exit 2
fi

TS="$(date -u +%Y%m%dT%H%M%SZ)"
if [[ -z "$OUT_DIR" ]]; then
  OUT_DIR="target/replay/${RIR}_live_bundle_sequence_${TS}"
fi

SEQUENCE_BIN="$BIN_DIR/replay_bundle_capture_sequence"
if [[ "$NO_BUILD" -eq 0 ]]; then
  echo "[1/1] build release binary"
  cargo build --release --bin replay_bundle_capture_sequence
else
  echo "[1/1] reuse existing binary from $BIN_DIR"
fi

if [[ ! -x "$SEQUENCE_BIN" ]]; then
  echo "missing executable: $SEQUENCE_BIN" >&2
  exit 1
fi

cmd=(
  "$SEQUENCE_BIN"
  --rir "$RIR"
  --out-dir "$OUT_DIR"
  --tal-path "$TAL_PATH"
  --ta-path "$TA_PATH"
)
[[ -n "$BASE_VALIDATION_TIME" ]] && cmd+=(--base-validation-time "$BASE_VALIDATION_TIME")
[[ -n "$DELTA_COUNT" ]] && cmd+=(--delta-count "$DELTA_COUNT")
[[ -n "$DELTA_INTERVAL_SECS" ]] && cmd+=(--delta-interval-secs "$DELTA_INTERVAL_SECS")
[[ -n "$HTTP_TIMEOUT_SECS" ]] && cmd+=(--http-timeout-secs "$HTTP_TIMEOUT_SECS")
[[ -n "$RSYNC_TIMEOUT_SECS" ]] && cmd+=(--rsync-timeout-secs "$RSYNC_TIMEOUT_SECS")
[[ -n "$RSYNC_MIRROR_ROOT" ]] && cmd+=(--rsync-mirror-root "$RSYNC_MIRROR_ROOT")
[[ -n "$MAX_DEPTH" ]] && cmd+=(--max-depth "$MAX_DEPTH")
[[ -n "$MAX_INSTANCES" ]] && cmd+=(--max-instances "$MAX_INSTANCES")
[[ -n "$TRUST_ANCHOR" ]] && cmd+=(--trust-anchor "$TRUST_ANCHOR")
[[ "$KEEP_DB" -eq 1 ]] && cmd+=(--keep-db)
[[ "$CAPTURE_INPUTS_ONLY" -eq 1 ]] && cmd+=(--capture-inputs-only)

RPKI_PROGRESS_LOG="$PROGRESS_LOG" \
RPKI_PROGRESS_SLOW_SECS="$PROGRESS_SLOW_SECS" \
"${cmd[@]}"
