name: Update Homebrew Tap

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      tag:
        description: 'Release tag (e.g. v2.0.0)'
        required: true

jobs:
  update-tap:
    runs-on: ubuntu-latest
    env:
      TAG: ${{ github.event.release.tag_name || inputs.tag }}

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

    - name: Download release binaries and compute checksums
      id: shas
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        for target in x86_64-apple-darwin aarch64-apple-darwin x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu; do
          gh release download "$TAG" \
            --repo bearded-giant/mdlive \
            --pattern "mdlive-$target" \
            --dir .
          sha=$(sha256sum "mdlive-$target" | cut -d' ' -f1)
          key=$(echo "$target" | tr '-' '_')
          echo "${key}=${sha}" >> "$GITHUB_OUTPUT"
        done

    - name: Download DMG and compute checksum
      id: dmg
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        VERSION: ${{ steps.version.outputs.version }}
      run: |
        DMG_NAME="mdlive_${VERSION}_aarch64.dmg"
        gh release download "$TAG" \
          --repo bearded-giant/mdlive \
          --pattern "$DMG_NAME" \
          --dir .
        sha=$(sha256sum "$DMG_NAME" | cut -d' ' -f1)
        echo "sha_dmg=${sha}" >> "$GITHUB_OUTPUT"

    - name: Checkout tap repo
      uses: actions/checkout@v5
      with:
        repository: bearded-giant/homebrew-tap
        token: ${{ secrets.HOMEBREW_TAP_TOKEN }}

    - name: Update formula
      env:
        VERSION: ${{ steps.version.outputs.version }}
        SHA_X86_MACOS: ${{ steps.shas.outputs.x86_64_apple_darwin }}
        SHA_ARM_MACOS: ${{ steps.shas.outputs.aarch64_apple_darwin }}
        SHA_X86_LINUX: ${{ steps.shas.outputs.x86_64_unknown_linux_gnu }}
        SHA_ARM_LINUX: ${{ steps.shas.outputs.aarch64_unknown_linux_gnu }}
      run: |
        mkdir -p Formula
        cat > Formula/mdlive.rb << EOF
        class Mdlive < Formula
          desc "Markdown workspace server for AI coding agents"
          homepage "https://github.com/bearded-giant/mdlive"
          version "$VERSION"
          license "MIT"

          on_macos do
            if Hardware::CPU.arm?
              url "https://github.com/bearded-giant/mdlive/releases/download/v#{version}/mdlive-aarch64-apple-darwin"
              sha256 "$SHA_ARM_MACOS"
            else
              url "https://github.com/bearded-giant/mdlive/releases/download/v#{version}/mdlive-x86_64-apple-darwin"
              sha256 "$SHA_X86_MACOS"
            end
          end

          on_linux do
            if Hardware::CPU.arm?
              url "https://github.com/bearded-giant/mdlive/releases/download/v#{version}/mdlive-aarch64-unknown-linux-gnu"
              sha256 "$SHA_ARM_LINUX"
            else
              url "https://github.com/bearded-giant/mdlive/releases/download/v#{version}/mdlive-x86_64-unknown-linux-gnu"
              sha256 "$SHA_X86_LINUX"
            end
          end

          def install
            bin.install Dir["mdlive-*"].first => "mdlive"
          end

          test do
            assert_match version.to_s, shell_output("#{bin}/mdlive --version")
          end
        end
        EOF

    - name: Update cask
      env:
        VERSION: ${{ steps.version.outputs.version }}
        SHA_DMG: ${{ steps.dmg.outputs.sha_dmg }}
      run: |
        mkdir -p Casks
        cat > Casks/mdlive-app.rb << 'CASK'
        cask "mdlive-app" do
          version "VPLACEHOLDER"
          sha256 "SHAPLACEHOLDER"

          url "https://github.com/bearded-giant/mdlive/releases/download/v#{version}/mdlive_#{version}_aarch64.dmg"
          name "mdlive"
          desc "Markdown workspace server for AI coding agents"
          homepage "https://github.com/bearded-giant/mdlive"

          app "mdlive.app"

          zap trash: [
            "~/Library/Application Support/com.beardedgiant.mdlive",
            "~/.config/mdlive",
          ]
        end
        CASK
        sed -i "s/VPLACEHOLDER/$VERSION/" Casks/mdlive-app.rb
        sed -i "s/SHAPLACEHOLDER/$SHA_DMG/" Casks/mdlive-app.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/mdlive.rb Casks/mdlive-app.rb
        git diff --staged --quiet && exit 0
        git commit -m "mdlive ${{ steps.version.outputs.version }}"
        git pull --rebase
        git push
