name: Auto-update doxygen-awesome

on:
  workflow_dispatch:
  schedule:
  - cron: 0 2 * * 1

jobs:
  check-for-updates:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - uses: actions/checkout@v2
      - name: Open PR if doxygen-awesome outdated.
        run: |

          VERSION=$(doc/doxygen-awesome-css/update_doxygen_awesome.sh "$(mktemp -d)")
          BRANCH=update-doxygen-awesome-${VERSION}
          COMMIT_MESSAGE="Update doxygen-awesome to ${VERSION}"

          # NOTE: In a later runs of CI we will search for PR with this exact
          #       title. Only if no such PR exists will the script create a
          #       new PR.
          PR_TITLE="[docs] Update doxygen-awesome to ${VERSION}"

          if [[ -z "$(git status --porcelain)" ]]
          then
              echo "No differences detected: doxygen-awesome is up-to-date."
              exit 0
          fi

          if [[ -z "$(gh pr list --state all --search "${PR_TITLE}")" ]]
          then

              git checkout -b $BRANCH
              git config user.name github-actions
              git config user.email github-actions@github.com
              git commit -a -m "${COMMIT_MESSAGE}"

              git push -u origin ${BRANCH}
              gh pr create \
                  --title "${PR_TITLE}" \
                  --body "This PR was generated by a Github Actions workflow."

          else
              echo "Old PR detected: didn't create a new one."
          fi
