#
#  For more information, please see: http://software.sci.utah.edu
#
#  The MIT License
#
#  Copyright (c) 2016 Scientific Computing and Imaging Institute,
#  University of Utah.
#
#
#  Permission is hereby granted, free of charge, to any person obtaining a
#  copy of this software and associated documentation files (the "Software"),
#  to deal in the Software without restriction, including without limitation
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
#  and/or sell copies of the Software, and to permit persons to whom the
#  Software is furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included
#  in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#  DEALINGS IN THE SOFTWARE.
#

# Define macros for adding documentation
# Migrated from the SCIRun documentation build

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/seg3d.sty.in  ${CMAKE_CURRENT_BINARY_DIR}/seg3d.sty)

message(STATUS "Setting up documentation")

macro(ADD_PDF_DOCUMENT document)

  message(STATUS "Create ${document} Document")

  add_custom_command(
      OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/${document}.aux
      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${document}.tex
      COMMAND ${PDFLATEX_COMPILER}
      ARGS    -interaction=batchmode ${CMAKE_CURRENT_BINARY_DIR}/${document}.tex
      COMMENT "PDFLatex ${document} (first pass)"
    )

  file(READ ${CMAKE_CURRENT_SOURCE_DIR}/${document}.tex tex_file)
  string(REGEX MATCH "bibliography" NEED_BIBTEX ${tex_file})

  if(NEED_BIBTEX)
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${document}.bbl
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${document}.aux
        COMMAND ${BIBTEX_COMPILER}
        ARGS    -terse ${CMAKE_CURRENT_BINARY_DIR}/${document}
        COMMENT "Bibtex ${document}"
      )

    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/${document}.pdf
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${document}.bbl
        COMMAND ${PDFLATEX_COMPILER}
        ARGS    -interaction=batchmode ${CMAKE_CURRENT_BINARY_DIR}/${document}.tex
        COMMENT "PDFLatex ${document} (second pass)"
      )

    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/${document}.log
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${document}.bbl
                ${CMAKE_CURRENT_BINARY_DIR}/${document}.pdf
        COMMAND ${PDFLATEX_COMPILER}
        ARGS    -interaction=batchmode ${CMAKE_CURRENT_BINARY_DIR}/${document}.tex
        COMMENT "PDFLatex ${document} (third pass)"
      )
  else()
    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/${document}.pdf
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${document}.aux
        COMMAND ${PDFLATEX_COMPILER}
        ARGS    -interaction=batchmode ${CMAKE_CURRENT_BINARY_DIR}/${document}.tex
        COMMENT "PDFLatex ${document} (second pass)"
      )

    add_custom_command(
        OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/${document}.log
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${document}.pdf
        COMMAND ${PDFLATEX_COMPILER}
        ARGS    -interaction=batchmode ${CMAKE_CURRENT_BINARY_DIR}/${document}.tex
        COMMENT "PDFLatex ${document} (third pass)"
      )

  endif()

  add_custom_target(${document} ALL
                      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${document}.log)

  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${document}.tex  ${CMAKE_CURRENT_BINARY_DIR}/${document}.tex COPYONLY)

  file(GLOB_RECURSE png_figures "${CMAKE_CURRENT_SOURCE_DIR}/${document}_figures/*.png")

  file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${document}_figures)
  foreach(png_figure ${png_figures})
    message(STATUS "copying ${png_figure}")
    get_filename_component(png_name ${png_figure} NAME)
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${document}_figures/${png_name}
      ${CMAKE_CURRENT_BINARY_DIR}/${document}_figures/${png_name} COPYONLY)
  endforeach()

  file(GLOB_RECURSE pdf_figures "${CMAKE_CURRENT_SOURCE_DIR}/${document}_figures/*.pdf")
  foreach(pdf_figure ${pdf_figures})
    get_filename_component(pdf_name ${pdf_figure} NAME)
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${document}_figures/${pdf_name}
      ${CMAKE_CURRENT_BINARY_DIR}/${document}_figures/${pdf_name} COPYONLY)
  endforeach()

  if(APPLE)
    install(FILES ${SEG3D_BINARY_DIR}/Documentation/${document}.pdf
      DESTINATION ${OSX_UTILS_DIR})
  else()
    install(FILES ${SEG3D_BINARY_DIR}/Documentation/${document}.pdf DESTINATION Documentation)
  endif()
endmacro()

# ADD DOCUMENTS

ADD_PDF_DOCUMENT(Seg3DTutorial)
ADD_PDF_DOCUMENT(Seg3DBasicFunctionality)
#ADD_PDF_DOCUMENT(Seg3DPythonCookbook)

# Forcing documentation build
