# Copyright (C) 2022 - 2024 by the authors of the ASPECT code.
#
# This file is part of ASPECT.
#
# ASPECT is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# ASPECT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ASPECT; see the file doc/COPYING.  If not see
# <http://www.gnu.org/licenses/>.

CMAKE_MINIMUM_REQUIRED(VERSION 3.13.4)

FIND_PACKAGE(Perl)


####################################################
# Generate input files for the manual
####################################################

# Step 1: Find all of the .prm files that will be used in
#         the manual.
message(STATUS "Finding .prm files")

FILE(GLOB _doc_directories_cookbooks
     ${CMAKE_SOURCE_DIR}/cookbooks/*/doc)
FILE(GLOB _doc_directories_benchmarks
     ${CMAKE_SOURCE_DIR}/benchmarks/*/doc)
SET(_doc_directories
    ${CMAKE_SOURCE_DIR}/doc/manual
    ${CMAKE_SOURCE_DIR}/doc/logo)

LIST(APPEND _doc_directories
    ${_doc_directories_cookbooks}
    ${_doc_directories_benchmarks})


# Step 2: Deal with images
message(STATUS "Finding image files")
ADD_CUSTOM_TARGET(build_images)

#         We have one image file that we need to create -- the plugin
#         graph. This happens by calling the 'neato' program with
#         the plugin graph input file generated by ASPECT.
#
#         Right now, this file is put into the repository, so we don't
#         need to generate it. As a consequence, just add the rule
#         whenever actually necessary. (If the file is found, then it's
#         already copied into the build directory in step 5 above.)
IF (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/manual/plugin_graph.svg)
  ADD_CUSTOM_COMMAND(
    OUTPUT
      ${CMAKE_CURRENT_BINARY_DIR}/doc/manual/plugin_graph.svg
    COMMAND
      neato
        ${CMAKE_CURRENT_SOURCE_DIR}/manual/plugin_graph.dot
        -Tsvg
        -o ${CMAKE_CURRENT_BINARY_DIR}/doc/manual/plugin_graph.svg
    DEPENDS
      ${CMAKE_CURRENT_SOURCE_DIR}/manual/plugin_graph.dot
    COMMENT
      "Generating the plugin graph image."
  )

  ADD_CUSTOM_TARGET(build_plugin_graph_svg
    DEPENDS
      ${CMAKE_CURRENT_BINARY_DIR}/doc/manual/plugin_graph.svg
  )

  # Add the step above as a dependency -- somewhat randomly -- to the
  # image generation target above. This way we don't have to do another
  # IF(NOT EXISTS...) when adding dependencies to the larger targets below.
  ADD_DEPENDENCIES(build_images
                   build_plugin_graph_svg)
ENDIF()



################################################################
#
# The final target: Build it all
#
################################################################
message(STATUS "Setting up build information")
ADD_CUSTOM_TARGET(documentation
  DEPENDS
    build_images
)
