name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-24.04-arm

    steps:
      - uses: actions/checkout@v6

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

      - uses: Swatinem/rust-cache@v2

      - name: Install musl tools
        if: contains(matrix.target, 'linux-musl')
        run: sudo apt-get install -y musl-tools

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

      - name: Rename binary
        shell: bash
        run: cp "target/${{ matrix.target }}/release/agv" "agv-${{ matrix.target }}"

      - uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.target }}
          path: agv-${{ matrix.target }}

  release:
    name: Create release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/download-artifact@v7
        with:
          path: artifacts
          merge-multiple: true

      - name: Checksums
        run: |
          cd artifacts
          sha256sum agv-* > checksums.txt

      - name: Create GitHub release
        run: |
          gh release create "${{ github.ref_name }}" \
            --repo "${{ github.repository }}" \
            --title "${{ github.ref_name }}" \
            --generate-notes \
            artifacts/agv-* \
            artifacts/checksums.txt
        env:
          GH_TOKEN: ${{ github.token }}
