name: Update master docs
on:
  push:
    branches:
      - master
    paths:
      - man/*
  workflow_dispatch:

env:
  # Force colored output (see https://bixense.com/clicolors/ and https://force-color.org/)
  TERM: xterm-256color
  CLICOLOR: 1
  CLICOLOR_FORCE: 1

jobs:
  build:
    if: github.repository_owner == 'gbdev'
    runs-on: ubuntu-slim
    steps:
      - name: Checkout rgbds
        uses: actions/checkout@v7
        with:
          path: rgbds
          sparse-checkout: man # Avoid checking out other files, we don't use them here.
      - name: Checkout rgbds-www
        uses: actions/checkout@v7
        with:
          path: rgbds-www
          repository: ${{ github.repository_owner }}/rgbds-www
          ref: master # Need to check out the branch to be able to commit on and push it.
          ssh-key: !!str ${{ secrets.SSH_KEY_SECRET }}
      - name: Install groff and mandoc
        run: |
          sudo apt install -Uyq groff mandoc -o APT::Color=yes -o Acquire::Queue-Mode=access
      - name: Update pages
        working-directory: rgbds/man
        run: |
          ../../rgbds-www/maintainer/man_to_html.sh master *
      - name: Push new pages
        working-directory: rgbds-www
        env:
          GIT_AUTHOR_NAME: !!str ${{ github.actor }}
          GIT_AUTHOR_EMAIL: !!str ${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com
          GIT_COMMITTER_NAME: github-actions[bot]
          GIT_COMMITTER_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
        run: |
          git add -A
          git commit -m "Update RGBDS master documentation"
          git push
