name: Update Homebrew Tap

on:
  release:
    types: [published]

jobs:
  update-tap:
    name: Update Homebrew formula
    runs-on: ubuntu-latest

    steps:
      - name: Checkout tap repo
        uses: actions/checkout@v4
        with:
          repository: skyline69/homebrew-agcp
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          path: homebrew-agcp

      - name: Get release version
        id: version
        run: |
          version="${GITHUB_REF_NAME#v}"
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Download release assets
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          mkdir -p /tmp/assets
          for target in aarch64-apple-darwin x86_64-apple-darwin aarch64-unknown-linux-gnu x86_64-unknown-linux-gnu; do
            gh release download "$GITHUB_REF_NAME" \
              --repo "$GITHUB_REPOSITORY" \
              --pattern "agcp-v${{ steps.version.outputs.version }}-${target}.tar.gz" \
              --dir /tmp/assets
          done

      - name: Compute SHA256 hashes
        id: hashes
        run: |
          v="${{ steps.version.outputs.version }}"
          echo "aarch64_macos=$(sha256sum /tmp/assets/agcp-v${v}-aarch64-apple-darwin.tar.gz | awk '{print $1}')" >> "$GITHUB_OUTPUT"
          echo "x86_64_macos=$(sha256sum /tmp/assets/agcp-v${v}-x86_64-apple-darwin.tar.gz | awk '{print $1}')" >> "$GITHUB_OUTPUT"
          echo "aarch64_linux=$(sha256sum /tmp/assets/agcp-v${v}-aarch64-unknown-linux-gnu.tar.gz | awk '{print $1}')" >> "$GITHUB_OUTPUT"
          echo "x86_64_linux=$(sha256sum /tmp/assets/agcp-v${v}-x86_64-unknown-linux-gnu.tar.gz | awk '{print $1}')" >> "$GITHUB_OUTPUT"

      - name: Update formula
        run: |
          cat > homebrew-agcp/Formula/agcp.rb << 'FORMULA'
          class Agcp < Formula
            desc "Lightweight Rust proxy translating Anthropic Claude API to Google Cloud Code API"
            homepage "https://github.com/skyline69/agcp"
            version "${{ steps.version.outputs.version }}"
            license "MIT"

            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/skyline69/agcp/releases/download/v#{version}/agcp-v#{version}-aarch64-apple-darwin.tar.gz"
                sha256 "${{ steps.hashes.outputs.aarch64_macos }}"
              else
                url "https://github.com/skyline69/agcp/releases/download/v#{version}/agcp-v#{version}-x86_64-apple-darwin.tar.gz"
                sha256 "${{ steps.hashes.outputs.x86_64_macos }}"
              end
            end

            on_linux do
              if Hardware::CPU.arm?
                url "https://github.com/skyline69/agcp/releases/download/v#{version}/agcp-v#{version}-aarch64-unknown-linux-gnu.tar.gz"
                sha256 "${{ steps.hashes.outputs.aarch64_linux }}"
              else
                url "https://github.com/skyline69/agcp/releases/download/v#{version}/agcp-v#{version}-x86_64-unknown-linux-gnu.tar.gz"
                sha256 "${{ steps.hashes.outputs.x86_64_linux }}"
              end
            end

            def install
              bin.install "agcp"
            end

            test do
              assert_match version.to_s, shell_output("#{bin}/agcp --version")
            end
          end
          FORMULA
          # Remove leading whitespace from heredoc
          sed -i 's/^          //' homebrew-agcp/Formula/agcp.rb

      - name: Commit and push
        working-directory: homebrew-agcp
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/agcp.rb
          git commit -m "agcp ${{ steps.version.outputs.version }}"
          git push
