name: Release

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+*'

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build-release:
    name: Build - ${{ matrix.name }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: sip-client
            asset: sip-client-linux-x86_64
            name: linux-x86_64
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            artifact: sip-client
            asset: sip-client-linux-aarch64
            name: linux-aarch64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: sip-client.exe
            asset: sip-client-windows-x86_64.exe
            name: windows-x86_64
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            artifact: sip-client.exe
            asset: sip-client-windows-aarch64.exe
            name: windows-aarch64
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact: sip-client
            asset: sip-client-macos-x86_64
            name: macos-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: sip-client
            asset: sip-client-macos-aarch64
            name: macos-aarch64

    steps:
      - uses: actions/checkout@v4

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

      - uses: Swatinem/rust-cache@v2
        continue-on-error: true

      - name: Build release (no opus)
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build release (with opus)
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          sudo apt-get update && sudo apt-get install -y libopus-dev
          cargo build --release --features opus --target ${{ matrix.target }}

      - name: Rename artifact
        shell: bash
        run: |
          cp target/${{ matrix.target }}/release/${{ matrix.artifact }} ${{ matrix.asset }}

      - name: Upload artifact to actions
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset }}
          path: ${{ matrix.asset }}

  publish-release:
    name: Publish GitHub Release
    runs-on: ubuntu-latest
    needs: [build-release]
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/*
          generate_release_notes: true

  crate-publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [publish-release]
    if: github.ref_type == 'tag' && (!contains(github.ref_name, '-'))
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - name: Publish
        run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}
