#!/bin/bash
set -e

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

echo "📦 Downloading release assets for $VERSION..."
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"

gh release download "$VERSION" --repo lukylong/Spool

echo ""
echo "🔐 Calculating SHA256 checksums..."
ARM64_SHA=$(shasum -a 256 spool-macos-arm64.tar.gz | awk '{print $1}')
INTEL_SHA=$(shasum -a 256 spool-macos-intel.tar.gz | awk '{print $1}')
LINUX_X64_SHA=$(shasum -a 256 spool-linux-x86_64.tar.gz | awk '{print $1}')
LINUX_ARM64_SHA=$(shasum -a 256 spool-linux-arm64.tar.gz | awk '{print $1}')

echo ""
echo "📝 Updating homebrew/spool.rb..."
cd "$OLDPWD"

sed -i '' "s/PLACEHOLDER_ARM64_SHA256/$ARM64_SHA/" homebrew/spool.rb
sed -i '' "s/PLACEHOLDER_INTEL_SHA256/$INTEL_SHA/" homebrew/spool.rb
sed -i '' "s/PLACEHOLDER_LINUX_X86_64_SHA256/$LINUX_X64_SHA/" homebrew/spool.rb
sed -i '' "s/PLACEHOLDER_LINUX_ARM64_SHA256/$LINUX_ARM64_SHA/" homebrew/spool.rb

echo ""
echo "✅ SHA256 values updated in homebrew/spool.rb"
echo ""
echo "📋 Summary:"
echo "  macOS ARM64:    $ARM64_SHA"
echo "  macOS Intel:    $INTEL_SHA"
echo "  Linux x86_64:   $LINUX_X64_SHA"
echo "  Linux ARM64:    $LINUX_ARM64_SHA"
echo ""
echo "💾 Commit and push:"
echo "   git add homebrew/spool.rb"
echo "   git commit -m 'chore: update homebrew SHA256 for $VERSION'"
echo "   git push origin main"

# 清理临时目录
rm -rf "$TEMP_DIR"
