name: release

on:
  workflow_dispatch:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            artifact: rapidgzip-rs-cli-linux-x86_64
            archive: rapidgzip-rs-cli-linux-x86_64.tar.gz
            binary: rapidgzip-rs-cli
            raw_asset: rapidgzip-rs-cli-linux-x86_64
          - os: ubuntu-24.04-arm
            artifact: rapidgzip-rs-cli-linux-arm64
            archive: rapidgzip-rs-cli-linux-arm64.tar.gz
            binary: rapidgzip-rs-cli
            raw_asset: rapidgzip-rs-cli-linux-arm64
          - os: macos-15-intel
            artifact: rapidgzip-rs-cli-macos-x86_64
            archive: rapidgzip-rs-cli-macos-x86_64.tar.gz
            binary: rapidgzip-rs-cli
            raw_asset: rapidgzip-rs-cli-macos-x86_64
          - os: macos-15
            artifact: rapidgzip-rs-cli-macos-arm64
            archive: rapidgzip-rs-cli-macos-arm64.tar.gz
            binary: rapidgzip-rs-cli
            raw_asset: rapidgzip-rs-cli-macos-arm64
          - os: windows-latest
            artifact: rapidgzip-rs-cli-windows-x86_64
            archive: rapidgzip-rs-cli-windows-x86_64.zip
            binary: rapidgzip-rs-cli.exe
            raw_asset: rapidgzip-rs-cli-windows-x86_64.exe
          - os: windows-11-arm
            artifact: rapidgzip-rs-cli-windows-arm64
            archive: rapidgzip-rs-cli-windows-arm64.zip
            binary: rapidgzip-rs-cli.exe
            raw_asset: rapidgzip-rs-cli-windows-arm64.exe

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install native build dependencies
        shell: bash
        run: |
          set -euxo pipefail
          case "$RUNNER_OS" in
            Linux)
              sudo apt-get update
              sudo apt-get install -y cmake build-essential zlib1g-dev nasm
              ;;
            macOS)
              brew install -q nasm
              ;;
            Windows)
              choco install -y nasm
              echo "C:\Program Files\NASM" >> "$GITHUB_PATH"
              ;;
          esac

      - name: Build release binary
        run: cargo build --release

      - name: Stage package contents
        shell: bash
        run: |
          set -euxo pipefail
          rm -rf package
          mkdir -p package
          cp "target/release/${{ matrix.binary }}" "package/${{ matrix.binary }}"
          cp README.md package/README.md
          cp SECURITY.md package/SECURITY.md
          cp LICENSE-MIT package/LICENSE-MIT
          cp LICENSE-APACHE package/LICENSE-APACHE

      - name: Create tar.gz archive
        if: runner.os != 'Windows'
        shell: bash
        run: |
          set -euxo pipefail
          tar -C package -czf "${{ matrix.archive }}" .

      - name: Create zip archive
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Compress-Archive -Path package/* -DestinationPath "${{ matrix.archive }}"

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

      - name: Upload raw binary workflow artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}-raw
          path: package/${{ matrix.binary }}

      - name: Ensure GitHub release exists
        if: startsWith(github.ref, 'refs/tags/v')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
          set -euxo pipefail
          if ! gh release view "${GITHUB_REF_NAME}" >/dev/null 2>&1; then
            gh release create "${GITHUB_REF_NAME}" \
              --verify-tag \
              --title "${GITHUB_REF_NAME}" \
              --generate-notes
          fi

      - name: Upload release assets
        if: startsWith(github.ref, 'refs/tags/v')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
          set -euxo pipefail
          cp "package/${{ matrix.binary }}" "${{ matrix.raw_asset }}"
          gh release upload "${GITHUB_REF_NAME}" "${{ matrix.archive }}" "${{ matrix.raw_asset }}" --clobber
