name: Unix Release

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

permissions:
  contents: write

jobs:
  build-unix-release:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]

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

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

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

      - name: Cache Rust build outputs
        uses: Swatinem/rust-cache@v2

      - name: Install Linux build dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libasound2-dev libpcaudio-dev libsonic-dev

      - name: Cache voice model assets
        uses: actions/cache@v4
        with:
          path: .hematite/assets/voice
          key: kokoro-voice-assets-v1.0

      - name: Download voice model assets
        run: |
          mkdir -p .hematite/assets/voice
          base="https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0"
          [[ -f .hematite/assets/voice/kokoro-v1.0.onnx ]] || curl -L --retry 3 --max-time 300 -o .hematite/assets/voice/kokoro-v1.0.onnx "$base/kokoro-v1.0.onnx"
          [[ -f .hematite/assets/voice/voices.bin ]] || curl -L --retry 3 --max-time 120 -o .hematite/assets/voice/voices.bin "$base/voices-v1.0.bin"

      - name: Build portable archive
        shell: bash
        run: bash ./scripts/package-unix.sh

      - name: Upload packaged artifacts
        uses: actions/upload-artifact@v4
        with:
          name: hematite-${{ runner.os }}
          path: dist/*/*.tar.gz

      - name: Publish GitHub release assets
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v2
        with:
          files: dist/*/*.tar.gz
