name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross (linux only)
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build
        run: |
          if [[ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]]; then
            cross build --release --target ${{ matrix.target }}
          else
            cargo build --release --target ${{ matrix.target }}
          fi

      - name: Package
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../ritalin-${{ matrix.target }}.tar.gz ritalin
          cd -
          shasum -a 256 ritalin-${{ matrix.target }}.tar.gz > ritalin-${{ matrix.target }}.tar.gz.sha256

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ritalin-${{ matrix.target }}.tar.gz
            ritalin-${{ matrix.target }}.tar.gz.sha256

  publish-crate:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish

  # Homebrew is bumped separately by the self-polling workflow
  # (.github/workflows/bump-ritalin.yml) in paperfoot/homebrew-tap — releases
  # here can't push there cross-repo, so the tap polls this repo's latest
  # release every 6h. For an instant bump after a release:
  #   gh workflow run bump-ritalin.yml -R paperfoot/homebrew-tap
