#!/usr/bin/env bash
# Build script for mpq_extractor GDExtension on Linux/macOS
set -e

BUILD_TYPE="${1:-Release}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BUILD_DIR="$SCRIPT_DIR/build"

echo "[mpq_extractor] BUILD_TYPE=$BUILD_TYPE"

# Init submodules if needed
if [ ! -f "$SCRIPT_DIR/../../third_party/godot-cpp/CMakeLists.txt" ]; then
    echo "[mpq_extractor] Initializing submodules..."
    cd "$SCRIPT_DIR/../.."
    git submodule update --init --recursive
fi

# Configure
cmake -S "$SCRIPT_DIR" -B "$BUILD_DIR" \
    -DCMAKE_BUILD_TYPE="$BUILD_TYPE"

# Build
cmake --build "$BUILD_DIR" --config "$BUILD_TYPE" --parallel "$(nproc 2>/dev/null || sysctl -n hw.logicalcpu)"

echo ""
echo "[mpq_extractor] Done. Library is in:"
echo "  $SCRIPT_DIR/../../addons/mpq_extractor/bin/"
