find_program(graphviz_exec dot)

if (QT_SRC_DIR)

if (NOT ${graphviz_exec} STREQUAL graphviz_exec-NOTFOUND)
add_custom_target(apidoc
                  #temporarily create a symlink for the generated .rst files inside ../doc
                  #this allows Sphinx to see them, and output goes to the correct dir
                  COMMAND ${CMAKE_COMMAND} -E create_symlink "${CMAKE_BINARY_DIR}/PySide"  "${CMAKE_CURRENT_SOURCE_DIR}/PySide"
                  COMMAND make html BUILDDIR=${CMAKE_BINARY_DIR}/apidoc WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                  COMMAND ${CMAKE_COMMAND} -E remove -f "${CMAKE_CURRENT_SOURCE_DIR}/PySide")


# copy pyside.qdocconf to qt source dir.
configure_file("pyside.qdocconf" "${QT_SRC_DIR}/tools/qdoc3/test/pyside.qdocconf" @ONLY)
set(DOC_DATA_DIR "${CMAKE_CURRENT_BINARY_DIR}/qt4xmldoc")
set(ENV{QTDIR} ${QT_SRC_DIR} )

add_custom_target(qdoc3
                  COMMAND QTDIR=${QT_SRC_DIR} qdoc3 pyside.qdocconf
                  WORKING_DIRECTORY "${QT_SRC_DIR}/tools/qdoc3/test"
                  COMMENT "Running qdoc3 against Qt source code..."
                  SOURCE "pyside.qdocconf")

macro(create_doc module)
string(REGEX REPLACE "^Qt" "" module_name ${module})
string(TOLOWER ${module_name} module_name)
add_custom_target("${module}-apidoc"
COMMAND ${GENERATOR} --generatorSet=qtdoc
        ${CMAKE_BINARY_DIR}/data/global.h
        --include-paths=${QT_INCLUDE_DIR}:${QT_QTCORE_INCLUDE_DIR}:${QT_PHONON_INCLUDE_DIR}
        --typesystem-paths=${typesystem_SOURCE_DIR}
        ${typesystem_SOURCE_DIR}/typesystem_${module_name}.xml
        --library-source-dir=${QT_SRC_DIR}
        --documentation-only
        --documentation-data-dir=${DOC_DATA_DIR}
        --documentation-out-dir=${CMAKE_BINARY_DIR}
        --documentation-code-snippets-dir=${CMAKE_CURRENT_SOURCE_DIR}/codesnippets
WORKING_DIRECTORY ${${module}_SOURCE_DIR}

COMMENT "Running generator to generate documentation of ${module}..."
)
add_dependencies("${module}-apidoc" qdoc3)
add_dependencies(apidoc "${module}-apidoc")
endmacro(create_doc module)

create_doc(QtCore)
create_doc(QtGui)
create_doc(QtHelp)
create_doc(QtNetwork)
create_doc(QtOpengl)
create_doc(QtScript)
create_doc(QtScripttools)
create_doc(QtSql)
create_doc(QtSvg)
create_doc(QtUitools)
create_doc(QtXml)
create_doc(QtWebkit)
create_doc(QtMultimedia)
create_doc(phonon)

#create devhelp file
add_custom_target(apidevhelp
    COMMAND python;${CMAKE_CURRENT_SOURCE_DIR}/pyhtml2devhelp.py;${CMAKE_BINARY_DIR}/apidoc/html;index.html >
            ${CMAKE_BINARY_DIR}/apidoc/html/PySide.devhelp;${BINDING_API_VERSION}&&;
            gzip;-9v;-f;${CMAKE_BINARY_DIR}/apidoc/html/PySide.devhelp
    COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_INSTALL_PREFIX}/share/devhelp/books"
    COMMAND ${CMAKE_COMMAND} -E create_symlink "${CMAKE_INSTALL_PREFIX}/share/doc/${BINDING_NAME}/html" "${CMAKE_INSTALL_PREFIX}/share/devhelp/books/${BINDING_NAME}"
)

#install files
add_custom_target(apidocinstall
    COMMAND mkdir -p ${CMAKE_INSTALL_PREFIX}/share/doc/${BINDING_NAME} && cp -rv ${CMAKE_BINARY_DIR}/apidoc/* ${CMAKE_INSTALL_PREFIX}/share/doc/${BINDING_NAME}
)

add_dependencies(apidocinstall apidevhelp)

else (NOT ${graphviz_exec} STREQUAL graphviz_exec-NOTFOUND)
    message(STATUS "Missing graphviz tool (dot), apidoc generation targets disabled.")
endif (NOT ${graphviz_exec} STREQUAL graphviz_exec-NOTFOUND)

else (QT_SRC_DIR)
    message(STATUS "QT_SRC_DIR variable not set, apidoc generation targets disabled.")
endif (QT_SRC_DIR)

