#!/usr/bin/env bash
# Platform loader.
# Usage: source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../scripts/common/platform.sh"

set -Eeuo pipefail

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

# Auto-detect platform if not set.
if [[ -z "${PLATFORM:-}" ]]; then
  # Capture xpu-smi output first; piping it directly can trigger SIGPIPE under
  # pipefail when grep -q exits early.
  _XPU_SMI_OUT=""
  if command -v xpu-smi >/dev/null 2>&1; then
    _XPU_SMI_OUT=$(xpu-smi 2>/dev/null || true)
  fi
  if [[ -n "$_XPU_SMI_OUT" ]] && grep -qiE "P800|Kunlun" <<< "$_XPU_SMI_OUT"; then
    PLATFORM="kunlun_p800"
  elif lspci 2>/dev/null | grep -qiE "XPU|Kunlun"; then
    PLATFORM="kunlun_p800"
  elif command -v nvidia-smi >/dev/null 2>&1; then
    _GPU_NAME=$(nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -n 1 || true)
    if [[ -n "$_GPU_NAME" ]] && grep -qiE "RTX 6000D|PRO 6000D" <<< "$_GPU_NAME"; then
      PLATFORM="nvidia_rtx6000d"
    elif [[ -n "$_GPU_NAME" ]] && grep -qi "H20" <<< "$_GPU_NAME"; then
      PLATFORM="nvidia_h20"
    else
      PLATFORM="nvidia_h200"
    fi
    unset _GPU_NAME
  else
    echo "ERROR: Could not auto-detect PLATFORM. Set PLATFORM env var explicitly." >&2
    exit 1
  fi
  unset _XPU_SMI_OUT
  echo "Auto-detected platform: ${PLATFORM}"
fi

PLATFORM_FILE="${ROOT_DIR}/platforms/${PLATFORM}.env"
if [[ ! -f "$PLATFORM_FILE" ]]; then
  echo "ERROR: Platform config not found: ${PLATFORM_FILE}" >&2
  exit 1
fi

# shellcheck source=/dev/null
source "$PLATFORM_FILE"

# Export key variables if not already set.
export CHIP="${CHIP:-$PLATFORM}"
export ACCELERATOR="${ACCELERATOR:-$PLATFORM}"
export HARDWARE="${HARDWARE:-$PLATFORM}"
export DEFAULT_PORT="${DEFAULT_PORT:-30000}"
export MODEL_ROOT="${MODEL_ROOT:-/data1/models}"
