#!/bin/bash
set -e

VERSION=$1
if [ -z "$VERSION" ]; then
  echo "Usage: ./scripts/release.sh v0.2.0"
  exit 1
fi

# 移除 v 前缀用于 Cargo.toml
VERSION_NUMBER="${VERSION#v}"

echo "🚀 Releasing $VERSION"

# 1. 更新版本号
echo "📝 Updating version numbers..."
sed -i '' "s/^version = \".*\"/version = \"$VERSION_NUMBER\"/" Cargo.toml
sed -i '' "s/^  version \".*\"/  version \"$VERSION_NUMBER\"/" homebrew/spool.rb

# 2. 提交版本更新
echo "💾 Committing version bump..."
git add Cargo.toml homebrew/spool.rb
git commit -m "chore: bump version to $VERSION"

# 3. 打 tag 并推送
echo "🏷️  Creating and pushing tag..."
git tag "$VERSION"
git push origin main
git push origin "$VERSION"

echo ""
echo "✅ Release $VERSION triggered successfully!"
echo ""
echo "📊 Check build progress:"
echo "   https://github.com/lukylong/Spool/actions"
echo ""
echo "📦 After build completes, update Homebrew formula SHA256:"
echo "   1. Download release assets: gh release download $VERSION"
echo "   2. Calculate SHA256: shasum -a 256 *.tar.gz"
echo "   3. Update homebrew/spool.rb with actual SHA256 values"
echo "   4. Commit and push: git add homebrew/spool.rb && git commit -m 'chore: update homebrew SHA256 for $VERSION' && git push"
