#!/bin/bash
#
# MVGAL Cross-Vendor GPU Validation Script
# Generates a hardware validation report for multi-vendor GPU setups
#

set -e

OUTPUT_FILE="${1:-/tmp/mvgal_validation_$(date +%Y%m%d_%H%M%S).md}"

cat > "$OUTPUT_FILE" << EOF
# MVGAL Hardware Validation Report

**Date:** $(date -Iseconds)
**System:** $(uname -r) ($(uname -m))
**MVGAL Version:** $(grep '^version =' Cargo.toml 2>/dev/null || echo "unknown")

## Summary

EOF

# Check if MVGAL kernel module is loaded
if lsmod | grep -q "^mvgal "; then
    echo "MVGAL kernel module: ✅ Loaded" >> "$OUTPUT_FILE"
    MODULE_LOADED=true
else
    echo "MVGAL kernel module: ❌ Not loaded" >> "$OUTPUT_FILE"
    MODULE_LOADED=false
fi

# GPU Detection
echo "" >> "$OUTPUT_FILE"
echo "## GPU Detection" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

if [ -e "/dev/mvgal0" ]; then
    echo "| Vendor | Device | PCI Slot | Driver | PCIe Gen | Width | VRAM | Status |" >> "$OUTPUT_FILE"
    echo "|--------|--------|----------|--------|----------|-------|------|--------|" >> "$OUTPUT_FILE"

    # Parse each GPU from sysfs
    for gpu_idx in 0 1 2 3 4 5 6 7; do
        if [ -d "/sys/class/mvgal/mvgal0/gpu${gpu_idx}" ]; then
            # Get PCI info for this GPU
            pci_path=$(cat "/sys/class/mvgal/mvgal0/gpu${gpu_idx}/pci_path" 2>/dev/null || echo "unknown")
            echo "GPU ${gpu_idx}: ${pci_path}" >> "$OUTPUT_FILE"
        fi
    done
else
    echo "No MVGAL device found at /dev/mvgal0" >> "$OUTPUT_FILE"
fi

# PCIe Topology
echo "" >> "$OUTPUT_FILE"
echo "## PCIe Topology" >> "$OUTPUT_FILE"
echo '```' >> "$OUTPUT_FILE"
lspci -tv 2>/dev/null >> "$OUTPUT_FILE" || echo "lspci not available" >> "$OUTPUT_FILE"
echo '```' >> "$OUTPUT_FILE"

# Vulkan Information
echo "" >> "$OUTPUT_FILE"
echo "## Vulkan" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

MVGAL_VULKAL_ENABLE=1 vulkaninfo --summary 2>&1 | grep -E "(Instance Layers|VK_LAYER_MVGAL|VULKANINFO)" >> "$OUTPUT_FILE" || echo "Vulkan not available or layer not registered" >> "$OUTPUT_FILE"

# P2P Matrix
echo "" >> "$OUTPUT_FILE"
echo "## P2P Connectivity Matrix" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

if command -v mvgal-info &> /dev/null; then
    mvgal-info 2>&1 | grep -A20 "P2P Connectivity" >> "$OUTPUT_FILE" || echo "mvgal-info not available" >> "$OUTPUT_FILE"
else
    echo "mvgal-info not in PATH - run from build directory" >> "$OUTPUT_FILE"
fi

# DMA-BUF / P2P matrix
echo "" >> "$OUTPUT_FILE"
echo "## DMA-BUF / P2P Matrix" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

MATRIX_TOOL=""
for candidate in \
    "./build_output/tools/mvgal-dmabuf-matrix" \
    "./build/tools/mvgal-dmabuf-matrix" \
    "mvgal-dmabuf-matrix"; do
    if command -v "$candidate" &>/dev/null || [ -x "$candidate" ]; then
        MATRIX_TOOL="$candidate"
        break
    fi
done

if [ -n "$MATRIX_TOOL" ]; then
    "$MATRIX_TOOL" 2>/dev/null >> "$OUTPUT_FILE" || echo "mvgal-dmabuf-matrix failed" >> "$OUTPUT_FILE"
else
    echo "Build and run: \`mvgal-dmabuf-matrix\` (tools/mvgal-dmabuf-matrix.c)" >> "$OUTPUT_FILE"
fi

# Test Results
echo "" >> "$OUTPUT_FILE"
echo "## Test Results" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

BUILD_DIR="${MVGAL_BUILD_DIR:-build_output}"
if [ -d "$BUILD_DIR" ] && command -v ctest &>/dev/null; then
    echo '```' >> "$OUTPUT_FILE"
    ctest --test-dir "$BUILD_DIR" --output-on-failure 2>&1 | tail -40 >> "$OUTPUT_FILE" || true
    echo '```' >> "$OUTPUT_FILE"
else
    echo "Run \`ctest --test-dir $BUILD_DIR\` for full test results." >> "$OUTPUT_FILE"
fi

if [ -x "$BUILD_DIR/misc/benchmarks/cross_vendor_compute" ]; then
    echo "" >> "$OUTPUT_FILE"
    echo "## Cross-vendor compute sample" >> "$OUTPUT_FILE"
    echo '```' >> "$OUTPUT_FILE"
    "$BUILD_DIR/misc/benchmarks/cross_vendor_compute" 2>&1 >> "$OUTPUT_FILE" || true
    echo '```' >> "$OUTPUT_FILE"
fi

echo "" >> "$OUTPUT_FILE"
echo "## Known Issues" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

# Check NVIDIA VRAM
if ls /sys/class/drm/card*/device/uevent 2>/dev/null | xargs grep -l "PCI_ID=10de" 2>/dev/null; then
    echo "- NVIDIA VRAM may not be detectable via sysfs (requires NVML)" >> "$OUTPUT_FILE"
fi

echo "" >> "$OUTPUT_FILE"
echo "Validation complete: $OUTPUT_FILE" >> "$OUTPUT_FILE"

echo "Validation report saved to: $OUTPUT_FILE"
cat "$OUTPUT_FILE"
