name: Release

on:
  workflow_dispatch:
  push:
    tags: ['v*']

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  linux:
    if: github.server_url == 'https://github.com'
    uses: ./.github/workflows/build-linux.yml
    with:
      build-packages: true

  macos:
    if: github.server_url == 'https://github.com'
    uses: ./.github/workflows/build-macos.yml
    with:
      build-packages: true
      matrix: >-
        [
          {"os":"macos-14","target":"aarch64-apple-darwin","artifact":"cagire-macos-aarch64"},
          {"os":"macos-15-intel","target":"x86_64-apple-darwin","artifact":"cagire-macos-x86_64"}
        ]

  windows:
    if: github.server_url == 'https://github.com'
    uses: ./.github/workflows/build-windows.yml
    with:
      build-packages: true

  cross:
    if: github.server_url == 'https://github.com'
    uses: ./.github/workflows/build-cross.yml

  assemble-macos:
    needs: macos
    uses: ./.github/workflows/assemble-macos.yml

  release:
    needs: [linux, macos, windows, cross, assemble-macos]
    if: startsWith(github.ref, 'refs/tags/v') && github.server_url == 'https://github.com'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: write

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Prepare release files
        run: |
          mkdir -p release
          for dir in artifacts/*/; do
            name=$(basename "$dir")
            if [[ "$name" == "cagire-macos-universal-dmg" ]]; then
              cp "$dir"/*.dmg release/
            elif [[ "$name" == "cagire-macos-universal-pkg" ]]; then
              cp "$dir"/*.pkg release/
            elif [[ "$name" == "cagire-macos-universal-desktop" ]]; then
              cp "$dir/Cagire.app.zip" "release/cagire-macos-universal-desktop.app.zip"
            elif [[ "$name" == "cagire-macos-universal" ]]; then
              cp "$dir/cagire" "release/cagire-macos-universal"
            elif [[ "$name" == "cagire-macos-universal-clap" ]]; then
              cd "$dir" && zip -r "../../release/cagire-macos-universal-clap.zip" cagire-plugins.clap && cd ../..
            elif [[ "$name" == "cagire-macos-universal-vst3" ]]; then
              cd "$dir" && zip -r "../../release/cagire-macos-universal-vst3.zip" cagire-plugins.vst3 && cd ../..
            elif [[ "$name" == *-clap ]]; then
              base="${name%-clap}"
              cd "$dir" && zip -r "../../release/${base}-clap.zip" cagire-plugins.clap && cd ../..
            elif [[ "$name" == *-vst3 ]]; then
              base="${name%-vst3}"
              cd "$dir" && zip -r "../../release/${base}-vst3.zip" cagire-plugins.vst3 && cd ../..
            elif [[ "$name" == *-installer ]]; then
              cp "$dir"/*-setup.exe release/
            elif [[ "$name" == *-appimage ]]; then
              cp "$dir"/*.AppImage release/
            elif [[ "$name" == *-desktop ]]; then
              base="${name%-desktop}"
              if ls "$dir"/*.deb 1>/dev/null 2>&1; then
                cp "$dir"/*.deb "release/${base}-desktop.deb"
              elif [ -f "$dir/Cagire.app.zip" ]; then
                cp "$dir/Cagire.app.zip" "release/${base}-desktop.app.zip"
              elif [ -f "$dir/cagire-desktop.exe" ]; then
                cp "$dir/cagire-desktop.exe" "release/${base}-desktop.exe"
              fi
            else
              if [ -f "$dir/cagire.exe" ]; then
                cp "$dir/cagire.exe" "release/${name}.exe"
              elif [ -f "$dir/cagire" ]; then
                cp "$dir/cagire" "release/${name}"
              fi
            fi
          done

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: release/*
          generate_release_notes: true
