#
# This file is part of MIA - a toolbox for medical image analysis 
# Copyright (c) Leipzig, Madrid 1999-2015 Gert Wollny
#
# MIA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#


INCLUDE (${CMAKE_ROOT}/Modules/FindDoxygen.cmake)


ADD_CUSTOM_TARGET(docs echo "creating documentation")
add_dependencies(doc docs)  

SET(MIADOCTOOLS_FILES 
  miareadxml.py  
  miawritprogram.py  
  MiaDoctools.cmake
  __init__.py
  )

SET(MIADOCTOOLS_PYTHON_EXE
  miaxml2man.py  
  miaxml2nipype.py  
  miaxml2sgml.py
  )


IF(MIA_CREATE_USERDOC) 

  INSTALL(FILES ${MIADOCTOOLS_FILES} DESTINATION ${MIA_DOCTOOLS_INSTALL_ROOT})
  
  INSTALL(PROGRAMS ${MIADOCTOOLS_PYTHON_EXE} DESTINATION ${MIA_DOCTOOLS_INSTALL_ROOT})

  ##################################################################
  #  for html userref xsltproc and the style sheet is needed
  #
  find_program(XSLTPROC xsltproc)
  IF(XSLTPROC)
    FIND_PATH(HTML_CHUNK_FILEPATH chunk.xsl
      PATHS 
      /usr/share/sgml/docbook/xsl-stylesheets/html/
      /usr/share/xml/docbook/stylesheet/docbook-xsl/html/
      DOC "style sheet for the creation of the user reference"
      )
  ELSE (XSLTPROC)
    IF(STRICT_DEPENDECIES)
      MESSAGE(ERROR ": xsltproc not found and STRICT_DEPENDECIES set")
    ENDIF(STRICT_DEPENDECIES)
  ENDIF(XSLTPROC)
  
  IF(HTML_CHUNK_FILEPATH) 
    #
    # handle the linking of some files to run the 
    # 
    ADD_CUSTOM_TARGET(userref_outdir mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/userref)
    ADD_CUSTOM_TARGET(userref_css 
      cp  ${CMAKE_CURRENT_SOURCE_DIR}/progref.css 
      ${CMAKE_CURRENT_BINARY_DIR}/userref/progref.css DEPENDS userref_outdir)

    ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/progref.xml
      COMMAND ln ARGS -sf ${CMAKE_CURRENT_SOURCE_DIR}/progref.xml ${CMAKE_CURRENT_BINARY_DIR}/progref.xml) 

    ADD_CUSTOM_TARGET(progref_link DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/progref.xml)

    MESSAGE("Python=${Python3_EXECUTABLE}")
    ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/program.xml 
      COMMAND ${Python3_EXECUTABLE} ARGS ${CMAKE_CURRENT_SOURCE_DIR}/miaxml2sgml.py ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
      DEPENDS xmldoc
      )
    
    ADD_CUSTOM_TARGET(process_xml DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/program.xml)

      
    ADD_CUSTOM_COMMAND(OUTPUT  "userref.stamp"
      COMMAND  
      ${XSLTPROC} ARGS 
      --param chunk.section.depth 3 
      --param chunk.first.sections 1 
      --param use.id.as.filename 1 
      --stringparam html.stylesheet  progref.css 
      --output "userref/" 
      ${HTML_CHUNK_FILEPATH}/chunk.xsl progref.xml
      COMMAND touch ARGS userref.stamp
      DEPENDS process_xml userref_css userref_outdir progref_link ${CMAKE_CURRENT_BINARY_DIR}/program.xml ) 
    
    ADD_CUSTOM_TARGET(userref DEPENDS userref.stamp)
    add_dependencies(docs userref)
    
    ####################################################
    #
    # installation of the user reference 
    IF(ALWAYS_CREATE_DOC)
      INSTALL(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/userref DESTINATION ${DOC_INSTALL_PATH}/)
      CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/mia2-doc.desktop.cmake ${CMAKE_CURRENT_BINARY_DIR}/mia2-doc.desktop)
      INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/mia2-doc.desktop DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications)
    ENDIF(ALWAYS_CREATE_DOC)     
  ENDIF(HTML_CHUNK_FILEPATH) 
  
ENDIF(MIA_CREATE_USERDOC)

OPTION(DOXYGEN_USE_MATHJAX "Use mathjax for formulas in html documentation" ON)

IF (DOXYGEN_FOUND)
  ADD_CUSTOM_TARGET(LibraryDoc echo "creating user library documentation")
  ADD_DEPENDENCIES(docs LibraryDoc)
  
  IF(DOXYGEN_DOT_FOUND)
    SET(HAVE_DOT YES)
  ENDIF(DOXYGEN_DOT_FOUND)

  IF(DOXYGEN_USE_MATHJAX)
    SET(USE_MATHJAX YES)
    FIND_PATH(MATHJAX_CODEFILE MathJax.js
      PATHS
      /usr/share/mathjax/
      /usr/share/javascript/
      /usr/share/javascript/mathjax/
      )
    IF(NOT MATHJAX_CODEFILE)
      MESSAGE(STATUS "No mathjax installation found, Doxygen will create the documentation linking to the on-line version") 
    ENDIF()
  ENDIF()
  
  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/reference.dox.cmake 
    ${CMAKE_CURRENT_BINARY_DIR}/reference.dox)
  
  ADD_CUSTOM_COMMAND(SOURCE COMMAND ${DOXYGEN_EXECUTABLE}
    ARGS  "${CMAKE_CURRENT_BINARY_DIR}/reference.dox"
    TARGET LibraryDoc
    OUTPUTS libdoc)
  
  IF(ALWAYS_CREATE_DOC) 
    INSTALL(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libdoc/html/ DESTINATION ${DOC_INSTALL_PATH}/libdoc/)
    CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libmia2-doc.desktop.cmake ${CMAKE_CURRENT_BINARY_DIR}/libmia2-doc.desktop)
    INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libmia2-doc.desktop DESTINATION ${CMAKE_INSTALL_PREFIX}/share/applications)
  ENDIF(ALWAYS_CREATE_DOC)     
 
ENDIF (DOXYGEN_FOUND)



