#!/usr/bin/env bash
# build-linux-binaries.sh — build stripped Linux server binaries locally using Docker.
#
# PRIMARY path: push to main (or trigger manually from the GitHub Actions tab) and
# download the artifacts from .github/workflows/build-linux.yml — no local disk
# pressure, both amd64 and arm64 handled in the cloud.
#
# LOCAL path (this script): uses Docker + buildx with BuildKit cache.  Requires
# ~10–15 GB of free disk for the vcpkg build cache.  Fine on a Linux dev machine;
# on Windows/macOS prefer the GitHub Actions path to avoid filling your Docker VM disk.
#
# Usage:
#   ./scripts/build-linux-binaries.sh            # build both arches
#   ./scripts/build-linux-binaries.sh amd64      # build one arch only
#   ./scripts/build-linux-binaries.sh arm64
#
# Output:
#   dist/linux-amd64/{voicecat-server,voicecat-admin}
#   dist/linux-arm64/{voicecat-server,voicecat-admin}

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
DIST_DIR="${REPO_ROOT}/dist"

# ── arg parsing ────────────────────────────────────────────────────────────────
case "${1:-both}" in
    amd64) ARCHS=("amd64") ;;
    arm64) ARCHS=("arm64") ;;
    both)  ARCHS=("amd64" "arm64") ;;
    *)     echo "Usage: $0 [amd64|arm64|both]" >&2; exit 1 ;;
esac

# ── pre-flight ─────────────────────────────────────────────────────────────────
if ! command -v docker &>/dev/null; then
    echo "Error: docker not found." >&2
    echo "Install Docker Desktop: https://docs.docker.com/get-docker/" >&2
    echo "Or use GitHub Actions (push to main and download artifacts)." >&2
    exit 1
fi

if ! docker buildx version &>/dev/null; then
    echo "Error: docker buildx not available." >&2
    echo "Docker Desktop ships with buildx.  On Linux: docker buildx install" >&2
    exit 1
fi

echo "Note: first run compiles vcpkg packages from source (~10-15 GB build cache)."
echo "On Windows/macOS, consider using GitHub Actions instead (Actions → Build Linux Binaries → Run workflow)."
echo ""

# ── ensure a builder with multi-platform support ───────────────────────────────
BUILDER="voicecat-builder"
if ! docker buildx inspect "${BUILDER}" &>/dev/null; then
    echo "→ Creating buildx builder '${BUILDER}' (docker-container driver)..."
    docker buildx create --name "${BUILDER}" --driver docker-container --bootstrap
fi
docker buildx use "${BUILDER}"

# ── build each arch ────────────────────────────────────────────────────────────
for ARCH in "${ARCHS[@]}"; do
    PLATFORM="linux/${ARCH}"
    OUT_DIR="${DIST_DIR}/linux-${ARCH}"
    mkdir -p "${OUT_DIR}"

    echo "══ Building ${PLATFORM} ══════════════════════════════════════════════════"
    if [[ "${ARCH}" == "arm64" ]] && [[ "$(uname -m)" != "aarch64" ]]; then
        echo "   (Running under QEMU on a non-arm64 host — will be slow)"
    fi

    docker buildx build \
        --platform "${PLATFORM}" \
        --target export \
        --output "type=local,dest=${OUT_DIR}" \
        --progress plain \
        "${REPO_ROOT}"

    echo "→ ${PLATFORM} binaries:"
    ls -lh "${OUT_DIR}/"
    echo ""
done

echo "════════════════════════════════════════════════════════════════════════════"
echo "Done. Binaries in dist/:"
for ARCH in "${ARCHS[@]}"; do
    ls -lh "${DIST_DIR}/linux-${ARCH}/"
done
