name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    strategy:
      matrix:
        include:
          - target: aarch64-apple-darwin
            name: arm64-darwin
          - target: x86_64-apple-darwin
            name: x86_64-darwin

    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v4

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

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

      - name: Package
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../email-cli-${{ github.ref_name }}-${{ matrix.name }}.tar.gz email-cli

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: email-cli-${{ matrix.name }}
          path: email-cli-${{ github.ref_name }}-${{ matrix.name }}.tar.gz

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

      - name: Create checksums
        run: sha256sum email-cli-*.tar.gz > checksums.txt

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            email-cli-*.tar.gz
            checksums.txt
