#!/bin/bash
set -e

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
OUTPUT_DIR="$SCRIPT_DIR/output"
PROJECT_NAME="dehydrate"



BUILD_DIR="$SCRIPT_DIR/build"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR" "$OUTPUT_DIR"
cd "$BUILD_DIR"
cmake ..
make -j$(nproc)
cp "$PROJECT_NAME" "$OUTPUT_DIR/$PROJECT_NAME.amd64"
echo "Built $OUTPUT_DIR/$PROJECT_NAME.amd64 (x86_64, static if CMake is configured for musl/static)"
cd "$SCRIPT_DIR"

# 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) with CMake..."
    BUILD_DIR_ARM64="$SCRIPT_DIR/build_arm64"
    rm -rf "$BUILD_DIR_ARM64"
    mkdir -p "$BUILD_DIR_ARM64"
    cd "$BUILD_DIR_ARM64"
    cmake -DCMAKE_CXX_COMPILER=aarch64-linux-musl-g++ ..
    make -j$(nproc)
    cp "$PROJECT_NAME" "$OUTPUT_DIR/$PROJECT_NAME.arm64"
    echo "Built $OUTPUT_DIR/$PROJECT_NAME.arm64 (arm64, static)"
    cd "$SCRIPT_DIR"
fi 
