name: Assemble macOS Universal

on:
  workflow_call:

jobs:
  assemble:
    runs-on: macos-14
    timeout-minutes: 10

    steps:
      - name: Download macOS artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: cagire-macos-*
          path: artifacts

      - name: Create universal CLI binary
        run: |
          lipo -create \
            artifacts/cagire-macos-x86_64/cagire \
            artifacts/cagire-macos-aarch64/cagire \
            -output cagire
          chmod +x cagire
          lipo -info cagire

      - name: Create universal app bundle
        run: |
          cd artifacts/cagire-macos-aarch64-desktop
          unzip Cagire.app.zip
          cd ../cagire-macos-x86_64-desktop
          unzip Cagire.app.zip
          cd ../..
          cp -R artifacts/cagire-macos-aarch64-desktop/Cagire.app Cagire.app
          lipo -create \
            artifacts/cagire-macos-x86_64-desktop/Cagire.app/Contents/MacOS/cagire-desktop \
            artifacts/cagire-macos-aarch64-desktop/Cagire.app/Contents/MacOS/cagire-desktop \
            -output Cagire.app/Contents/MacOS/cagire-desktop
          lipo -info Cagire.app/Contents/MacOS/cagire-desktop
          zip -r Cagire.app.zip Cagire.app

      - name: Create universal CLAP plugin
        run: |
          mkdir -p cagire-plugins.clap/Contents/MacOS
          cp artifacts/cagire-macos-aarch64-clap/cagire-plugins.clap/Contents/Info.plist \
            cagire-plugins.clap/Contents/ 2>/dev/null || true
          cp artifacts/cagire-macos-aarch64-clap/cagire-plugins.clap/Contents/PkgInfo \
            cagire-plugins.clap/Contents/ 2>/dev/null || true
          lipo -create \
            artifacts/cagire-macos-x86_64-clap/cagire-plugins.clap/Contents/MacOS/cagire-plugins \
            artifacts/cagire-macos-aarch64-clap/cagire-plugins.clap/Contents/MacOS/cagire-plugins \
            -output cagire-plugins.clap/Contents/MacOS/cagire-plugins
          lipo -info cagire-plugins.clap/Contents/MacOS/cagire-plugins

      - name: Create universal VST3 plugin
        run: |
          mkdir -p cagire-plugins.vst3/Contents/MacOS
          cp -R artifacts/cagire-macos-aarch64-vst3/cagire-plugins.vst3/Contents/Info.plist \
            cagire-plugins.vst3/Contents/ 2>/dev/null || true
          cp artifacts/cagire-macos-aarch64-vst3/cagire-plugins.vst3/Contents/PkgInfo \
            cagire-plugins.vst3/Contents/ 2>/dev/null || true
          cp -R artifacts/cagire-macos-aarch64-vst3/cagire-plugins.vst3/Contents/Resources \
            cagire-plugins.vst3/Contents/ 2>/dev/null || true
          lipo -create \
            artifacts/cagire-macos-x86_64-vst3/cagire-plugins.vst3/Contents/MacOS/cagire-plugins \
            artifacts/cagire-macos-aarch64-vst3/cagire-plugins.vst3/Contents/MacOS/cagire-plugins \
            -output cagire-plugins.vst3/Contents/MacOS/cagire-plugins
          lipo -info cagire-plugins.vst3/Contents/MacOS/cagire-plugins

      - uses: actions/checkout@v4
        with:
          sparse-checkout: |
            assets/DMG-README.txt
            scripts/make-dmg.sh
          clean: false

      - name: Create DMG
        run: |
          chmod +x scripts/make-dmg.sh
          scripts/make-dmg.sh Cagire.app .

      - name: Build .pkg installer
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          mkdir -p pkg-root/Applications pkg-root/usr/local/bin
          cp -R Cagire.app pkg-root/Applications/
          cp cagire pkg-root/usr/local/bin/
          pkgbuild --analyze --root pkg-root component.plist
          plutil -replace BundleIsRelocatable -bool NO component.plist
          pkgbuild --root pkg-root --identifier com.sova.cagire \
            --version "$VERSION" --install-location / \
            --component-plist component.plist \
            "Cagire-${VERSION}-universal.pkg"

      - name: Upload universal CLI
        uses: actions/upload-artifact@v4
        with:
          name: cagire-macos-universal
          path: cagire

      - name: Upload universal app bundle
        uses: actions/upload-artifact@v4
        with:
          name: cagire-macos-universal-desktop
          path: Cagire.app.zip

      - name: Prepare universal plugin staging
        run: |
          mkdir -p staging/clap staging/vst3
          cp -R cagire-plugins.clap staging/clap/
          cp -R cagire-plugins.vst3 staging/vst3/

      - name: Upload universal CLAP plugin
        uses: actions/upload-artifact@v4
        with:
          name: cagire-macos-universal-clap
          path: staging/clap/

      - name: Upload universal VST3 plugin
        uses: actions/upload-artifact@v4
        with:
          name: cagire-macos-universal-vst3
          path: staging/vst3/

      - name: Upload DMG
        uses: actions/upload-artifact@v4
        with:
          name: cagire-macos-universal-dmg
          path: Cagire-*.dmg

      - name: Upload .pkg installer
        uses: actions/upload-artifact@v4
        with:
          name: cagire-macos-universal-pkg
          path: Cagire-*-universal.pkg
