##*************************************************************************##
##  CUBE        http://www.scalasca.org/                                   ##
##*************************************************************************##
##  Copyright (c) 2024                                                     ##
##  Forschungszentrum Juelich GmbH, Juelich Supercomputing Centre          ##
##                                                                         ##
##  This software may be modified and distributed under the terms of       ##
##  a BSD-style license.  See the COPYING file in the package base         ##
##  directory for details.                                                 ##
##*************************************************************************##


#############################################################################
# build pdf and html documentation with doxygen
#############################################################################

find_package(Doxygen QUIET)
if (DOXYGEN_FOUND)
  message(STATUS "Doxygen found version: " ${DOXYGEN_VERSION})
  #include(../cmake-modules/UseLATEX.cmake)
  include(../cmake-modules/pdflatex.cmake)

  find_package(LATEX REQUIRED COMPONENTS PDFLATEX)

  #--- variables that will be replaced
  set( abs_top_srcdir ${CMAKE_SOURCE_DIR} )
  set( abs_top_builddir ${CMAKE_BINARY_DIR} )
  set( have_doxygen_latex YES )

  #--- generate common include files (doxygen-common.cfg)
  configure_file( ${CMAKE_SOURCE_DIR}/doc/doxygen-common/doxygen-common.cfg.in
                  ${CMAKE_BINARY_DIR}/doc/doxygen-common/doxygen-common.cfg @ONLY )

  #--- doxygen-common/config.tex.in (not all variables are used)
  set( PACKAGE_MAJOR  ${CUBEGUI_VERSION_MAJOR} )
  set( PACKAGE_MINOR  ${CUBEGUI_VERSION_MINOR} )
  set( PACKAGE_BUGFIX ${CUBEGUI_VERSION_BUGFIX} )
  set( PACKAGE_NAME   "CubeGUI" )
  configure_file( ${CMAKE_SOURCE_DIR}/doc/doxygen-common/config.tex.in
                  ${CMAKE_BINARY_DIR}/doc/doxygen-common/config.tex @ONLY )

  #--- create local destination dir DOC_ROOT to which the generated documents will be copied
  file( MAKE_DIRECTORY ${DOC_ROOT} )

  #--- documentation config
  message(STATUS "Building documentation in " ${CMAKE_CURRENT_BINARY_DIR} )

  add_subdirectory(doxygen-user/guide)
  add_subdirectory(doxygen-user/plugins-guide)
  add_subdirectory(doxygen-dev/cube-plugins)
  add_subdirectory(doxygen-dev/cubegui)

  #--- documentation target
  add_custom_target( documentation ALL
                     COMMENT "Doxygen documentation will be copied to: " ${DOC_ROOT}
                     DEPENDS guide plugins-guide doxygen-dev doxygen-plugins-dev )
else ()
  message(FATAL_ERROR "BUILD_DOC is enabled, but Doxygen cannot be found, which is required to create the documentation.")
endif ()
