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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib.sh
source "${SCRIPT_DIR}/lib.sh"

echo "[host] prereq check"

require_cmd docker
require_cmd bash

if ! docker info >/dev/null 2>&1; then
  echo "docker is not available (docker info failed)" >&2
  exit 1
fi

if ! docker compose version >/dev/null 2>&1; then
  echo "docker compose is not available" >&2
  exit 1
fi

if ! command -v nvidia-smi >/dev/null 2>&1; then
  echo "WARN: nvidia-smi not found on host; GPU validation skipped"
else
  echo "[host] GPU summary"
  nvidia-smi -L || true
fi

echo "[host] ensure shared dirs exist under ../../shared"
mkdir -p "${ROOT_DIR}/../../shared"/{datasets,hf,jobs,outputs,ray,common,user}
mkdir -p "${ROOT_DIR}/../../shared/common"/{code,datasets,models}
mkdir -p "${ROOT_DIR}/../../shared/user"/{code}
# v3.6: W&B local server persists metadata under /private/common/wandb (mounted as /vol).
# The wandb container runs as a non-root user; ensure the bind mount is writable.
mkdir -p "${ROOT_DIR}/../../shared/common/wandb"
chmod 777 "${ROOT_DIR}/../../shared/common/wandb" 2>/dev/null || true

echo "[host] ensure verl repo exists under ../../verl (required by prepare scripts)"
if [[ ! -d "${ROOT_DIR}/../../verl" ]]; then
  echo "missing ../../verl. On remote, ensure /home2/argus/infra/mvp/verl exists (git clone volcengine/verl)." >&2
  exit 1
fi

echo "ok"
