name: CI

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

jobs:
  typos:
    name: "Typos"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check typos
        uses: crate-ci/typos@master
        with:
          config: ./tools/typos.toml

  typstyle:
    name: "Typst formatting"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: "Download Typstyle"
        run: |
          wget -q "https://github.com/Enter-tainer/typstyle/releases/download/v0.12.14/typstyle-x86_64-unknown-linux-musl"
          chmod +x typstyle-x86_64-unknown-linux-musl

      - name: Check typst formatting
        run: |
          find . -name "*.typ" -type f -print0 | xargs -0 \
            ./typstyle-x86_64-unknown-linux-musl --check

  build:
    needs:
      - typos
      - typstyle

    name: "Build"
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      # Could instead install `texlive-full`, but that takes ~20 minutes.
      # We'll specify the packages we need manually.
      - name: "Install TeXLive"
        run: |
          sudo apt update
          DEBIAN_FRONTEND=noninteractive \
            sudo apt install --yes \
            texlive texlive-xetex \
            texlive-games texlive-fonts-extra texlive-latex-extra \
            texlive-pictures texlive-pstricks \
            python3-requests

      # Typst isn't packaged, and manual download gives us
      # more control anyway.
      - name: "Download Typst"
        run: |
          wget -q "https://github.com/typst/typst/releases/download/v0.12.0/typst-x86_64-unknown-linux-musl.tar.xz"
          tar -xf "typst-x86_64-unknown-linux-musl.tar.xz"
          mv "typst-x86_64-unknown-linux-musl/typst" .
          rm "typst-x86_64-unknown-linux-musl.tar.xz"
          rm -dr "typst-x86_64-unknown-linux-musl"

      # Builds all handouts, LaTeX and Typst
      - name: "Build handouts"
        run: TYPST_PATH="$(pwd)/typst" python tools/scripts/build.py

      # Upload logs, even if build fails.
      # LaTeX stdout/stderr isn't always helpful.
      - name: "Save LaTeX logs"
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: "LaTeX logs"
          path: "**/*.log"
          retention-days: 14

      # Upload build output
      - name: "Save output"
        uses: actions/upload-artifact@v3
        with:
          name: "Build output"
          path: "output/*"
          retention-days: 7

      - name: "Publish package (hash)"
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
        run: |
          PUBLISH_USER="${{ secrets.PUBLISH_USER }}" \
          PUBLISH_KEY="${{ secrets.PUBLISH_KEY }}" \
          VERSION="${{ github.sha }}" \
          PACKAGE="${{ vars.PACKAGE }}" \
            python tools/scripts/publish.py

      - name: "Publish package (latest)"
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
        run: |
          PUBLISH_USER="${{ secrets.PUBLISH_USER }}" \
          PUBLISH_KEY="${{ secrets.PUBLISH_KEY }}" \
          VERSION="latest" \
          PACKAGE="${{ vars.PACKAGE }}" \
            python tools/scripts/publish.py
