#!/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_VALIDATION_TIME=""
HTTP_TIMEOUT_SECS=""
RSYNC_TIMEOUT_SECS=""
RSYNC_MIRROR_ROOT=""
MAX_DEPTH=""
MAX_INSTANCES=""
TRUST_ANCHOR=""
NO_BUILD=0
BIN_DIR="target/release"

usage() {
  cat <<'EOF'
Usage:
  ./scripts/replay_bundle/run_live_bundle_record.sh \
    --rir <name> \
    --tal-path <path> \
    --ta-path <path> \
    [--out-dir <path>] \
    [--base-validation-time <rfc3339>] \
    [--delta-validation-time <rfc3339>] \
    [--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]
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-validation-time) DELTA_VALIDATION_TIME="${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 ;;
    --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_${TS}"
fi

STAGE_BASE="${OUT_DIR}.stage-base"
STAGE_DELTA="${OUT_DIR}.stage-delta"

rm -rf "$OUT_DIR" "$STAGE_BASE" "$STAGE_DELTA"
mkdir -p "$(dirname "$OUT_DIR")"

CAPTURE_BIN="$BIN_DIR/replay_bundle_capture"
DELTA_CAPTURE_BIN="$BIN_DIR/replay_bundle_capture_delta"

if [[ "$NO_BUILD" -eq 0 ]]; then
  echo "[1/3] build release binaries"
  cargo build --release --bin replay_bundle_capture --bin replay_bundle_capture_delta
else
  echo "[1/3] reuse existing binaries from $BIN_DIR"
fi

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

echo "[2/3] record live base bundle into $STAGE_BASE"
BASE_CMD=(
  "$CAPTURE_BIN"
  --rir "$RIR"
  --out-dir "$STAGE_BASE"
  --tal-path "$TAL_PATH"
  --ta-path "$TA_PATH"
)
[[ -n "$BASE_VALIDATION_TIME" ]] && BASE_CMD+=(--validation-time "$BASE_VALIDATION_TIME")
[[ -n "$HTTP_TIMEOUT_SECS" ]] && BASE_CMD+=(--http-timeout-secs "$HTTP_TIMEOUT_SECS")
[[ -n "$RSYNC_TIMEOUT_SECS" ]] && BASE_CMD+=(--rsync-timeout-secs "$RSYNC_TIMEOUT_SECS")
[[ -n "$RSYNC_MIRROR_ROOT" ]] && BASE_CMD+=(--rsync-mirror-root "$RSYNC_MIRROR_ROOT")
[[ -n "$MAX_DEPTH" ]] && BASE_CMD+=(--max-depth "$MAX_DEPTH")
[[ -n "$MAX_INSTANCES" ]] && BASE_CMD+=(--max-instances "$MAX_INSTANCES")
[[ -n "$TRUST_ANCHOR" ]] && BASE_CMD+=(--trust-anchor "$TRUST_ANCHOR")
"${BASE_CMD[@]}"

echo "[3/3] record live delta bundle into $STAGE_DELTA"
DELTA_CMD=(
  "$DELTA_CAPTURE_BIN"
  --rir "$RIR"
  --base-bundle-dir "$STAGE_BASE"
  --out-dir "$STAGE_DELTA"
)
[[ -n "$DELTA_VALIDATION_TIME" ]] && DELTA_CMD+=(--validation-time "$DELTA_VALIDATION_TIME")
[[ -n "$HTTP_TIMEOUT_SECS" ]] && DELTA_CMD+=(--http-timeout-secs "$HTTP_TIMEOUT_SECS")
[[ -n "$RSYNC_TIMEOUT_SECS" ]] && DELTA_CMD+=(--rsync-timeout-secs "$RSYNC_TIMEOUT_SECS")
[[ -n "$RSYNC_MIRROR_ROOT" ]] && DELTA_CMD+=(--rsync-mirror-root "$RSYNC_MIRROR_ROOT")
[[ -n "$MAX_DEPTH" ]] && DELTA_CMD+=(--max-depth "$MAX_DEPTH")
[[ -n "$MAX_INSTANCES" ]] && DELTA_CMD+=(--max-instances "$MAX_INSTANCES")
[[ -n "$TRUST_ANCHOR" ]] && DELTA_CMD+=(--trust-anchor "$TRUST_ANCHOR")
"${DELTA_CMD[@]}"

mv "$STAGE_DELTA" "$OUT_DIR"
rm -rf "$STAGE_BASE"

echo "$OUT_DIR"
