cmake_minimum_required(VERSION 3.15)
project(CMakeBook LANGUAGES NONE VERSION 1.0.0)

# Locate Sphinx build tool
find_program(SPHINX_EXECUTABLE
  NAMES sphinx-build
  DOC "Sphinx Documentation Builder (sphinx-doc.org)"
  HINTS
    "[HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\2.7\\InstallPath]/Scripts"
    "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.7\\InstallPath]/Scripts"
  )
if(NOT SPHINX_EXECUTABLE)
  message(FATAL_ERROR "SPHINX_EXECUTABLE (sphinx-build) is not found!")
endif()

option(SPHINX_HTML "Build html help with Sphinx" ON)
option(SPHINX_LATEX "Build latex help with Sphinx" OFF)
option(SPHINX_SINGLEHTML "Build html single page help with Sphinx" OFF)

# Configure Sphinx
set(conf_copyright "Kitware, Inc.")
set(conf_docs "${CMakeBook_SOURCE_DIR}/Help")
set(conf_path "${CMakeBook_SOURCE_DIR}/Utilities/Sphinx")
set(conf_version "${CMakeBook_VERSION_MAJOR}.${CMakeBook_VERSION_MINOR}.${CMakeBook_VERSION_PATCH}")
set(conf_release "${CMakeBook_VERSION}")
set(conf_version_short "${CMakeBook_VERSION_MAJOR}.${CMakeBook_VERSION_MINOR}")
configure_file(Utilities/Sphinx/conf.py.in conf.py @ONLY)

# Add Sphinx generation rules
set(book_formats "")
if(SPHINX_HTML)
  list(APPEND book_formats html)
endif()
if(SPHINX_LATEX)
  list(APPEND book_formats latex)
endif()
if(SPHINX_SINGLEHTML)
  list(APPEND book_formats singlehtml)
endif()
set(book_format_outputs "")
set(book_format_last "")
foreach(format ${book_formats})
  set(book_format_output "book_format_${format}")
  set(book_format_log "build-${format}.log")
  add_custom_command(
    OUTPUT ${book_format_output}
    COMMAND ${SPHINX_EXECUTABLE}
            -c ${CMAKE_CURRENT_BINARY_DIR}
            -d ${CMAKE_CURRENT_BINARY_DIR}/doctrees
            -b ${format}
            ${CMakeBook_SOURCE_DIR}/Help
            ${CMAKE_CURRENT_BINARY_DIR}/${format}
            > ${book_format_log} # log stdout, pass stderr
    DEPENDS ${book_format_last}
    COMMENT "sphinx-build ${format}: see ${book_format_log}"
    VERBATIM
    )
  set_property(SOURCE ${book_format_output} PROPERTY SYMBOLIC 1)
  list(APPEND book_format_outputs ${book_format_output})
  set(book_format_last ${book_format_output})
  if(WIN32)
    set(book_format "${format}")
    configure_file(Utilities/Scripts/build.cmd.in build-${format}.cmd @ONLY)
  endif()
endforeach()

if(WIN32 AND SPHINX_LATEX)
  find_program(PDFLATEX_EXECUTABLE
    NAMES pdflatex
    PATH_SUFFIXES
      "MiKTeX 2.9/miktex/bin"
    )
  if(NOT PDFLATEX_EXECUTABLE)
    message(FATAL_ERROR "pdflatex.exe not found.  "
      "Set PDFLATEX_EXECUTABLE to the location of a pdflatex "
      "with at least these packages installed:
 cmap
 fancybox
 fancyhdr
 fancyvrb
 framed
 mdwtools
 mmap
 mptopdf
 threeparttable
 titlesec
 url
 wrapfig")
  endif()
  get_filename_component(pdflatex_dir "${PDFLATEX_EXECUTABLE}" PATH)
  string(REPLACE "/" "\\" pdflatex_dir "${pdflatex_dir}")
  configure_file(Utilities/Scripts/make-latex.cmd.in make-latex.cmd @ONLY)
endif()

add_custom_target(book ALL DEPENDS ${book_format_outputs})
