name: Update Homebrew Tap

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to update (e.g. 0.3.1)'
        required: true

jobs:
  update-tap:
    name: Update Homebrew Formula
    runs-on: ubuntu-latest
    steps:
      - name: Get release info
        id: release
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            VERSION="${{ github.event.inputs.version }}"
          else
            VERSION="${GITHUB_REF_NAME#v}"
          fi
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"

      - name: Download release assets and compute SHA256
        id: sha
        run: |
          BASE_URL="https://github.com/${{ github.repository }}/releases/download/${{ steps.release.outputs.tag }}"

          curl -sL "${BASE_URL}/aidaemon-macos-aarch64.tar.gz" -o macos-aarch64.tar.gz
          curl -sL "${BASE_URL}/aidaemon-macos-x86_64.tar.gz" -o macos-x86_64.tar.gz
          curl -sL "${BASE_URL}/aidaemon-linux-x86_64.tar.gz" -o linux-x86_64.tar.gz

          echo "macos_arm64=$(sha256sum macos-aarch64.tar.gz | cut -d' ' -f1)" >> "$GITHUB_OUTPUT"
          echo "macos_x86_64=$(sha256sum macos-x86_64.tar.gz | cut -d' ' -f1)" >> "$GITHUB_OUTPUT"
          echo "linux_x86_64=$(sha256sum linux-x86_64.tar.gz | cut -d' ' -f1)" >> "$GITHUB_OUTPUT"

      - name: Checkout tap
        uses: actions/checkout@v4
        with:
          repository: davo20019/homebrew-tap
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}

      - name: Update formula
        run: |
          mkdir -p Formula
          cat > Formula/aidaemon.rb << 'RUBY'
          class Aidaemon < Formula
            desc "Personal AI agent daemon with tool use, MCP integration, and persistent memory"
            homepage "https://aidaemon.ai/"
            license "MIT"
            version "VERSION_PLACEHOLDER"

            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/davo20019/aidaemon/releases/download/vVERSION_PLACEHOLDER/aidaemon-macos-aarch64.tar.gz"
                sha256 "SHA_MACOS_ARM64_PLACEHOLDER"
              else
                url "https://github.com/davo20019/aidaemon/releases/download/vVERSION_PLACEHOLDER/aidaemon-macos-x86_64.tar.gz"
                sha256 "SHA_MACOS_X86_64_PLACEHOLDER"
              end
            end

            on_linux do
              url "https://github.com/davo20019/aidaemon/releases/download/vVERSION_PLACEHOLDER/aidaemon-linux-x86_64.tar.gz"
              sha256 "SHA_LINUX_X86_64_PLACEHOLDER"
            end

            def install
              bin.install "aidaemon"
            end

            test do
              assert_match "aidaemon", shell_output("#{bin}/aidaemon --version", 0)
            end
          end
          RUBY

          sed -i "s/VERSION_PLACEHOLDER/${{ steps.release.outputs.version }}/g" Formula/aidaemon.rb
          sed -i "s/SHA_MACOS_ARM64_PLACEHOLDER/${{ steps.sha.outputs.macos_arm64 }}/g" Formula/aidaemon.rb
          sed -i "s/SHA_MACOS_X86_64_PLACEHOLDER/${{ steps.sha.outputs.macos_x86_64 }}/g" Formula/aidaemon.rb
          sed -i "s/SHA_LINUX_X86_64_PLACEHOLDER/${{ steps.sha.outputs.linux_x86_64 }}/g" Formula/aidaemon.rb

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