#!/bin/bash

# Build script for TACO format with C/C++/Fortran interfaces

set -e

echo "Building TACO format library with C/C++/Fortran interfaces..."

# Build the Rust library with C API
echo "Building Rust library..."
cargo build --release --features c_api

# Check if the library was built successfully
LIB_PATH="target/release/libtaco_format.a"
if [ ! -f "$LIB_PATH" ]; then
    echo "Error: Library not built at $LIB_PATH"
    exit 1
fi

echo "Library built successfully at $LIB_PATH"

# Build examples if requested
if [ "$1" == "examples" ] || [ "$1" == "all" ]; then
    echo "Building examples..."
    cd examples
    make all
    cd ..
    echo "Examples built successfully"
fi

# Build and run tests if requested
if [ "$1" == "tests" ] || [ "$1" == "all" ]; then
    echo "Building and running tests..."
    cd tests/c_api
    make all
    make test
    cd ../..
    echo "Tests completed successfully"
fi

echo "Build completed successfully!"
echo ""
echo "Next steps:"
echo "1. To build examples: ./build.sh examples"
echo "2. To run tests: ./build.sh tests"
echo "3. To build everything: ./build.sh all"
echo "4. See docs/c_cpp_fortran_api.md for usage instructions"
