# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2019-2020 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.

cmake_minimum_required(VERSION 3.0)

project(bareos-docs NONE)

# 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 $<$<BOOL:${docs-build-json}>: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 $<$<BOOL:${docs-build-json}>: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 $<$<BOOL:${docs-build-json}>: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 $<$<BOOL:${docs-build-json}>: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 $<$<BOOL:${docs-build-json}>: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
)

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

# 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_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-git
  COMMAND git diff -p --exit-code --stat=80
          ${PROJECT_SOURCE_DIR}/source/include/autogenerated
  DEPENDS ${AUTOGENERATED_RESOURCE_DESCRIPTION_JSON_FILES}
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  COMMENT
    "Comparing json files in \"${PROJECT_SOURCE_DIR}/source/include/autogenerated\" with git repo"
)

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()

# 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
)
