message(STATUS "\nStarting configuring of the ${DOCTARGET}-user-manual material\n")

#############################################################
# There are warning issued during the compilation of almost
# any LaTeX source, but we want the compilation not to stop...
# Because the build process has -Werror set globally we want to
# disable it here specifically.
if(PEDANTIC)
  REMOVE_DEFINITIONS (-Werror)
endif(PEDANTIC)

if(UNIX AND NOT APPLE)

  set(CUR_DOC_DIR ${CMAKE_SOURCE_DIR}/${DOCTARGET}/user-manual)
  set(CUR_DOC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/build-user-manual)

  message(STATUS "Current doc directory: ${CUR_DOC_DIR}")
  message(STATUS "Current doc build directory: ${CUR_DOC_BUILD_DIR}")

  # Copy the directory to a build directory. This will also copy as "hard"
  # files, following the various symbolic links,  to other files in other places
  # in the source tree (preface.tex is an example, images/splashscreen.png or
  # images/addresses.png are two other examples; the files in
  # user-manual-common/figures that are linked in the local figures directory
  # also).
  execute_process(COMMAND cp -rpL ${CUR_DOC_DIR} ${CUR_DOC_BUILD_DIR}
    WORKING_DIRECTORY	${CMAKE_CURRENT_BINARY_DIR})

  # Make sure we use the Makefile that is located in the new directory, copy 
  # of the original user-manual directory.
  add_custom_command(OUTPUT ${DOCTARGET}-doc.pdf
    COMMAND make -f ${CUR_DOC_BUILD_DIR}/Makefile usermanual
    WORKING_DIRECTORY ${CUR_DOC_BUILD_DIR}
    COMMENT "Build the ${DOCTARGET} user manual")

  if(BUILD_USER_MANUALS)

    # Command:
    # make ${DOCTARGET}-user-manual
    add_custom_target(${DOCTARGET}-user-manual ALL
      DEPENDS ${DOCTARGET}-doc.pdf
      COMMENT "Make ${DOCTARGET}-user-manual")

    # Command:
    # make copy-${DOCTARGET}-doc.pdf
    add_custom_target(copy-${DOCTARGET}-doc.pdf ALL
      COMMAND ${CMAKE_COMMAND} -E copy ${CUR_DOC_BUILD_DIR}/${DOCTARGET}-doc.pdf
      ${CMAKE_SOURCE_DIR}/${DOCTARGET}/user-manual
      DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${DOCTARGET}-doc.pdf
      COMMENT "Copy the pdf file to the source doc dir.")

  endif()

endif()

if(PEDANTIC)
  ADD_DEFINITIONS (-Werror)
endif(PEDANTIC)

if(UNIX AND NOT APPLE)

install(FILES ${CUR_DOC_BUILD_DIR}/${DOCTARGET}-doc.pdf 
  DESTINATION ${MSXPERTSUITE_DOC_DIR})

elseif(APPLE)

	install(FILES ${CMAKE_SOURCE_DIR}/${DOCTARGET}/user-manual/${DOCTARGET}-doc.pdf
		DESTINATION ${MSXPERTSUITE_DOC_DIR})

endif()

message(STATUS "\nFinished configuring of the ${DOCTARGET}-user-manual material\n")

