name: docs

on:
  pull_request:
    types: [ opened, synchronize, reopened, closed ]

  push:
    branches:
      - 'develop'
      - 'main'
    tags:
      - '**'

jobs:
  # Setup environment and build ecflow documentation
  build:
    if: ${{ (github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' }}

    runs-on: ubuntu-22.04

    outputs:
      artifact-id: ${{ steps.archivedocs.outputs.artifact-id }}

    steps:

      - name: Install base dependencies 'boost, qt6, doxygen, python, sphinx, ...'
        run: |
          sudo apt update
          sudo apt install -y \
            libboost-all-dev \
            libssl-dev \
            doxygen \
            graphviz \
            python3 \
            python3-sphinx \
            python3-sphinx-rtd-theme

      - name: Checkout 'ecbuild'
        uses: actions/checkout@v4
        with:
          repository: ecmwf/ecbuild
          path: ecbuild

      - name: Checkout 'ecflow'
        uses: actions/checkout@v4
        with:
          path: ecflow

      - name: Install documentation python requirements
        run: |
          python -m pip install -r ecflow/docs/requirements.txt

      - name: Configure ecflow
        run: |
          cmake -S ./ecflow \
                -B ./build \
                -DENABLE_UI=OFF \
                -DENABLE_DOCS=ON \
                -DENABLE_STATIC_BOOST_LIBS=OFF \
                -DBoost_ROOT=/usr \
                -DBoost_INCLUDE_DIR=/usr/include

      - name: Build ecflow
        run: |
          pwd
          cmake --build ./build -j 4 --target ecflow_docs

      - name: Archive documentation
        id: archivedocs
        uses: actions/upload-artifact@v4
        with:
          name: documentation
          path: ${{ github.workspace }}/build/docs/_build/html

  # Publish the documentation preview for pull requests while they are "ongoing"
  preview-publish:
    if: ${{ (github.event_name == 'pull_request' && github.event.action != 'closed') || github.event_name == 'push' }}
    needs: build
    uses: ecmwf/reusable-workflows/.github/workflows/pr-preview-publish.yml@main
    with:
      artifact-id: ${{ needs.build.outputs.artifact-id }}
      space: docs
      name: dev-section
      path: ecflow/pull-requests
      link-text: 🌦️ >> Documentation << 🌦️
    secrets:
      sites-token: ${{ secrets.ECMWF_SITES_DOCS_DEV_SECTION_TOKEN }}

  # Unpublish the documentation preview for pull requests once they are "closed"
  preview-unpublish:
    if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }}
    uses: ecmwf/reusable-workflows/.github/workflows/pr-preview-unpublish.yml@main
    with:
      space: docs
      name: dev-section
      path: ecflow/pull-requests
    secrets:
      sites-token: ${{ secrets.ECMWF_SITES_DOCS_DEV_SECTION_TOKEN }}

  # Publish the documentation for the 'develop/main' branches, or on tag creation
  publish:
    if: >-
      ${{ github.event_name == 'push' && (github.ref_name == 'develop' || github.ref_name == 'main'  || github.ref_type == 'tag') }}
    needs: build
    uses: ecmwf/reusable-workflows/.github/workflows/docs-publish.yml@main
    with:
      artifact-id: ${{ needs.build.outputs.artifact-id }}
      space: docs
      name: dev-section
      path: ecflow
      id: ${{ github.ref_name }}
      softlink: ${{ github.ref_name == 'main' && 'stable' || github.ref_name == 'develop' && 'latest' }}
    secrets:
      sites-token: ${{ secrets.ECMWF_SITES_DOCS_DEV_SECTION_TOKEN }}
