#!/bin/bash

set -e

echo "==================================="
echo "Bluehound Build Script"
echo "==================================="
echo

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# vendored simpleble static lib
if [ ! -f "${SCRIPT_DIR}/simpleble/simpleble/build/lib/libsimpleble.a" ]; then
    echo "❌ vendored SimpleBLE not found at simpleble/simpleble/build/lib/libsimpleble.a"
    exit 1
fi

# vendored cpp-httplib header
if [ ! -f "${SCRIPT_DIR}/third_party/httplib.h" ]; then
    echo "❌ vendored httplib.h not found at third_party/httplib.h"
    exit 1
fi

# cmake toolchain
if ! command -v cmake >/dev/null 2>&1; then
    echo "❌ cmake not found (need >= 3.15)"
    echo "  macOS: brew install cmake"
    echo "  Ubuntu/Debian: sudo apt-get install cmake"
    exit 1
fi

echo "✓ Dependencies found"
echo

# Create build directory
mkdir -p build
cd build

# Configure
echo "Configuring..."
cmake ..

# Build
echo "Building..."
make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)

echo
echo "==================================="
echo "✓ Build complete!"
echo "==================================="
echo
echo "Run the scanner:"
echo "  ./build/bluehound"
echo
echo "Install system-wide:"
echo "  sudo make install"
echo
