# Copyright(C) 2023  Sutou Kouhei <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

include(GroongaSphinx)
include(${CMAKE_CURRENT_SOURCE_DIR}/files.cmake)

set(GRN_DOC_PRE_BUILD_STAMP "${CMAKE_CURRENT_SOURCE_DIR}/build-stamp")
set(GRN_DOC_LOCALES en ja)
foreach(LOCALE ${GRN_DOC_LOCALES})
  set(GRN_DOC_HTML_FILES_LOCALE)
  foreach(file ${GRN_DOC_HTML_FILES})
    list(APPEND GRN_DOC_HTML_FILES_LOCALE
         ${CMAKE_CURRENT_BINARY_DIR}/${LOCALE}/html/${file})
  endforeach()
  if(EXISTS "${GRN_DOC_PRE_BUILD_STAMP}")
    install(
      DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/locale/${LOCALE}/html/
      COMPONENT Document
      DESTINATION ${CMAKE_INSTALL_DOCDIR}/${LOCALE}
      PATTERN ".buildinfo" EXCLUDE)
  else()
    grn_sphinx(${CMAKE_CURRENT_SOURCE_DIR}/source ${LOCALE}
               "${GRN_DOC_SOURCES}" "${GRN_DOC_HTML_FILES_LOCALE}")
    install(
      DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${LOCALE}/html/
      COMPONENT Document
      DESTINATION ${CMAKE_INSTALL_DOCDIR}/${LOCALE}
      PATTERN ".buildinfo" EXCLUDE)
  endif()
endforeach()

# TODO: When building packages in `packages/`, the documentation generated by Doxygen should be included.
if(NOT EXISTS "${GRN_DOC_PRE_BUILD_STAMP}")
  find_program(DOXYGEN doxygen REQUIRED)
  find_path(DOXYGEN_AWESOME_CSS_DIR doxygen-awesome.css
            HINTS /usr/share/doxygen-awesome-css)
  if(DOXYGEN_AWESOME_CSS_DIR)
    message(STATUS "Found doxygen-awesome-css: ${DOXYGEN_AWESOME_CSS_DIR}")
    set(DOXYGEN_HTML_HEADER "doc/header.html")
    set(DOXYGEN_HTML_EXTRA_STYLESHEET
        "${DOXYGEN_AWESOME_CSS_DIR}/doxygen-awesome.css")
    set(DOXYGEN_HTML_EXTRA_FILES
        "${DOXYGEN_AWESOME_CSS_DIR}/doxygen-awesome-darkmode-toggle.js \
         ${DOXYGEN_AWESOME_CSS_DIR}/doxygen-awesome-fragment-copy-button.js \
         ${DOXYGEN_AWESOME_CSS_DIR}/doxygen-awesome-interactive-toc.js \
         ${DOXYGEN_AWESOME_CSS_DIR}/doxygen-awesome-paragraph-link.js")
  else()
    message(STATUS "doxygen-awesome-css not found, using default Doxygen theme")
    set(DOXYGEN_HTML_HEADER "")
    set(DOXYGEN_HTML_EXTRA_STYLESHEET "")
    set(DOXYGEN_HTML_EXTRA_FILES "")
  endif()
  set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/en/html/reference)
  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in
                 ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  # Since `${DOXYGEN_OUTPUT_DIRECTORY}` is created by `doc_en_html`,
  # this target is executed after that.
  add_custom_target(
    doc_c_api ALL
    COMMAND ${DOXYGEN} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..
    DEPENDS doc_en_html
    VERBATIM)
endif()

add_custom_target(
  doc_update_files
  COMMAND ${RUBY} ${CMAKE_CURRENT_SOURCE_DIR}/update-files.rb
  DEPENDS doc_en_html)

add_custom_target(
  doc_update_examples
  COMMAND
    ${CMAKE_COMMAND} -E env
    GRN_PLUGINS_DIR=${CMAKE_CURRENT_SOURCE_DIR}/../plugins
    GRN_PLUGINS_PATH=${CMAKE_CURRENT_BINARY_DIR}/../plugins
    GRN_RUBY_SCRIPTS_DIR=${CMAKE_CURRENT_SOURCE_DIR}/../lib/mrb/scripts
    LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/../lib:$ENV{LD_LIBRARY_PATH}
    PATH=${CMAKE_CURRENT_BINARY_DIR}/../src:${CMAKE_CURRENT_BINARY_DIR}/../src/suggest:$ENV{PATH}
    ${RUBY} ${CMAKE_CURRENT_SOURCE_DIR}/update-execution-example.rb
  DEPENDS groonga groonga-suggest-create-dataset groonga-suggest-httpd
          groonga-suggest-learner
  VERBATIM)

set(GRN_DOC_DEPLOY_DEPENDS)
foreach(LOCALE ${GRN_DOC_LOCALES})
  list(APPEND GRN_DOC_DEPLOY_DEPENDS doc_${LOCALE}_html)
endforeach()
add_custom_target(
  doc_deploy
  COMMAND ${RUBY} ${CMAKE_CURRENT_SOURCE_DIR}/deploy.rb
  DEPENDS ${GRN_DOC_DEPLOY_DEPENDS}
  VERBATIM)
