name: Build Plugins RPi

on:
  workflow_call:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 60

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-unknown-linux-gnu

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: aarch64-unknown-linux-gnu-plugins

      - name: Install cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build plugins
        run: cross build --release -p cagire-plugins --target aarch64-unknown-linux-gnu

      - name: Prepare plugin artifacts
        run: |
          mkdir -p target/bundled
          # CLAP: single .so renamed to .clap
          cp target/aarch64-unknown-linux-gnu/release/libcagire_plugins.so target/bundled/cagire-plugins.clap
          # VST3: correct directory structure
          mkdir -p "target/bundled/cagire-plugins.vst3/Contents/aarch64-linux"
          cp target/aarch64-unknown-linux-gnu/release/libcagire_plugins.so "target/bundled/cagire-plugins.vst3/Contents/aarch64-linux/cagire-plugins.so"

          mkdir -p staging/clap staging/vst3
          cp -R target/bundled/cagire-plugins.clap staging/clap/
          cp -R target/bundled/cagire-plugins.vst3 staging/vst3/

      - name: Upload CLAP artifact
        uses: actions/upload-artifact@v4
        with:
          name: plugins-linux-aarch64-clap
          path: staging/clap/

      - name: Upload VST3 artifact
        uses: actions/upload-artifact@v4
        with:
          name: plugins-linux-aarch64-vst3
          path: staging/vst3/
