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 Chrome and tools (no snap)
        run: |
          set -euxo pipefail
          if command -v sudo >/dev/null 2>&1; then SUDO=sudo; else SUDO=; fi
          $SUDO apt-get update
          # Base tools and fonts
          DEBIAN_FRONTEND=noninteractive $SUDO apt-get install -y --no-install-recommends \
            ca-certificates gnupg make fonts-liberation

          # Add Google's official Chrome APT repo (avoids Ubuntu's snap-only chromium)
          $SUDO install -d -m 0755 /etc/apt/keyrings
          curl -fsSL https://dl.google.com/linux/linux_signing_key.pub | $SUDO gpg --dearmor -o /etc/apt/keyrings/google-linux-signing-keyring.gpg
          echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/google-linux-signing-keyring.gpg] https://dl.google.com/linux/chrome/deb/ stable main" | \
            $SUDO tee /etc/apt/sources.list.d/google-chrome.list >/dev/null
          $SUDO apt-get update
          DEBIAN_FRONTEND=noninteractive $SUDO apt-get install -y --no-install-recommends google-chrome-stable

          # Clean up apt lists to keep image lean
          $SUDO rm -rf /var/lib/apt/lists/*

      - name: Build PDF
        env:
          # Ensure our tools pick Chrome first if multiple are present
          CHROME_PATH: /usr/bin/google-chrome-stable
        run: make pdf

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