#
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
# Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

cmake_minimum_required(VERSION 3.13)
find_package(Doxygen)

if (DOXYGEN_FOUND)
    string(REPLACE ";" " " DOXYGEN_INPUT
        "${libcaosdb_INCL} ${PROJECT_INCLUDE_DIR}/ccaosdb.h")
    configure_file(Doxyfile.in Doxyfile)


    # Note: do not put "ALL" - this builds docs together with application EVERY TIME!
    add_custom_target(doc-doxygen
        COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Generating API documentation with Doxygen"
        VERBATIM )

    find_program(SPHINX_CMD sphinx-build)
    if (SPHINX_CMD)
        configure_file(conf.py.in conf.py)
        configure_file(index.rst.in index.rst)

        # create C++ docs
        configure_file(cppapi/index.rst.in cppapi/index.rst)

        # create rst pages for every header file
        list(LENGTH libcaosdb_INCL len_header_files)
        math(EXPR len_header_files "${len_header_files} - 1")
        foreach (i RANGE "${len_header_files}")
            list(GET libcaosdb_INCL ${i} HEADER_FILE)
            string(REPLACE
                "${PROJECT_INCLUDE_DIR}/"
                ""
                HEADER_FILE_NAME
                ${HEADER_FILE})
            string(REPLACE
                "${CMAKE_BINARY_DIR}/include/"
                ""
                HEADER_FILE_NAME
                ${HEADER_FILE_NAME})
            string(REPLACE
                "caosdb/"
                ""
                DOC_FILE_NAME
                ${HEADER_FILE_NAME})
            configure_file(
                header_file.rst.in
                cppapi/_${DOC_FILE_NAME}.rst)
        endforeach ()

        # create (plain) C docs
        configure_file(capi/index.rst.in capi/index.rst)
        set(HEADER_FILE_NAME ccaosdb.h)
        set(HEADER_FILE ccaosdb.h)
        configure_file(
            header_file.rst.in
            capi/_ccaosdb.rst)

        add_custom_target(doc-sphinx
            COMMAND ${SPHINX_CMD}
                -b html
                -n
                -c ${CMAKE_CURRENT_BINARY_DIR}
                ${CMAKE_CURRENT_BINARY_DIR}
                sphinx_out
                DEPENDS doc-doxygen
                Examples.rst
                Install_develop.rst
                FEATURES.md
                CHANGELOG.md
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMENT "Generating API documentation with Sphinx"
            VERBATIM
            )
        # Copying files is necessary: https://stackoverflow.com/a/45808534/232888
        file(COPY
            Examples.rst
            Install_develop.rst
            DEPENDENCIES.md
            CHANGELOG.md
            FEATURES.md
            # Tutorial.rst
            DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
    else ()
        message("Sphinx need to be installed to generate the sphinx documentation")
    endif ()

else ()
  message("Doxygen need to be installed to generate the doxygen documentation")
endif (DOXYGEN_FOUND)
