name: Test bundles and wheels on multiple Linux distros

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  wheel:
    name: Build wheel
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.14"
      - name: Set up uv
        uses: astral-sh/setup-uv@v7
      - name: Generate pyside files
        run: uv run --no-dev python -m tools.generate_pyside_files
      - name: Get version from hatch
        run: |
          RAW_VERSION=$(uvx --with hatch hatch version)
          echo "Raw version: $RAW_VERSION"
          echo "RAW_VERSION=$RAW_VERSION" >> ${GITHUB_OUTPUT}

          CLEAN_VERSION="${RAW_VERSION%+*}"

          git config user.name "gha"
          git config user.email "gha@localhost"
          git tag -a "v$CLEAN_VERSION" -m "Local tag"

          echo "Created temporary tag: v$CLEAN_VERSION"
      - name: Build the wheel
        run: uv build --wheel --force-pep517
      - name: Upload whl artifact
        uses: actions/upload-artifact@v6
        with:
          name: wheel
          path: dist/*.whl
  bundle:
    name: Build bundle
    runs-on: ubuntu-latest
    container: ghcr.io/bohning/syncer_linux_builder_image:latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-python@v5
        with:
          python-version: "3.14"
      - uses: astral-sh/setup-uv@v7
      - run: uv run --no-dev --group bundle python -m tools.generate_pyside_files
      - run: uv run --no-dev --group bundle python -m tools.bundle Linux --version TestBuild
      - name: Upload bundle artifact
        uses: actions/upload-artifact@v6
        with:
          name: bundle
          path: dist/USDB_Syncer*

  test_bundle:
    name: Test bundle
    runs-on: ubuntu-latest
    container: ${{ matrix.image }}
    needs: [bundle]
    strategy:
      fail-fast: false
      matrix:
        include:
          - image: ubuntu:25.10
            COMPATIBILITY: ubuntu
          - image: ubuntu:24.04
            COMPATIBILITY: ubuntu
          - image: ubuntu:22.04
            COMPATIBILITY: ubuntu
          - image: debian:12
            COMPATIBILITY: ubuntu
          - image: debian:13
            COMPATIBILITY: ubuntu
          - image: fedora:42
            COMPATIBILITY: fedora
          - image: fedora:43
            COMPATIBILITY: fedora
          - image: archlinux:latest
            COMPATIBILITY: archlinux
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v7
        with:
          name: bundle
      - run: ls -R
      - name: Set up ubuntu packages
        if: matrix.COMPATIBILITY == 'ubuntu'
        run: |
          apt update
          apt install -y libgl1 libegl1 libva2 libva-drm2 libva-x11-2 pulseaudio libpipewire-0.3-0 libportaudio2 xvfb
      - name: Set up fedora packages
        if: matrix.COMPATIBILITY == 'fedora'
        run: |
          dnf install -y dbus-daemon libva pulseaudio pipewire portaudio xorg-x11-server-Xvfb
      - name: Set up archlinux packages
        if: matrix.COMPATIBILITY == 'archlinux'
        run: |
          pacman -Sy --noconfirm dbus libva pulseaudio pipewire portaudio xorg-server-xvfb
          groupadd -g 500 pulse
          useradd -u 500 -g pulse -d /var/run/pulse -s /usr/bin/nologin pulse
          usermod -aG audio pulse
      - name: Run the binary
        run: |
          mkdir -p /var/run/dbus
          dbus-daemon --system --fork
          mkdir -p /etc/pulse/client.conf.d
          pulseaudio -D --system --disallow-exit --exit-idle-time=-1
          export QT_FATAL_WARNINGS=1
          export LC_CTYPE="C.utf8"

          chmod +x ./USDB_Syncer-TestBuild-Linux
          xvfb-run -a timeout 60s ./USDB_Syncer-TestBuild-Linux --healthcheck

  test_wheel:
    name: Test wheel
    runs-on: ubuntu-latest
    needs: [wheel]
    container: ${{ matrix.image }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - image: ubuntu:25.10
            COMPATIBILITY: ubuntu
          - image: ubuntu:24.04
            COMPATIBILITY: ubuntu
          - image: ubuntu:22.04
            COMPATIBILITY: ubuntu
          - image: debian:12
            COMPATIBILITY: ubuntu
          - image: debian:13
            COMPATIBILITY: ubuntu
          - image: fedora:42
            COMPATIBILITY: fedora
          - image: fedora:43
            COMPATIBILITY: fedora
          - image: archlinux:latest
            COMPATIBILITY: archlinux
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v7
        with:
          name: wheel
      - run: ls -R
      - name: Set up python
        if: matrix.image != 'debian:12' && matrix.image != 'ubuntu:22.04' # debian:12 and ubuntu:22.04 are too old for setup-python (https://github.com/actions/setup-python/issues/1224)
        uses: actions/setup-python@v5
        with:
          python-version: "3.14"
      - name: Install python3.11 (for debian:12)
        if: matrix.image == 'debian:12'
        run: |
          apt update
          apt install -y python3 python3-venv python3-pip
          ln -s $(which python3) /usr/local/bin/python
      - name: Install python3.12 (for ubuntu:22.04) using deadsnakes PPA.
        if: matrix.image == 'ubuntu:22.04'
        run: |
          apt update
          apt install -y software-properties-common
          add-apt-repository ppa:deadsnakes/ppa
          apt update
          DEBIAN_FRONTEND=noninteractive apt install -y python3.14 python3.14-venv
          ln -s /usr/bin/python3.14 /usr/local/bin/python

      - name: Set up ubuntu packages
        if: matrix.COMPATIBILITY == 'ubuntu'
        run: |
          apt update
          apt install -y libgssapi-krb5-2 libgl1 libegl1 libva2 libva-drm2 libva-x11-2 pulseaudio libpipewire-0.3-0 libportaudio2 libxkbcommon0 libxkbcommon-x11-0 libxcb-cursor0 libxcb-icccm4 libxcb-keysyms1 libxcb-shape0 libxrandr2 libfontconfig1 xvfb
      - name: Set up fedora packages
        if: matrix.COMPATIBILITY == 'fedora'
        run: |
          dnf install -y libglvnd-glx libglvnd-egl fontconfig libxkbcommon dbus-daemon libXrandr libxkbcommon-x11 xcb-util-cursor xcb-util-wm xcb-util-keysyms libva pulseaudio pipewire portaudio xorg-x11-server-Xvfb
      - name: Set up archlinux packages
        if: matrix.COMPATIBILITY == 'archlinux'
        run: |
          pacman -Sy --noconfirm fontconfig libxkbcommon libxkbcommon-x11 xcb-util-cursor xcb-util-wm xcb-util-keysyms dbus pulseaudio pipewire portaudio xorg-server-xvfb
          groupadd -g 500 pulse
          useradd -u 500 -g pulse -d /var/run/pulse -s /usr/bin/nologin pulse
          usermod -aG audio pulse
      - name: Run the wheel
        run: |
          mkdir -p /var/run/dbus
          dbus-daemon --system --fork
          mkdir -p /etc/pulse/client.conf.d
          pulseaudio -D --system --disallow-exit --exit-idle-time=-1
          export QT_FATAL_WARNINGS=1
          export LC_CTYPE="C.utf8"

          python -m venv .venv
          . .venv/bin/activate
          python -m ensurepip
          python -m pip install ./*.whl
          xvfb-run -a timeout 60s usdb_syncer --healthcheck

  upload_wheel:
    name: Upload wheel to PyPI
    runs-on: ubuntu-latest
    needs: test_wheel
    if: ${{ github.event_name == 'push' }} # don't upload on manual runs
    environment: pypi
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@v7
        with:
          name: wheel
          path: ${{ github.workspace }}/dist
      - name: Upload to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: ${{ github.workspace }}/dist
          verbose: true
