##*************************************************************************##
##  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.                                                 ##
##*************************************************************************##


#--- generate Doxyfiles from templates ------------------------------------------------------

# TODO: doxygen-cube-plugins-dev.cfg.in in common makes no sense
# => merge with doxygen-cube-plugins-dev.cfg.in and remove from common
file(READ ${CMAKE_SOURCE_DIR}/doc/doxygen-common/doxygen-cube-plugins-dev.cfg.in PART1)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/doxygen-cube-plugins-dev.cfg.in PART2)
set(CONTENT "${PART1}${PART2}")
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.in "${CONTENT}")

set( DOXYFILE ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile )
configure_file(${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.in ${DOXYFILE} @ONLY)

#--------------------------------------------------------------------------------------------

# remove path, which is set in Doxyfile (used by autoconf)
file(READ ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile CONTENT)
string(REGEX REPLACE "HTML_OUTPUT[^\n]*/html" "" CONTENT "${CONTENT}")
string(REGEX REPLACE "@INCLUDE =[^=]*doxygen-cube-plugins-dev.cfg" "" CONTENT "${CONTENT}")
string(REGEX REPLACE "@INCLUDE =[^=]*time.cfg" "" CONTENT "${CONTENT}")
string(REGEX REPLACE "TAGFILES[^\n]*html" "" CONTENT "${CONTENT}") # TAGFILES refers to itsself -> delete
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile "${CONTENT}")

#--- generate Doxyfiles from templates ------------------------------------------------------

add_custom_command(OUTPUT ./html/index.html
         COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYFILE} > doxygen.log 2>&1
         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
         COMMENT "Generating API documentation with Doxygen (see ${CMAKE_CURRENT_BINARY_DIR}/doxygen.log)"
         DEPENDS ${DOXYFILE} cube-plugins.dox
)

add_custom_target( doxygen-plugins-dev DEPENDS ./html/index.html ) # create doxygen files
