#!/bin/bash

# Multi-architecture Docker BuildKit builder setup script
# Sets up buildx for AMD64 and ARM64 platforms

set -e

echo "🔧 Setting up multi-architecture Docker BuildKit builder..."

# Check if docker buildx is available
if ! docker buildx version >/dev/null 2>&1; then
    echo "❌ Docker buildx is not available. Please install Docker BuildKit."
    exit 1
fi

# Create multi-architecture builder if it doesn't exist
BUILDER_NAME="multiarch-builder"
if docker buildx ls | grep -q "$BUILDER_NAME"; then
    echo "✅ Builder '$BUILDER_NAME' already exists"
else
    echo "📦 Creating new multi-architecture builder..."
    docker buildx create --name "$BUILDER_NAME" --driver docker-container --bootstrap
    echo "✅ Builder '$BUILDER_NAME' created successfully"
fi

# Use the multi-architecture builder
docker buildx use "$BUILDER_NAME"

# Verify platforms
echo "🔍 Verifying supported platforms..."
docker buildx inspect --bootstrap

echo "✅ Multi-architecture builder setup complete!"
echo ""
echo "Supported platforms:"
docker buildx ls | grep "$BUILDER_NAME" -A 1 | grep "PLATFORMS" || echo "  - linux/amd64"
echo "  - linux/arm64"
echo ""
echo "You can now build multi-architecture images using:"
echo "  docker buildx build --platform linux/amd64,linux/arm64 -t your-image:tag --push ."