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

echo "[install_cpp_deps] Checking C++ build dependencies..."

MISSING=()

if ! command -v cmake &>/dev/null; then
    MISSING+=(cmake)
else
    echo "  cmake: $(cmake --version | head -1)"
fi

if ! command -v g++ &>/dev/null; then
    MISSING+=(g++ build-essential)
else
    echo "  g++: $(g++ --version | head -1)"
fi

if ! dpkg -s libglfw3-dev &>/dev/null 2>&1; then
    MISSING+=(libglfw3-dev)
else
    echo "  libglfw3-dev: installed"
fi

if ! dpkg -s libgl1-mesa-dev &>/dev/null 2>&1; then
    MISSING+=(libgl1-mesa-dev)
else
    echo "  libgl1-mesa-dev: installed"
fi

# Optional: mingw for cross-compile
if command -v x86_64-w64-mingw32-g++ &>/dev/null; then
    echo "  mingw-w64: $(x86_64-w64-mingw32-g++ --version | head -1)"
else
    echo "  mingw-w64: not installed (optional, for Windows cross-compile)"
fi

if [ ${#MISSING[@]} -eq 0 ]; then
    echo "[install_cpp_deps] All dependencies satisfied."
    exit 0
fi

echo ""
echo "[install_cpp_deps] Missing packages: ${MISSING[*]}"
echo "[install_cpp_deps] Installing..."
sudo apt-get update -qq
sudo apt-get install -y -qq "${MISSING[@]}"
echo "[install_cpp_deps] Done."
