#!/bin/bash
set -e

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
OUTPUT_DIR="$SCRIPT_DIR/output"
mkdir -p "$OUTPUT_DIR"

# Build for x86_64 with musl static linking
if [[ $(uname -m) == "x86_64" ]]; then
    echo "Building for x86_64 (musl static)..."
    if command -v x86_64-linux-musl-g++ &>/dev/null; then
        x86_64-linux-musl-g++ -O2 -static -o "$OUTPUT_DIR/bb64.amd64" bb64.cpp
    else
        g++ -O2 -static -o "$OUTPUT_DIR/bb64.amd64" bb64.cpp -static-libgcc -static-libstdc++
        echo "Warning: musl-g++ not found, built with g++ static flags."
    fi
    echo "Built bb64.amd64 (x86_64, static)"
fi

# Build for arm64 (musl static) if cross-compiler available
if command -v aarch64-linux-musl-g++ &>/dev/null; then
    echo "Building for arm64 (musl static)..."
    aarch64-linux-musl-g++ -O2 -static -o "$OUTPUT_DIR/bb64.arm64" bb64.cpp
    echo "Built bb64.arm64 (arm64, static)"
fi 