#!/usr/bin/env bash
# ===== entrypoint.sh =====
# Starts any pre-flight checks then exec's the CMD

set -euo pipefail

export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:/opt/rocm/lib"

# Apply AMD/ROCm env overrides before anything loads torch
if [[ "${AMD_COMPUTE:-false}" == "true" ]]; then
    export TORCH_DEVICE="${TORCH_DEVICE:-cuda}"
    export TORCH_DEVICE_MODEL="${TORCH_DEVICE:-cuda}"
    echo "[entrypoint] AMD compute enabled, TORCH_DEVICE=${TORCH_DEVICE}"
fi

# Check torch/ROCm availability
python3 -c "import torch; dev=torch.device('cuda' if torch.cuda.is_available() else 'cpu'); print(f'[entrypoint] PyTorch device: {dev}, ROCm: {torch.version.rocm if hasattr(torch.version, \"rocm\") else \"n/a\"}')" 2>&1

echo "[entrypoint] Starting marker-api on ${HOST:-0.0.0.0}:${PORT:-8001} ..."
exec "$@"
