#!/bin/bash

# Build script for WebAssembly project

echo "Building WebAssembly module..."
echo "Current directory: $(pwd)"

# Check if wasm-pack is installed
if ! command -v wasm-pack &> /dev/null; then
    echo "wasm-pack not found. Installing..."
    curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
fi

# Create www directory if it doesn't exist
mkdir -p www

# Build the WASM package
echo "Running wasm-pack build..."
wasm-pack build --target web --out-dir www/pkg

# Check if build was successful
if [ $? -eq 0 ]; then
    echo "✅ WASM build completed successfully!"
    echo "📁 Output files created in www/pkg/"
    echo "🌐 You can now open www/index.html in a web browser"

    # List the generated files
    echo ""
    echo "Generated files:"
    ls -la www/pkg/
else
    echo "❌ WASM build failed. Please check the error messages above."
    exit 1
fi