name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"

jobs:
  build:
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-24.04-arm
            archive: tar.gz
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: zip

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

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

      - name: Install system dependencies (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libssl-dev pkg-config

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

      - name: Package (unix)
        if: matrix.archive == 'tar.gz'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../km-${{ matrix.target }}.tar.gz km
          cd ../../..

      - name: Package (windows)
        if: matrix.archive == 'zip'
        shell: pwsh
        run: |
          Compress-Archive -Path target/${{ matrix.target }}/release/km.exe -DestinationPath km-${{ matrix.target }}.zip

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: km-${{ matrix.target }}
          path: km-${{ matrix.target }}.${{ matrix.archive }}

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Generate checksums
        run: sha256sum km-* > checksums-sha256.txt

      - name: Upload to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            km-*
            checksums-sha256.txt

  update-homebrew:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Extract version from tag
        id: version
        run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"

      - name: Download source tarball and compute sha256
        id: sha
        run: |
          curl -sL "https://github.com/${{ github.repository }}/archive/refs/tags/${GITHUB_REF_NAME}.tar.gz" -o source.tar.gz
          sha=$(sha256sum source.tar.gz | cut -d' ' -f1)
          echo "sha256=${sha}" >> "$GITHUB_OUTPUT"

      - name: Update Homebrew formula
        env:
          TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
        run: |
          git clone "https://x-access-token:${TAP_TOKEN}@github.com/lnds/homebrew-kimun.git" tap
          cd tap

          VERSION="${{ steps.version.outputs.version }}"
          SHA256="${{ steps.sha.outputs.sha256 }}"

          sed -i "s|url \".*\"|url \"https://github.com/${{ github.repository }}/archive/refs/tags/${GITHUB_REF_NAME}.tar.gz\"|" Formula/kimun.rb
          sed -i "s|sha256 \".*\"|sha256 \"${SHA256}\"|" Formula/kimun.rb

          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/kimun.rb
          git commit -m "bump kimun to ${VERSION}"
          git push
