name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  release:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          # aarch64-unknown-linux-gnu is intentionally omitted for v0.1.0:
          # ring (a rustls dep) requires extra sysroot setup to cross-compile.
          # Linux arm64 users can install via `cargo install elevenlabs` until
          # we wire up cross/dockcross in a follow-up.
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2

      - name: Install cross-compile deps (linux-arm)
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu
          mkdir -p .cargo
          printf '[target.aarch64-unknown-linux-gnu]\nlinker = "aarch64-linux-gnu-gcc"\n' > .cargo/config.toml

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

      - name: Package (unix)
        if: matrix.os != 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../elevenlabs-${{ matrix.target }}.tar.gz elevenlabs
          cd -

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

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            elevenlabs-*.tar.gz
            elevenlabs-*.zip
          draft: false
          generate_release_notes: true
