name: Build Plugins Linux

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: x86_64-unknown-linux-gnu

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

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential cmake pkg-config libasound2-dev libclang-dev libjack-dev \
            libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev libgl1-mesa-dev \
            libx11-dev libx11-xcb-dev libxcursor-dev libxrandr-dev libxi-dev libwayland-dev

      - name: Build plugins
        run: cargo xtask bundle cagire-plugins --release --target x86_64-unknown-linux-gnu

      - name: Prepare plugin artifacts
        run: |
          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-x86_64-clap
          path: staging/clap/

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