#!/bin/bash
set -e

VERSION="0.2.7"
LDFLAGS="-X main.version=${VERSION}"

echo "Building EduBox Agent v${VERSION}..."

export PATH=$PATH:/usr/local/go/bin

GOOS=windows GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o edubox-agent.exe .
echo "  edubox-agent.exe (Windows amd64)"
cp edubox-agent.exe "edubox-agent-v${VERSION}.exe"
echo "  edubox-agent-v${VERSION}.exe (Windows amd64)"

GOOS=linux GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o edubox-agent .
echo "  edubox-agent (Linux amd64)"
cp edubox-agent "edubox-agent-v${VERSION}"
echo "  edubox-agent-v${VERSION} (Linux amd64)"

GOOS=darwin GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o edubox-agent-mac .
echo "  edubox-agent-mac (macOS amd64)"
cp edubox-agent-mac "edubox-agent-v${VERSION}-mac"
echo "  edubox-agent-v${VERSION}-mac (macOS amd64)"

# Copy versioned binaries to server/public so the dashboard can serve them
SERVER_PUBLIC="../server/public"
if [ -d "${SERVER_PUBLIC}" ]; then
    cp "edubox-agent-v${VERSION}" "${SERVER_PUBLIC}/edubox-agent-v${VERSION}"
    cp "edubox-agent-v${VERSION}-mac" "${SERVER_PUBLIC}/edubox-agent-v${VERSION}-mac"
    cp "edubox-agent-v${VERSION}.exe" "${SERVER_PUBLIC}/edubox-agent-v${VERSION}.exe"
    echo "  Copied versioned binaries to ${SERVER_PUBLIC}"
fi

echo "Done."
