#!/usr/bin/env bash
# H200 native vllm-dspark benchmark orchestrator.
# This wraps the legacy scripts/benchmark_dspark_0707/ grid benchmark so that
# results live under experiments/dsv4_h200_dspark/results/<RUN_ID>/.

set -Eeuo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXPERIMENT_NAME="$(basename "$SCRIPT_DIR")"

# shellcheck source=/dev/null
source "${SCRIPT_DIR}/../../../scripts/common/lib.sh"
# shellcheck source=/dev/null
source "${SCRIPT_DIR}/../../../scripts/common/platform.sh"
# shellcheck source=/dev/null
source "${SCRIPT_DIR}/config.env"

RUN_ID="${RUN_ID:-$(date '+%Y%m%d-%H%M%S')}"
RESULT_ROOT="${RESULT_ROOT:-${SCRIPT_DIR}/results/${RUN_ID}}"
LOG_DIR="${RESULT_ROOT}/logs"

ensure_result_root "$RESULT_ROOT"
log_init "${LOG_DIR}/orchestrator.log"

log "experiment=${EXPERIMENT_NAME}"
log "run_id=${RUN_ID}"
log "result_root=${RESULT_ROOT}"
log "platform=${PLATFORM}"
log "chip=${CHIP}"
log "accelerator=${ACCELERATOR}"
log "engine=${ENGINE}"
log "hardware=${HARDWARE}"
log "model=${MODEL_PATH}"
log "server_start_script=${SERVER_START_SCRIPT}"

# Write initial metadata for this run.
METADATA_JSON="${RESULT_ROOT}/results.json"
write_metadata_json \
  "$METADATA_JSON" \
  "$EXPERIMENT_NAME" \
  "$RUN_ID" \
  "$MODEL_PATH" \
  "$BACKEND" \
  "$ENGINE" \
  "$HARDWARE" \
  "$ACCELERATOR" \
  "$CHIP" \
  "experiments/${EXPERIMENT_NAME}/run_bench.sh" \
  "$VENV_SERVER" \
  "H200 native vllm-dspark benchmark for DeepSeek-V4-Flash-DSpark"

log "starting legacy DSpark grid benchmark"
RUN_ID="$RUN_ID" \
RESULT_ROOT="$RESULT_ROOT" \
PORT="$PORT" \
MODEL_PATH="$MODEL_PATH" \
SERVER_START_SCRIPT="$SERVER_START_SCRIPT" \
SKIP_MANAGE_SERVER="${SKIP_MANAGE_SERVER:-}" \
  bash "$LEGACY_GRID_SCRIPT" >> "${LOG_DIR}/legacy_grid.log" 2>&1

log "parsing results"
"$VENV_CLIENT/bin/python" "$LEGACY_PARSE_SCRIPT" "$RESULT_ROOT" >> "${LOG_DIR}/parse.log" 2>&1 || {
  log "WARNING: legacy parser failed; see ${LOG_DIR}/parse.log"
}

log "all results saved to ${RESULT_ROOT}"
