name: Test package

on: [push]

jobs:
  check-launchpad:
    runs-on: ubuntu-latest
    outputs:
      launchpad_accessible: ${{ steps.check.outputs.accessible }}
    steps:
      - name: Check if launchpad.net is accessible
        id: check
        run: |
          # Try to access launchpad.net (timeout after 5 seconds)
          if curl -s --max-time 5 https://launchpad.net > /dev/null; then
            echo "accessible=true" >> $GITHUB_OUTPUT
          else
            echo "accessible=false" >> $GITHUB_OUTPUT
          fi

  latest_w_ppa:
    runs-on: ubuntu-latest
    needs: check-launchpad
    if: needs.check-launchpad.outputs.launchpad_accessible == 'true'
    steps:
      - uses: actions/checkout@v4
      - name: Install dependencies
        run: |
          sudo add-apt-repository ppa:jeffreyratcliffe/ppa
          sudo apt-get update
          sudo apt-get install -y djvulibre-bin gir1.2-gtk-3.0 gir1.2-goocanvas-2.0 \
            gobject-introspection imagemagick img2pdf libtiff-tools poppler-utils \
            python3-gi python3-gi-cairo python3-iso639 python3-pytest-cov \
            python3-pytest-mock python3-pytest-pylint python3-pytest-timeout \
            python3-pytest-xvfb python3-sane python3-tesserocr python3-tomli qpdf \
            tesseract-ocr-eng tesseract-ocr-deu unpaper
          python -m pip install --upgrade pip
          pip install ocrmypdf black==26.3.1

          # add locale, needed for a couple of tests
          sudo locale-gen de_DE.UTF-8
      - name: Display Python version
        run: python -c "import sys; print(sys.version)"
      - name: Black check
        run: black --check .
      - name: Test
        run: pytest-3

  latest:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y imagemagick libtiff-tools poppler-utils \
            djvulibre-bin python3-gi python3-gi-cairo \
            gobject-introspection gir1.2-gtk-3.0 gir1.2-goocanvas-2.0 \
            python3-sane unpaper \
            tesseract-ocr-eng tesseract-ocr-deu python3-tesserocr
          python -m pip install --upgrade pip
          pip install --upgrade pip setuptools wheel packaging build
          pip install .[test]

          # add locale, needed for a couple of tests
          sudo locale-gen de_DE.UTF-8

      - name: Display Python version
        run: python -c "import sys; print(sys.version)"
      - name: Black check
        run: black --check .
      - name: Test
        run: pytest

  oldest:
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v4
      - name: Define variables
        run: |
          (
            echo "GITHUB_REPO_NAME=$(echo "${GITHUB_REPOSITORY}" | cut -d/ -f2)"
            echo "VERSION=$(sed -n 's/^version[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' pyproject.toml)"
          ) >> $GITHUB_ENV

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y imagemagick libtiff-tools poppler-utils \
            djvulibre-bin python3-gi python3-gi-cairo \
            gobject-introspection gir1.2-gtk-3.0 gir1.2-goocanvas-2.0 \
            python3-sane unpaper \
            tesseract-ocr-eng tesseract-ocr-deu python3-tesserocr
          python -m pip install --upgrade pip
          pip install --upgrade pip setuptools wheel packaging build
          pip install .[test]

          # add locale, needed for a couple of tests
          sudo locale-gen de_DE.UTF-8

      - name: Display Python version
        run: python -c "import sys; print(sys.version)"
      - name: Black check
        run: black --check .
      - name: Build wheel
        run: python3 -m build --wheel
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ env.GITHUB_REPO_NAME }}-${{ env.VERSION }}-py3-none-any.whl
          path: dist/${{ env.GITHUB_REPO_NAME }}-${{ env.VERSION }}-py3-none-any.whl

      - name: Release packages
        uses: softprops/action-gh-release@v2
        if: github.ref_type == 'tag'
        with:
          files: dist/${{ env.GITHUB_REPO_NAME }}-${{ env.VERSION }}-py3-none-any.whl

      - name: Test
        run: pytest
