name: Test Building on Distros

on:
  pull_request:
    branches:
      - master
  workflow_dispatch:
    inputs:
      target:
        description: 'Target distribution to build (or "all" for all targets)'
        required: false
        default: 'all'
        type: choice
        options:
          - all
          - ubuntu-24.04
          - ubuntu-22.04
          - debian-unstable
          - debian-testing
          - debian-stable
          - fedora-42
          - fedora-38

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: ubuntu-24.04
            image: ubuntu:24.04
            setup_script: |
              sed -i 's/^Types: deb/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources
              DEBIAN_FRONTEND=noninteractive apt-get -qq update
              DEBIAN_FRONTEND=noninteractive apt-get -yqq build-dep mlt
              DEBIAN_FRONTEND=noninteractive apt-get -yqq install cmake qt6-base-dev libqt6svg6-dev libqt6core5compat6-dev
          - name: ubuntu-22.04
            image: ubuntu:22.04
            setup_script: |
              sed -i '/^#\sdeb-src /s/^#//' "/etc/apt/sources.list"
              DEBIAN_FRONTEND=noninteractive apt-get -qq update
              DEBIAN_FRONTEND=noninteractive apt-get -yqq build-dep mlt
              DEBIAN_FRONTEND=noninteractive apt-get -yqq install cmake qt6-base-dev libqt6svg6-dev libqt6core5compat6-dev
          - name: debian-unstable
            image: debian:unstable
            setup_script: |
              echo 'deb-src http://deb.debian.org/debian unstable main' >> /etc/apt/sources.list
              apt-get -qq update
              apt-get -yqq build-dep mlt
          - name: debian-testing
            image: debian:testing
            setup_script: |
              echo 'deb-src http://deb.debian.org/debian testing main' >> /etc/apt/sources.list
              apt-get -qq update
              apt-get -yqq build-dep mlt
              apt-get -yqq install cmake qt6-base-dev libqt6svg6-dev libqt6core5compat6-dev
          - name: debian-stable
            image: debian:stable
            setup_script: |
              echo 'deb-src http://deb.debian.org/debian stable main\ndeb-src http://deb.debian.org/debian stable-updates main' >> /etc/apt/sources.list
              echo 'deb http://deb.debian.org/debian bookworm-backports main' >> /etc/apt/sources.list
              apt-get -qq update
              apt-get -yqq build-dep mlt
              apt-get -yqq install cmake qt6-base-dev libqt6svg6-dev libqt6core5compat6-dev
          - name: fedora-42
            image: fedora:42
            setup_script: |
              dnf --assumeyes group install development-tools
              dnf --assumeyes install \
                yasm gavl-devel libsamplerate-devel libxml2-devel \
                ladspa-devel jack-audio-connection-kit-devel sox-devel \
                SDL2-devel fftw-devel frei0r-devel gtk2-devel libexif-devel \
                qt6-qtbase-devel qt6-qt5compat-devel qt6-qtsvg-devel \
                libtheora-devel libvorbis-devel libvdpau-devel \
                libsoup-devel liboil-devel python-devel alsa-lib \
                pulseaudio-libs-devel gcc-c++ cmake ffmpeg-free-devel \
                movit-devel rubberband-devel vid.stab-devel
          - name: fedora-38
            image: fedora:38
            setup_script: |
              yum --assumeyes groupinstall "Development Tools"
              yum --assumeyes install \
                yasm gavl-devel libsamplerate-devel libxml2-devel \
                ladspa-devel jack-audio-connection-kit-devel sox-devel \
                SDL2-devel fftw-devel frei0r-devel gtk2-devel libexif-devel \
                qt6-qtbase-devel qt6-qt5compat-devel qt6-qtsvg-devel \
                libtheora-devel libvorbis-devel libvdpau-devel \
                libsoup-devel liboil-devel python-devel alsa-lib \
                pulseaudio-libs-devel gcc-c++ cmake ffmpeg-free-devel \
                movit-devel rubberband-devel vid.stab-devel

    container:
      image: ${{ matrix.image }}

    name: Build on ${{ matrix.name }}

    steps:
      - name: Check if job should run
        id: should_run
        run: |
          # For pull requests, always run all jobs
          # For workflow_dispatch, check the input parameter
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            echo "run=true" >> $GITHUB_OUTPUT
          elif [ "${{ github.event.inputs.target }}" = "all" ] || [ "${{ github.event.inputs.target }}" = "${{ matrix.name }}" ]; then
            echo "run=true" >> $GITHUB_OUTPUT
          else
            echo "run=false" >> $GITHUB_OUTPUT
          fi

      - name: Checkout code
        if: steps.should_run.outputs.run == 'true'
        uses: actions/checkout@v4

      - name: Setup dependencies
        if: steps.should_run.outputs.run == 'true'
        run: |
          ${{ matrix.setup_script }}

      - name: Build and install
        if: steps.should_run.outputs.run == 'true'
        run: |
          cmake -DCMAKE_BUILD_TYPE=Debug . && make -j -f Makefile install
