#!/bin/bash

# 项目名称
PROJECT="xk-websocket"

# 设置时间格式
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")

# 创建目标目录
DIST_DIR="dist"
mkdir -p $DIST_DIR

# 清理旧构建
echo "🧹 清理旧构建文件..."
find $DIST_DIR -name "${PROJECT}-*" -delete

echo "🚀 开始构建 ${PROJECT}..."
echo "⏰ 构建时间: $TIMESTAMP"

# 构建 Linux 版本
echo "🐧 构建 Linux (amd64) 版本..."
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o $DIST_DIR/${PROJECT}-linux-amd64-${TIMESTAMP} .

# 构建 Linux ARM 版本 (适合树莓派)
echo "🍓 构建 Linux (arm64) 版本..."
GOOS=linux GOARCH=arm64 go build -ldflags "-s -w" -o $DIST_DIR/${PROJECT}-linux-arm64-${TIMESTAMP} .

# 构建 Windows 版本
echo "🪟 构建 Windows (amd64) 版本..."
GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o $DIST_DIR/${PROJECT}-windows-amd64-${TIMESTAMP}.exe .

# 压缩文件
echo "📦 压缩构建文件..."
pushd $DIST_DIR >/dev/null

# Linux amd64
tar -czf ${PROJECT}-linux-amd64-${TIMESTAMP}.tar.gz ${PROJECT}-linux-amd64-${TIMESTAMP}
rm ${PROJECT}-linux-amd64-${TIMESTAMP}

# Linux arm64
tar -czf ${PROJECT}-linux-arm64-${TIMESTAMP}.tar.gz ${PROJECT}-linux-arm64-${TIMESTAMP}
rm ${PROJECT}-linux-arm64-${TIMESTAMP}

# Windows
zip -q ${PROJECT}-windows-amd64-${TIMESTAMP}.zip ${PROJECT}-windows-amd64-${TIMESTAMP}.exe
rm ${PROJECT}-windows-amd64-${TIMESTAMP}.exe

popd >/dev/null

echo "✅ 构建完成！"
echo "📁 输出目录: $DIST_DIR"
echo "📦 生成文件:"
ls -lh $DIST_DIR/*${TIMESTAMP}*