# Build Sphinx documentation

# Function for processing reStructuredText files
if (SPHINX_FOUND)
  function(build_sphinx_docs sphinxTargetName)
    set(options)
    set(oneValueArgs DESTINATION SOURCE_DIR BUILD_DIR)
    set(multiValueArgs DEPENDS FIGURES)
    cmake_parse_arguments(sphinx "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
    if (NOT sphinx_SOURCE_DIR)
      set(sphinx_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") # Reasonable default
    endif()
    if (NOT sphinx_BUILD_DIR)
      set(sphinx_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}") # Reasonable default
    endif()
    # Generate HTML version of docs
    set(sphinx_HTML_TOP "${sphinx_BUILD_DIR}/html/index.html")
    add_custom_command(
      OUTPUT "${sphinx_HTML_TOP}"
      DEPENDS
        ${CMAKE_CURRENT_SOURCE_DIR}/conf.py
        ${sphinx_DEPENDS}
        ${figureList}
        COMMAND ${SPHINX_EXECUTABLE}
      ARGS
        -Q
        -b html
        "${sphinx_SOURCE_DIR}"
        "${sphinx_BUILD_DIR}/html"
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
      COMMENT "Generating HTML for ${sphinxTargetName}"
    )
    if (SMTK_BUILD_DOCUMENTATION STREQUAL "always")
      add_custom_target(doc-${sphinxTargetName} ALL DEPENDS "${sphinx_HTML_TOP}")
    else() # must be "manual"
      add_custom_target(doc-${sphinxTargetName} DEPENDS "${sphinx_HTML_TOP}")
    endif()
    message("Added \"doc-${sphinxTargetName}\" target to generate html documentation from Sphinx source")

    if (sphinx_DESTINATION)
      install(
        DIRECTORY "${sphinx_BUILD_DIR}/html/"
        DESTINATION "${sphinx_DESTINATION}"
        COMPONENT Development)
      install(
        FILES ${figureList}
        DESTINATION "${sphinx_DESTINATION}/figures"
        COMPONENT Development)
    endif()
  endfunction()

  set(sphinx_source
    conf.py
    index.rst
    splashImage_small.png

    getting-started/example-omega3p-close.rst
    getting-started/example-omega3p-create.rst
    getting-started/example-omega3p-download.rst
    getting-started/example-omega3p-specify.rst
    getting-started/example-omega3p-submit.rst
    getting-started/example-omega3p-track.rst
    getting-started/example-omega3p-view.rst
    getting-started/example-omega3p.rst
    getting-started/index.rst
    getting-started/installing.rst

    getting-started/images/ace3p-menu.png
    getting-started/images/download-page.png
    getting-started/images/download-toolbutton-highlight.png
    getting-started/images/modelbuilder-start.png
    getting-started/images/nersc-login.png
    getting-started/images/new-project-dialog.png
    getting-started/images/paraview-toolbutton.png
    getting-started/images/project-bcs.png
    getting-started/images/project-created.png
    getting-started/images/project-export.png
    getting-started/images/project-jobs1.png
    getting-started/images/project-jobs2.png
    getting-started/images/results-view.png
    getting-started/images/second-renderview.png
    getting-started/images/slactools-first.png

    _static/css/getting-started.css
  )

  # Add the top-level reStructuredText file.
  # All others are included by it.
  build_sphinx_docs(user
    DEPENDS ${GETTING_STARTED_DOCS}
    DESTINATION share/doc/getting-started
  )
endif()
