name: Build PDF

on:
  push:
    branches: [ main, master ]
  pull_request:
  workflow_dispatch:

jobs:
  pdf:
    name: make pdf
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Go (from go.mod)
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod
          check-latest: true
          cache: true

      - name: Install Chromium and tools
        run: |
          set -euxo pipefail
          if command -v sudo >/dev/null 2>&1; then SUDO=sudo; else SUDO=; fi
          $SUDO apt-get update
          # Prefer chromium if available via apt (Debian/Ubuntu variants); fall back to chromium-browser if needed
          if apt-cache show chromium >/dev/null 2>&1; then
            DEBIAN_FRONTEND=noninteractive $SUDO apt-get install -y --no-install-recommends \
              make chromium ca-certificates fonts-liberation
          else
            DEBIAN_FRONTEND=noninteractive $SUDO apt-get install -y --no-install-recommends \
              make chromium-browser ca-certificates fonts-liberation || true
          fi
          $SUDO rm -rf /var/lib/apt/lists/*

      - name: Build PDF
        run: make pdf

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: output-pdf
          path: _dist/output.pdf
          if-no-files-found: error
