# Copy all online help files from current directory over to the binary directory.
# These do not include any server manager documentation htmls.
file(GLOB CMB_DOCUMENTATION_HTML_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}/[^.]*.html)

file(GLOB CMB_DOCUMENTATION_PNG_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}/[^.]*.png)

list(APPEND CMB_DOCUMENTATION_FILES ${CMB_DOCUMENTATION_HTML_FILES} ${CMB_DOCUMENTATION_PNG_FILES})

foreach (file ${CMB_DOCUMENTATION_FILES})
  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/${file}"
    "${CMAKE_CURRENT_BINARY_DIR}/${file}"
    COPYONLY)
endforeach (file)

configure_file(
  "${CMAKE_CURRENT_SOURCE_DIR}/CMBSuiteDoc.css"
  "${CMAKE_CURRENT_BINARY_DIR}/CMBSuiteDoc.css"
  COPYONLY)

configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/index.html.in"
    "${CMAKE_CURRENT_BINARY_DIR}/index.html"
    @ONLY)

configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/about.html.in"
    "${CMAKE_CURRENT_BINARY_DIR}/about.html"
    @ONLY)

set(qhp_dependencies
  "${CMAKE_CURRENT_BINARY_DIR}/about.html"
  "${CMAKE_CURRENT_BINARY_DIR}/index.html")

set(qhp_dependencies ${qhp_dependencies}
    ${CMB_DOCUMENTATION_HTML_FILES})

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qhp
  COMMAND ${CMAKE_COMMAND}
    ARGS
      -D DOCUMENTATION_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}
      -D INPUT:PATH=${CMAKE_CURRENT_SOURCE_DIR}/cmbsuite.qhp.in
      -D OUTPUT:PATH=${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qhp
      -P ${CMAKE_CURRENT_SOURCE_DIR}/GenerateQHP.cmake

  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/cmbsuite.qhp.in
          ${CMAKE_CURRENT_SOURCE_DIR}/GenerateQHP.cmake
          ${qhp_dependencies}
  COMMENT "Generating cmbsuite.qhp"
)

add_custom_target(GenerateCMBSuiteQHP
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qhp)

find_program(QT_HELP_GENERATOR
  qhelpgenerator
  PATHS "${QT_BINARY_DIR}"
  DOC "qhelpgenerator used to compile Qt help project files")


#simulate touching the cmbsuite.qch file at configure time
#this is to work around current issues in the ninja gnerator.
#Ninja is currently unable to detect that cmbsuite.qch is
#generated by a build time event when it gets to creating
#the applications in different directories
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmbsuite.qch"
    "${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qch"
    COPYONLY)

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qch
  COMMAND ${QT_HELP_GENERATOR}
  ARGS ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qhp
      -o ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qch
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qhp
  COMMENT "Compiling Qt help project cmbsuite.qhp"
)

add_custom_target(CMBSuiteHelp DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qhp  ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qch)

set_source_files_properties(  ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qhp PROPERTIES GENERATED TRUE )
set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/cmbsuite.qch PROPERTIES GENERATED TRUE )

add_dependencies(CMBSuiteHelp GenerateCMBSuiteQHP)

#INSTALL RULES FOR README FILES
set(CmbApps PointsBuilder SceneBuilder ModelBuilder MeshViewer GeologyBuilder ProjectManager)
foreach (appName ${CmbApps})
  set(readmeName ${appName}_README.html)
  if(NOT APPLE)
    install(FILES ${readmeName} DESTINATION ${VTK_INSTALL_RUNTIME_DIR})
  else()
    install(FILES ${readmeName} DESTINATION "${VTK_INSTALL_RUNTIME_DIR}/${appName}.app/")
  endif(NOT APPLE)
endforeach()
