name: Release

on:
  push:
    tags: [ "v*" ]

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v5

    - uses: dtolnay/rust-toolchain@stable

    - name: Publish to crates.io
      run: cargo publish --locked
      env:
        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    steps:
      - uses: actions/checkout@v5
        with:
          submodules: recursive

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Set up cross toolchain
        if: runner.os == 'Linux'
        uses: taiki-e/setup-cross-toolchain-action@v1
        with:
          target: ${{ matrix.target }}

      - name: Build
        run: cargo build --release --locked --target ${{ matrix.target }}

      - name: Sign binary
        if: runner.os == 'macOS'
        run: codesign --sign - target/${{ matrix.target }}/release/pxh

      - name: Package
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../pxh-${{ matrix.target }}.tar.gz pxh

      - uses: actions/upload-artifact@v4
        with:
          name: pxh-${{ matrix.target }}
          path: pxh-${{ matrix.target }}.tar.gz

  release:
    name: GitHub Release
    needs: [publish, build]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Generate checksums
        run: sha256sum pxh-*.tar.gz > SHA256SUMS

      - uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: |
            pxh-*.tar.gz
            SHA256SUMS

  update-homebrew:
    name: Update Homebrew formula
    needs: [release]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

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

      - name: Download SHA256SUMS from release
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh release download "${GITHUB_REF_NAME}" --pattern SHA256SUMS

      - name: Parse checksums
        id: sums
        run: |
          extract() { grep "$1" SHA256SUMS | cut -d' ' -f1; }
          echo "aarch64_apple=$(extract aarch64-apple-darwin)" >> "$GITHUB_OUTPUT"
          echo "x86_64_apple=$(extract x86_64-apple-darwin)" >> "$GITHUB_OUTPUT"
          echo "aarch64_linux=$(extract aarch64-unknown-linux-musl)" >> "$GITHUB_OUTPUT"
          echo "x86_64_linux=$(extract x86_64-unknown-linux-musl)" >> "$GITHUB_OUTPUT"

      - name: Update formula and push to tap
        env:
          VERSION: ${{ steps.version.outputs.version }}
          SHA_AARCH64_APPLE: ${{ steps.sums.outputs.aarch64_apple }}
          SHA_X86_64_APPLE: ${{ steps.sums.outputs.x86_64_apple }}
          SHA_AARCH64_LINUX: ${{ steps.sums.outputs.aarch64_linux }}
          SHA_X86_64_LINUX: ${{ steps.sums.outputs.x86_64_linux }}
          TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
        run: |
          git clone "https://x-access-token:${TAP_TOKEN}@github.com/chipturner/homebrew-tap.git" homebrew-tap
          cd homebrew-tap
          mkdir -p Formula
          cat > Formula/pxh.rb << FORMULA
          class Pxh < Formula
            desc "Fast, cross-shell history mining tool with interactive fuzzy search and sync"
            homepage "https://github.com/chipturner/pxhist"
            version "${VERSION}"
            license "MIT"

            on_macos do
              on_arm do
                url "https://github.com/chipturner/pxhist/releases/download/v#{version}/pxh-aarch64-apple-darwin.tar.gz"
                sha256 "${SHA_AARCH64_APPLE}"
              end
              on_intel do
                url "https://github.com/chipturner/pxhist/releases/download/v#{version}/pxh-x86_64-apple-darwin.tar.gz"
                sha256 "${SHA_X86_64_APPLE}"
              end
            end

            on_linux do
              on_arm do
                url "https://github.com/chipturner/pxhist/releases/download/v#{version}/pxh-aarch64-unknown-linux-musl.tar.gz"
                sha256 "${SHA_AARCH64_LINUX}"
              end
              on_intel do
                url "https://github.com/chipturner/pxhist/releases/download/v#{version}/pxh-x86_64-unknown-linux-musl.tar.gz"
                sha256 "${SHA_X86_64_LINUX}"
              end
            end

            def install
              bin.install "pxh"
            end

            test do
              system bin/"pxh", "--version"
            end
          end
          FORMULA
          sed -i 's/^          //' Formula/pxh.rb
          git add Formula/pxh.rb
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git diff --cached --quiet || git commit -m "Update pxh to ${VERSION}" && git push
