#!/bin/bash
set -e

# Check for cmake
if ! command -v cmake &> /dev/null; then
    echo "Error: cmake is not installed. Please install cmake." >&2
    exit 1
fi

# Check for g++
if ! command -v g++ &> /dev/null; then
    echo "Error: g++ is not installed. Please install g++." >&2
    exit 1
fi

# Check for OpenSSL
if ! pkg-config --exists openssl; then
    echo "Error: OpenSSL development libraries not found. Please install libssl-dev." >&2
    exit 1
fi

# Check for nlohmann_json
if ! pkg-config --exists nlohmann_json; then
    echo "Warning: nlohmann_json not found via pkg-config. Make sure it is installed or available to CMake." >&2
fi

BUILD_DIR=build
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"

cmake ..
make -j$(nproc)

if [ -f runner ]; then
    echo "Build successful. Run ./build/runner BASE64COMMAND to test."
else
    echo "Build failed. Check the output above for errors."
    exit 1
fi