# Distributed under the MIT License.
# See LICENSE.txt for details.

find_package(Doxygen)
if (DOXYGEN_FOUND)
  set(SPECTRE_DOXYGEN_GROUPS "${CMAKE_BINARY_DIR}/docs/tmp/GroupDefs.hpp")

  set(SPECTRE_DOX_GENERATE_HTML "YES")
  set(SPECTRE_DOX_GENERATE_XML "NO")
  configure_file(
      docs/Doxyfile.in
      ${PROJECT_BINARY_DIR}/docs/DoxyfileHtml @ONLY IMMEDIATE
  )
  # Configure file that contains doxygen groups
  configure_file(docs/GroupDefs.hpp ${SPECTRE_DOXYGEN_GROUPS})

  add_custom_target(
      doc
      COMMAND ${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/docs/DoxyfileHtml 2>&1
      DEPENDS
      ${PROJECT_BINARY_DIR}/docs/DoxyfileHtml
      ${SPECTRE_DOXYGEN_GROUPS}
      )

  add_custom_target(
    doc-check
    # search for warnings and print out 6 lines after to make sure
    # majority of warning is printed so the results are actually
    # useful.
    COMMAND ! ${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/docs/DoxyfileHtml 2>&1
    | grep -A 6 'warning'
    DEPENDS
    ${PROJECT_BINARY_DIR}/docs/DoxyfileHtml
    ${SPECTRE_DOXYGEN_GROUPS}
    )

  set(SPECTRE_DOX_GENERATE_HTML "NO")
  set(SPECTRE_DOX_GENERATE_XML "YES")
  configure_file(
      docs/Doxyfile.in
      ${PROJECT_BINARY_DIR}/docs/DoxyfileXml @ONLY IMMEDIATE
  )

  add_custom_target(
      doc-xml
      COMMAND ${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/docs/DoxyfileXml
      DEPENDS
      ${PROJECT_BINARY_DIR}/docs/DoxyfileXml
      ${SPECTRE_DOXYGEN_GROUPS}
  )

  find_package(PythonInterp)
  find_program(LCOV lcov)
  find_program(GENHTML genhtml)
  find_program(SED sed)

  if (PYTHONINTERP_FOUND AND LCOV AND GENHTML AND SED)
    include(FindPythonModule)
    find_python_module(coverxygen FALSE)
    # Use [coverxygen](https://github.com/psycofdj/coverxygen) to check
    # the level of documentation coverage.
    if(NOT "${PY_COVERXYGEN}" STREQUAL "")
      set(DOX_COVERAGE_OUTPUT "${CMAKE_BINARY_DIR}/docs/html/doc_coverage/")
      add_custom_target(
          doc-coverage

          COMMAND ${PYTHON_EXECUTABLE}
          -m coverxygen
          --xml-dir ${CMAKE_BINARY_DIR}/docs/xml
          --src-dir ${CMAKE_SOURCE_DIR}
          --output ${CMAKE_BINARY_DIR}/docs/tmp/doc_coverage.info

          COMMAND ${LCOV}
          --remove ${CMAKE_BINARY_DIR}/docs/tmp/doc_coverage.info
          '${CMAKE_SOURCE_DIR}/src/Executables/*'
          '${CMAKE_SOURCE_DIR}/tests/*'
          --output ${CMAKE_BINARY_DIR}/docs/tmp/doc_coverage.info

          COMMAND
          ${LCOV} --summary ${CMAKE_BINARY_DIR}/docs/tmp/doc_coverage.info

          COMMAND
          ${GENHTML} --legend
          --no-function-coverage
          --no-branch-coverage
          --title `cd ${CMAKE_SOURCE_DIR} && git rev-parse HEAD`
          ${CMAKE_BINARY_DIR}/docs/tmp/doc_coverage.info
          -o ${DOX_COVERAGE_OUTPUT}

          COMMAND
          find ${DOX_COVERAGE_OUTPUT} -type f -print
          | xargs file
          | grep text
          | cut -f1 -d:
          | xargs ${SED} -i'.bak' 's/LCOV - code coverage report/
          SpECTRE Documentation Coverage Report/g'

          COMMAND find ${DOX_COVERAGE_OUTPUT} -type f -print | xargs file
          | grep text
          | cut -f1 -d:
          | xargs ${SED} -i'.bak' 's^<td class="headerItem">Test:</td>^
          <td class="headerItem">Commit:</td>^g'

          COMMAND find ${DOX_COVERAGE_OUTPUT} -type f -print | xargs file
          | grep text
          | cut -f1 -d:
          | xargs ${SED} -i'.bak' 's^<td class="headerValue">\\\([a-z0-9]\\{40\\}\\\)^
          <td class="headerValue"><a target="_blank"
          href="https://github.com/sxs-collaboration/spectre/commit/\\1">\\1</a>^g'

          # Delete backup files created by sed
          COMMAND find ${DOX_COVERAGE_OUTPUT} -type f -name \"*.bak\" -print
          | xargs file | grep text | cut -f1 -d: | xargs rm

          DEPENDS
          ${PROJECT_BINARY_DIR}/docs/DoxyfileXml
          ${SPECTRE_DOXYGEN_GROUPS}
          doc-xml

          # Set work directory for target
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}

          COMMENT "SpECTRE Documentation Coverage"
      )
    endif(NOT "${PY_COVERXYGEN}" STREQUAL "")
  endif(PYTHONINTERP_FOUND AND LCOV AND GENHTML AND SED)
else(DOXYGEN_FOUND)
  message(WARNING "Doxygen is needed to build the documentation.")
endif (DOXYGEN_FOUND)
