#!/bin/bash
set -e

# This script builds standalone binaries for the Cortex Agent Node
# using Docker Multi-Arch BuildX and PyInstaller.

echo "🏗️ Setting up builder..."
cd "$(dirname "$0")/../.."

# Ensure buildx is available
docker buildx create --use --name cortex-builder || true

echo "🔨 Building Linux AMD64 Binary..."
docker buildx build \
  --platform linux/amd64 \
  --build-arg ARCH=amd64 \
  -f agent-node/Dockerfile.binary \
  --output type=local,dest=agent-node/dist/linux_amd64 \
  .

echo "🔨 Building Linux ARM64 Binary..."
docker buildx build \
  --platform linux/arm64 \
  --build-arg ARCH=arm64 \
  -f agent-node/Dockerfile.binary \
  --output type=local,dest=agent-node/dist/linux_arm64 \
  .

echo "✅ Build complete! Binaries are in agent-node/dist/"
chmod +x agent-node/dist/*/cortex-agent || true
