name: Release
on:
  push:
    tags:
      - "*"

env:
  CRATE_NAME: ocarina-tui

jobs:
  github_build:
    name: Build release binaries
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            name: aarch64-unknown-linux-musl.tar.gz
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: x86_64-unknown-linux-gnu.tar.gz
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            name: x86_64-unknown-linux-musl.tar.gz
          - target: x86_64-apple-darwin
            os: macOS-latest
            name: x86_64-apple-darwin.tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            name: x86_64-pc-windows-msvc.zip
    runs-on: ${{ matrix.os }}
    steps:
      - name: Setup | Checkout
        uses: actions/checkout@v4

      - name: Setup | Cache Cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Setup | Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          target: ${{ matrix.target }}

      - name: Setup | cross
        if: endsWith(matrix.target, '-unknown-linux-musl')
        uses: taiki-e/install-action@v2
        with:
          tool: cross

      - name: Setup | Install ALSA
        if: matrix.os == 'ubuntu-latest'
        run: sudo apt-get update && sudo apt-get install -y libasound2-dev pkg-config

      - name: Build | Build
        if: ${{ !endsWith(matrix.target, '-unknown-linux-musl') }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build | Build (musl)
        if: endsWith(matrix.target, '-unknown-linux-musl')
        run: cross build --release --target ${{ matrix.target }}

      - name: Post Setup | Extract tag name
        shell: bash
        run: echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
        id: extract_tag

      - name: Post Setup | Prepare artifacts [Windows]
        if: matrix.os == 'windows-latest'
        run: |
          mkdir target/stage
          cd target/${{ matrix.target }}/release
          7z a ../../stage/${{ env.CRATE_NAME }}-${{ steps.extract_tag.outputs.tag }}-${{ matrix.name }} ${{ env.CRATE_NAME }}.exe
          cd -
      - name: Post Setup | Prepare artifacts [-nix]
        if: matrix.os != 'windows-latest'
        run: |
          mkdir target/stage
          cd target/${{ matrix.target }}/release
          tar czvf ../../stage/${{ env.CRATE_NAME }}-${{ steps.extract_tag.outputs.tag }}-${{ matrix.name }} ${{ env.CRATE_NAME }}
          cd -
      - name: Post Setup | Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.CRATE_NAME }}-${{ steps.extract_tag.outputs.tag }}-${{ matrix.name }}
          path: target/stage/*
          # Idempotency: overwrite artefact by name if we're rerunning the deployment
          overwrite: true

  github_release:
    name: Create GitHub Release
    needs: github_build
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Setup | Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup | Artifacts
        uses: actions/download-artifact@v4

      - name: Setup | Extract version
        shell: bash
        run: echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
        id: extract_version

      - name: Setup | Release notes
        run: |
          sed -n '/^## \[${{ steps.extract_version.outputs.version }}\].*$/,/^## /p' CHANGELOG.md | sed '$d' > RELEASE.md

      - name: Build | Publish
        uses: softprops/action-gh-release@v2
        with:
          files: ${{ env.CRATE_NAME }}-*/${{ env.CRATE_NAME }}-*
          body_path: RELEASE.md
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
