#!/usr/bin/env bash
# 一键交叉编译 Linux 采集端（amd64 + arm64），输出与 dist/install_mengyamonitor.sh 发布路径一致。
#
# 用法（在 mengyamonitor-backend-client 目录下）:
#   chmod +x build_linux.sh && ./build_linux.sh
#
# Windows: Git Bash 同上；或在项目目录执行:  powershell -ExecutionPolicy Bypass -File .\build_linux.ps1
#
# 可选环境变量:
#   BINARY_NAME   默认 mengyamonitor-agent
#   OUT_DIR       默认 dist（相对本脚本所在目录）

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT"

NAME="${BINARY_NAME:-mengyamonitor-agent}"
OUT="${OUT_DIR:-dist}"

mkdir -p "$OUT/linux_amd64" "$OUT/linux_arm64"

export CGO_ENABLED=0

echo "==> linux/amd64 -> $OUT/linux_amd64/$NAME"
GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o "$OUT/linux_amd64/$NAME" .

echo "==> linux/arm64 -> $OUT/linux_arm64/$NAME"
GOOS=linux GOARCH=arm64 go build -trimpath -ldflags="-s -w" -o "$OUT/linux_arm64/$NAME" .

echo "完成。产物:"
for arch in amd64 arm64; do
	f="$OUT/linux_${arch}/$NAME"
	if command -v ls >/dev/null 2>&1; then
		ls -la "$f"
	else
		echo "  $f"
	fi
done
