name: Build Linux

on:
  workflow_call:
    inputs:
      run-tests:
        type: boolean
        default: false
      run-clippy:
        type: boolean
        default: false
      build-packages:
        type: boolean
        default: false
  workflow_dispatch:
    inputs:
      run-tests:
        type: boolean
        default: true
      run-clippy:
        type: boolean
        default: true
      build-packages:
        type: boolean
        default: false

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
          components: clippy

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

      - 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
        run: cargo build --release --target x86_64-unknown-linux-gnu

      - name: Build desktop
        run: cargo build --release --features desktop --bin cagire-desktop --target x86_64-unknown-linux-gnu

      - name: Test
        if: inputs.run-tests
        run: cargo test --target x86_64-unknown-linux-gnu

      - name: Clippy
        if: inputs.run-clippy
        run: cargo clippy --target x86_64-unknown-linux-gnu -- -D warnings

      - name: Install cargo-bundle
        if: inputs.build-packages
        run: cargo install cargo-bundle

      - name: Bundle desktop app
        if: inputs.build-packages
        run: cargo bundle --release --features desktop --bin cagire-desktop --target x86_64-unknown-linux-gnu

      - name: Build AppImages
        if: inputs.build-packages
        run: |
          mkdir -p target/releases
          scripts/make-appimage.sh target/x86_64-unknown-linux-gnu/release/cagire x86_64 target/releases
          scripts/make-appimage.sh target/x86_64-unknown-linux-gnu/release/cagire-desktop x86_64 target/releases

      - name: Bundle CLAP plugin
        if: inputs.build-packages
        run: cargo xtask bundle cagire-plugins --release --target x86_64-unknown-linux-gnu

      - name: Upload CLI artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-linux-x86_64
          path: target/x86_64-unknown-linux-gnu/release/cagire

      - name: Upload desktop artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-linux-x86_64-desktop
          path: target/x86_64-unknown-linux-gnu/release/bundle/deb/*.deb

      - name: Upload AppImage artifacts
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-linux-x86_64-appimage
          path: target/releases/*.AppImage

      - name: Prepare plugin artifacts
        if: inputs.build-packages
        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
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-linux-x86_64-clap
          path: staging/clap/

      - name: Upload VST3 artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: cagire-linux-x86_64-vst3
          path: staging/vst3/
