---
# This is a basic workflow to help you get started with Actions

name: Docs

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [main]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:

  # Check changes, cancel job is not.
  check_changes:
    runs-on: ubuntu-latest
    outputs:
      distrobox_changed: ${{ steps.check_file_changed.outputs.distrobox_changed }}
    steps:

      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          # Checkout as many commits as needed for the diff
          repository: 89luca89/distrobox
          ref: main
          persist-credentials: false
          fetch-depth: 2
          token: ${{ secrets.GITHUB_TOKEN }}

      # Fetch from compatibility table all the distros supported
      - id: check_file_changed
        run: |
          if git diff --name-only HEAD^ HEAD  | grep -E "^docs|gen-man"; then
            echo "::set-output name=distrobox_changed::True"
          else
            echo "::set-output name=distrobox_changed::False"
          fi

  gen_man:
    runs-on: ubuntu-latest
    needs: check_changes
    if: needs.check_changes.outputs.distrobox_changed == 'True'
    permissions:
      contents: write
    steps:

      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          token: ${{ secrets.PAT }}

      # Fetch from compatibility table all the distros supported
      - id: generate
        run: |
          VERSION=3.6.1
          RELEASE="jgm/pandoc/releases/download/${VERSION}/pandoc-${VERSION}-1-amd64.deb"
          NAME=$(echo $RELEASE | rev | cut -d'/' -f1 | rev)

          curl -L https://github.com/$RELEASE -o $NAME
          sudo apt-get update
          sudo apt-get install -y ./$NAME
          sudo apt-get install -y ronn

          rm -f $NAME

          man/gen-man

      - uses: stefanzweifel/git-auto-commit-action@4a55954c782fc1ea30b9056cd3e7a2b40ca8887d # v7.2.0
        with:
          branch: main
          commit_message: Automatic Man Page Update
          commit_options: '--no-verify --signoff'
          commit_user_name: distrobox-docs-bot
          commit_user_email: distrobox-docs-bot@users.noreply.github.com
          commit_author: distrobox-docs-bot <actions@github.com>
