# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2019-2025 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

project(bareos-docs NONE)

set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM 1)

if(${docs-build-json})
  # create the list of *.sql files for the pkg-plist.database-postgresql
  file(
    GLOB_RECURSE sqlfiles
    RELATIVE ${CMAKE_SOURCE_DIR}/core/src/cats
    ${CMAKE_SOURCE_DIR}/core/src/cats/ddl/*.sql
  )
  list(TRANSFORM sqlfiles PREPEND lib/bareos/scripts/)
  list(SORT sqlfiles)
  string(REGEX REPLACE ";" "\n" sqlfiles "${sqlfiles}")
  list(APPEND sqlfiles "\n")
  file(
    WRITE
    "${CMAKE_SOURCE_DIR}/core/platforms/freebsd/bareos-freebsd/bareos.com-common/pkg-plist.database-postgresql"
    ${sqlfiles}
  )

  add_custom_target(
    check-freebsd-pkglist
    COMMAND
      git diff -p --exit-code --stat=80
      ${CMAKE_SOURCE_DIR}/core/platforms/freebsd/bareos-freebsd/bareos.com-common
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    COMMENT
      "Comparing pkg-plist files in \"${CMAKE_SOURCE_DIR}/core/platforms/freebsd/bareos-freebsd/bareos.com-common\" with git repo"
  )

  # commands building json files ######
  add_custom_command(
    OUTPUT
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-dir-config-schema.json
    COMMAND bareos-dir --xs >/dev/null
    COMMAND
      bareos-dir --xs
      >${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-dir-config-schema.json
    COMMAND
      sed --in-place --expression="/\\\"version\\\":/d"
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-dir-config-schema.json
    DEPENDS bareos-dir
  )

  add_custom_command(
    OUTPUT
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-sd-config-schema.json
    COMMAND bareos-sd --xs >/dev/null
    COMMAND
      bareos-sd --xs
      >${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-sd-config-schema.json
    COMMAND
      sed --in-place --expression="/\\\"version\\\":/d"
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-sd-config-schema.json
    DEPENDS bareos-sd
  )

  add_custom_command(
    OUTPUT
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-fd-config-schema.json
    COMMAND bareos-fd --xs >/dev/null
    COMMAND
      bareos-fd --xs
      >${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-fd-config-schema.json
    COMMAND
      sed --in-place --expression="/\\\"version\\\":/d"
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-fd-config-schema.json
    DEPENDS bareos-fd
  )

  add_custom_command(
    OUTPUT
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
    COMMAND bconsole --xs >/dev/null
    COMMAND
      bconsole --xs
      >${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
    COMMAND
      sed --in-place --expression="/\\\"version\\\":/d"
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
    DEPENDS bconsole
  )

  add_custom_command(
    OUTPUT
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-tray-monitor-config-schema.json
    COMMAND bareos-tray-monitor --xs >/dev/null
    COMMAND
      bareos-tray-monitor --xs
      >${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-tray-monitor-config-schema.json
    COMMAND
      sed --in-place --expression="/\\\"version\\\":/d"
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-tray-monitor-config-schema.json
    DEPENDS bareos-tray-monitor
  )

  set(AUTOGENERATED_RESOURCE_DESCRIPTION_JSON_FILES
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-dir-config-schema.json
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-sd-config-schema.json
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-fd-config-schema.json
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
      ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-tray-monitor-config-schema.json
  )

  #
  # commands building usage files ######
  #

  set(AUTOGENERATE_DIR ${PROJECT_SOURCE_DIR}/source/include/autogenerated/)
  set(USAGE_DIR ${AUTOGENERATE_DIR}/usage/)

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bareos-dir.txt
    COMMAND bareos-dir --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bareos-dir>
            ${USAGE_DIR}
    DEPENDS bareos-dir
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bareos-sd.txt
    COMMAND bareos-sd --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bareos-sd>
            ${USAGE_DIR}
    DEPENDS bareos-sd
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bareos-fd.txt
    COMMAND bareos-fd --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bareos-fd>
            ${USAGE_DIR}
    DEPENDS bareos-fd
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bconsole.txt
    COMMAND bconsole --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bconsole>
            ${USAGE_DIR}
    DEPENDS bconsole
  )

  # add_custom_command( OUTPUT ${USAGE_DIR}/bareos-tray-monitor.txt COMMAND
  # bareos-tray-monitor --help >/dev/null COMMAND
  # ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh
  # $<TARGET_FILE:bareos-tray-monitor> #{USAGE_DIR} DEPENDS
  # $<$<BOOL:${docs-build-json}>:bareos-tray-monitor> )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bareos-dbcheck.txt
    COMMAND bareos-dbcheck --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh
            $<TARGET_FILE:bareos-dbcheck> ${USAGE_DIR}
    DEPENDS bareos-dbcheck
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bcopy.txt
    COMMAND bcopy --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bcopy>
            ${USAGE_DIR}
    DEPENDS bcopy
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bextract.txt
    COMMAND bextract --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bextract>
            ${USAGE_DIR}
    DEPENDS bextract
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bls.txt
    COMMAND bls --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bls>
            ${USAGE_DIR}
    DEPENDS bls
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bpluginfo.txt
    COMMAND bpluginfo --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bpluginfo>
            ${USAGE_DIR}
    DEPENDS bpluginfo
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bregex.txt
    COMMAND bregex --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bregex>
            ${USAGE_DIR}
    DEPENDS bregex
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bscan.txt
    COMMAND bscan --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bscan>
            ${USAGE_DIR}
    DEPENDS bscan
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bsmtp.txt
    COMMAND bsmtp --help > /dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bsmtp>
            ${USAGE_DIR}
    DEPENDS bsmtp
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/btape.txt
    COMMAND btape --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:btape>
            ${USAGE_DIR}
    DEPENDS btape
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bwild.txt
    COMMAND bwild --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh $<TARGET_FILE:bwild>
            ${USAGE_DIR}
    DEPENDS bwild
  )

  add_custom_command(
    OUTPUT ${USAGE_DIR}/bdedupestimate.txt
    COMMAND bdedupestimate --help >/dev/null
    COMMAND ${PROJECT_SOURCE_DIR}/scripts/get-usage.sh
            $<TARGET_FILE:bdedupestimate> ${USAGE_DIR}
    DEPENDS bdedupestimate
  )

  set(AUTOGENERATED_USAGE_FILES
      ${USAGE_DIR}/bareos-dir.txt
      ${USAGE_DIR}/bareos-sd.txt
      ${USAGE_DIR}/bareos-fd.txt
      ${USAGE_DIR}/bconsole.txt
      ${USAGE_DIR}/bareos-dbcheck.txt
      ${USAGE_DIR}/bcopy.txt
      ${USAGE_DIR}/bextract.txt
      ${USAGE_DIR}/bls.txt
      ${USAGE_DIR}/bpluginfo.txt
      ${USAGE_DIR}/bregex.txt
      ${USAGE_DIR}/bscan.txt
      ${USAGE_DIR}/bsmtp.txt
      ${USAGE_DIR}/btape.txt
      ${USAGE_DIR}/bwild.txt
      ${USAGE_DIR}/bdedupestimate.txt
  )

  set(CLEAN_JSON_FILES
      rm -f ${PROJECT_SOURCE_DIR}/source/include/autogenerated/*.json
  )

endif() # if(${docs-build-json})

# target: clean-docs ######
add_custom_target(
  clean-docs
  COMMAND ${CLEAN_JSON_FILES}
  COMMAND rm -f ${PROJECT_SOURCE_DIR}/source/include/autogenerated/*.inc
  COMMAND rm -rf ${PROJECT_SOURCE_DIR}/source/include/autogenerated/autosummary/
  COMMAND rm -rf ${PROJECT_BINARY_DIR}/BareosMainReference
)

# target: check-git ###### To prevent documentation to be built out of sync with
# the actual binaries, this target checks if the generated json files differ
# from the ones in the repository.

add_custom_target(
  check-docs-git
  COMMAND git diff -p --exit-code --stat=80
          ${PROJECT_SOURCE_DIR}/source/include/autogenerated
  DEPENDS ${AUTOGENERATED_RESOURCE_DESCRIPTION_JSON_FILES}
          ${AUTOGENERATED_USAGE_FILES}
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  COMMENT
    "Comparing json files in \"${PROJECT_SOURCE_DIR}/source/include/autogenerated\" with git repo"
)
if(TARGET check-freebsd-pkglist)
  add_dependencies(check-docs-git check-freebsd-pkglist)
endif()
add_dependencies(check-git check-docs-git)

set(SPHINX_OPTS $ENV{SPHINX_OPTS})
set(SPHINX_COMMAND sphinx-build)
set(SPHINX_BUILDDIR ${PROJECT_BINARY_DIR}/BareosMainReference)
set(SPHINX_SOURCE_DIR ${PROJECT_SOURCE_DIR}/source)

if(${docs-only})
  set(DOCS_ALL ALL) # this enables "make docs" by just typing "make"
endif()

add_custom_target(
  docs-check-urls
  COMMAND ${SPHINX_COMMAND} -M linkcheck "${SPHINX_SOURCE_DIR}"
          "${SPHINX_BUILDDIR}"
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  COMMENT "Check if the URLs used in the documentation are still valid targets."
)

# target: docs ######
add_custom_target(
  docs
  ${DOCS_ALL}
  COMMAND ${SPHINX_COMMAND} -M html "${SPHINX_SOURCE_DIR}" "${SPHINX_BUILDDIR}"
          "${SPHINX_OPTS}"
  COMMAND ${SPHINX_COMMAND} -b man "${SPHINX_SOURCE_DIR}" "${SPHINX_BUILDDIR}"
          "${SPHINX_OPTS}"
  DEPENDS $<$<BOOL:${docs-build-json}>:clean-docs>
  DEPENDS check-git
)
