name: "Generate API doc and publish to GitHub Pages"

on:
  # workflow_dispatch event is a manual run
  workflow_dispatch:

  push:
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
      - 'test/**'
      - '.github/workflows/test*'

  pull_request:
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
      - 'test/**'
      - '.github/workflows/test*'

# This allows a subsequently queued workflow run to interrupt previous runs.
# github.workflow is the name of the workflow.
#   If the workflow file doesn't specify a name,
#   the value of this property is the full path of the workflow
#   file in the repository.
# github.head_ref is source branch of a pull_request
# github.ref	The fully-formed ref of the branch or tag that triggered the workflow run.
#   For workflows triggered by push, this is the branch or tag ref that was pushed.
#   For workflows triggered by pull_request, this is the pull request merge branch.
#   ...
#   For workflow_dispatch this is the branch or tag ref that triggered the workflow run.
concurrency:
  group: '${{ github.workflow }} @ ${{ github.head_ref || github.ref }}'
  cancel-in-progress: true

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  build-api-doc:
    runs-on: ubuntu-latest

    steps:
      - name: Print debug info header
        run: |
          echo concurrency group: '${{ github.workflow }} @ ${{ github.head_ref || github.ref }}'
          echo github.workflow: ${{ github.workflow }}
          echo github.event_name: ${{ github.event_name }}
          echo github.event.pull_request.head.label: ${{ github.event.pull_request.head.label }}
          echo github.head_ref: ${{ github.head_ref }}
          echo github.ref: ${{ github.ref }}
          echo github.ref_name: ${{ github.ref_name }}

      - name: Check out repository code
        uses: actions/checkout@v3
        with:
          path: ${{ github.workspace }}/docker-home/cl-plus-ssl

      - name: Check out package-doc-dump # (since it's not in Quicklisp yet)
        uses: actions/checkout@v3
        with:
          repository: avodonosov/package-doc-dump
          ref: master
          path: ${{ github.workspace }}/docker-home/package-doc-dump

      - name: Check out browsable-colorize # (since it's not in Quicklisp yet)
        uses: actions/checkout@v3
        with:
          repository: avodonosov/browsable-colorize
          ref: master
          path: ${{ github.workspace }}/docker-home/browsable-colorize

      - name: List files in the repository
        run: |
          pwd
          ls ${{ github.workspace }}/docker-home/

      - name: Ask the clfoundation/cl-devel docker image to fill the directory we mount to it as /home/cl with default config (init files for lisps, quiclisp, etc)
        run: |
          docker run -u "$(id -u):$(id -g)" -i --mount type=bind,source=${{ github.workspace }}/docker-home,target=/home/cl/ clfoundation/cl-devel:2022-02-09 unpack-default-home-dir

      - name: run package-doc-dump
        run: |
          docker run -u "$(id -u):$(id -g)" -i --mount type=bind,source=${{ github.workspace }}/docker-home,target=/home/cl/ clfoundation/cl-devel:2022-02-09 -q ccl --batch --load /home/cl/cl-plus-ssl/.github/workflows/api-doc.lisp

      - name: Update quicklisp (needed for fixed "colorize" version)
        run: |
          docker run -u "$(id -u):$(id -g)" -i --mount type=bind,source=${{ github.workspace }}/docker-home,target=/home/cl/ clfoundation/cl-devel:2022-02-09 -q ccl --batch --eval '(ql-dist:install-dist "http://beta.quicklisp.org/dist/quicklisp/2023-02-15/distinfo.txt" :replace t :prompt nil)'

      - name: run browsable-colorize
        run: |
          docker run -u "$(id -u):$(id -g)" -i --mount type=bind,source=${{ github.workspace }}/docker-home,target=/home/cl/ clfoundation/cl-devel:2022-02-09 -q ccl --batch --load /home/cl/cl-plus-ssl/.github/workflows/browsable-package-files.lisp
      - name: List files in the repository
        run: |
          pwd
          ls ${{ github.workspace }}/docker-home/

      - name: prepare GitHub Pages content directory
        run: |
          mkdir ${{ github.workspace }}/github-pages/
          # package-doc-dump results
          cp ${{ github.workspace }}/docker-home/cl-plus-ssl-api.html ${{ github.workspace }}/github-pages/
          # browsable-colorize results
          cp ${{ github.workspace }}/docker-home/cl-plus-ssl/src/package.html ${{ github.workspace }}/github-pages/
          cp ${{ github.workspace }}/docker-home/cl-plus-ssl/src/config.html ${{ github.workspace }}/github-pages/
          # the legacy index.html
          cp ${{ github.workspace }}/docker-home/cl-plus-ssl/index.html ${{ github.workspace }}/github-pages/
          cp ${{ github.workspace }}/docker-home/cl-plus-ssl/index.css ${{ github.workspace }}/github-pages/

      - name: Package and upload GitHub Pages artifact
        if: |
          'master' == github.ref_name
              && ( 'workflow_dispatch' == github.event_name
                  || 'push' == github.event_name )
        uses: actions/upload-pages-artifact@v3
        with:
          path: ${{ github.workspace }}/github-pages


  deploy:
    if: |
      'master' == github.ref_name
          && ( 'workflow_dispatch' == github.event_name
              || 'push' == github.event_name )
    # Additionally protected by the deployment environment.
    # GitHub will automatically create a deployment
    # environment with the specified name, if not
    # exists already, and only the master branch will
    # have access to it by default.
    # (Can be managed in the Settings / Environments
    # of the repo)
    environment:
      name: github-pages
      # Display the resulting GitHub Pages URL at this
      # job box in the visualization graph for the workflow run
      url: ${{ steps.deployment.outputs.page_url }}cl-plus-ssl-api.html
    runs-on: ubuntu-latest
    needs: build-api-doc
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
