# FindMathematica @Mathematica_CMAKE_MODULE_VERSION@ documentation build script

# JAVACMD is an environment variable that points to the full path to the Java runtime executable
# used by the Apache Ant wrapper scripts
set (ENV{JAVACMD} "@_option_JAVACMD@")

set (Mathematica_KERNEL_EXECUTABLE "@Mathematica_KERNEL_EXECUTABLE@")
set (Mathematica_ANT_EXECUTABLE "@Mathematica_ANT_EXECUTABLE@")
set (Mathematica_JLink_PACKAGE_DIR "@Mathematica_JLink_PACKAGE_DIR@")
set (Mathematica_DocumentationBuild_PACKAGE_DIR "@Mathematica_DocumentationBuild_PACKAGE_DIR@")
set (Mathematica_DEBUG "@Mathematica_DEBUG@")

set (DOCU_INPUT_DIRECTORY "@_option_INPUT_DIRECTORY@")
set (DOCU_OUTPUT_DIRECTORY "@_option_OUTPUT_DIRECTORY@")
set (DOCU_TYPE "@_option_DOCUMENTATION_TYPE@")
set (DOCU_LANGUAGE "@_option_LANGUAGE@")
set (DOCU_APP_NAME "@_option_APPLICATION_NAME@")
set (CHECK_TIMESTAMPS "@_option_CHECK_TIMESTAMPS@")

function(_to_native_path _inPath _outPathVariable)
	if (CYGWIN)
		execute_process(
			COMMAND cygpath "--mixed" "${_inPath}" TIMEOUT 5
			OUTPUT_VARIABLE ${_outPathVariable} OUTPUT_STRIP_TRAILING_WHITESPACE)
	elseif (CMAKE_HOST_WIN32)
		string (REPLACE "/" "\\" ${_outPathVariable} "${_inPath}")
	else()
		# use CMake path literally
		set (${_outPathVariable} "${_inPath}")
	endif()
	set (${_outPathVariable} "${${_outPathVariable}}" PARENT_SCOPE)
endfunction()

set (_buildDocu TRUE)
# handle CHECK_TIMESTAMPS option
if (CHECK_TIMESTAMPS AND EXISTS "${DOCU_OUTPUT_DIRECTORY}")
	if (DOCU_TYPE MATCHES "[Nn]otebook")
		file (GLOB_RECURSE _docuSourceNBs RELATIVE "${DOCU_INPUT_DIRECTORY}" "${DOCU_INPUT_DIRECTORY}/*.nb")
		file (GLOB_RECURSE _docuBinaryNBs RELATIVE "${DOCU_OUTPUT_DIRECTORY}" "${DOCU_OUTPUT_DIRECTORY}/*.nb")
		if (_docuBinaryNBs)
			set (_docuNBs ${_docuSourceNBs} ${_docuBinaryNBs})
			list (REMOVE_DUPLICATES _docuNBs)
			set (_docuNBTrigger "")
			foreach (_docuNB ${_docuNBs})
				if ("${DOCU_INPUT_DIRECTORY}/${_docuNB}" IS_NEWER_THAN "${DOCU_OUTPUT_DIRECTORY}/${_docuNB}")
					list (APPEND _docuNBTrigger "${_docuNB}")
					list (LENGTH _docuNBTrigger _len)
					if (_len GREATER 10)
						# stop if many out-of-date files have been found
						break()
					endif()
				endif()
			endforeach()
			if (_docuNBTrigger)
				message (STATUS "Out-of-date ${DOCU_APP_NAME} Mathematica documentation notebooks: ${_docuNBTrigger}")
			else()
				message (STATUS "Built ${DOCU_APP_NAME} Mathematica ${DOCU_TYPE} documentation is up-to-date")
				set (_buildDocu FALSE)
			endif()
		endif()
	elseif (DOCU_TYPE MATCHES "HTML")
		file (GLOB_RECURSE _docuSourceNBs RELATIVE "${DOCU_INPUT_DIRECTORY}" "${DOCU_INPUT_DIRECTORY}/*.nb")
		file (GLOB_RECURSE _docuBinaryHTMLs RELATIVE "${DOCU_OUTPUT_DIRECTORY}" "${DOCU_OUTPUT_DIRECTORY}/*.html")
		if (_docuBinaryHTMLs)
			set (_docuNBTrigger "")
			foreach (_docuNB ${_docuSourceNBs})
				get_filename_component(_docuBaseName "${_docuNB}" NAME_WE)
				string (REGEX MATCHALL "[^;]+/${_docuBaseName}\\.html" _docuHTMLNames "${_docuBinaryHTMLs}")
				if (_docuHTMLNames)
					foreach (_docuHTMLName ${_docuHTMLNames})
						if ("${DOCU_INPUT_DIRECTORY}/${_docuNB}" IS_NEWER_THAN "${DOCU_OUTPUT_DIRECTORY}/${_docuHTMLName}")
							list (APPEND _docuNBTrigger "${_docuNB}")
							break()
						endif()
					endforeach()
				else()
					# no corresponding HTML document
					list (APPEND _docuNBTrigger "${_docuNB}")
				endif()
				list (LENGTH _docuNBTrigger _len)
				if (_len GREATER 10)
					# stop if many out-of-date files have been found
					break()
				endif()
			endforeach()
			if (_docuNBTrigger)
				message (STATUS "Out-of-date ${DOCU_APP_NAME} Mathematica documentation notebooks: ${_docuNBTrigger}")
			else()
				message (STATUS "Built ${DOCU_APP_NAME} Mathematica ${DOCU_TYPE} documentation is up-to-date")
				set (_buildDocu FALSE)
			endif()
		endif()
	endif()
endif()

if (_buildDocu)
	# clean previously built documentation files
	file (REMOVE_RECURSE "${DOCU_OUTPUT_DIRECTORY}")
	file (MAKE_DIRECTORY "${DOCU_OUTPUT_DIRECTORY}")
	get_filename_component(_appPath "${Mathematica_DocumentationBuild_PACKAGE_DIR}" DIRECTORY)
	string (TOLOWER "${DOCU_TYPE}.xml" _buildFileName)
	set (_buildFile "${Mathematica_DocumentationBuild_PACKAGE_DIR}/SystemFiles/ant/Build/${_buildFileName}")
	_to_native_path ("${Mathematica_ANT_EXECUTABLE}" _antExecutableNative)
	_to_native_path ("${_buildFile}" _buildFileNative)
	_to_native_path ("${_appPath}" _appPathNative)
	_to_native_path ("${Mathematica_KERNEL_EXECUTABLE}" _kernelExecutableNative)
	_to_native_path ("${Mathematica_JLink_PACKAGE_DIR}" _jlinkPathNative)
	_to_native_path ("${DOCU_INPUT_DIRECTORY}" _inputDirNative)
	_to_native_path ("${DOCU_OUTPUT_DIRECTORY}" _outputDirNative)
	set (_cmd COMMAND "${_antExecutableNative}")
	list (APPEND _cmd "-buildfile" "${_buildFileNative}")
	list (APPEND _cmd "-DappPath=${_appPathNative}")
	list (APPEND _cmd "-Dapp.name=${DOCU_APP_NAME}")
	list (APPEND _cmd "-DmathExe=${_kernelExecutableNative}")
	list (APPEND _cmd "-Djlinkpath=${_jlinkPathNative}")
	list (APPEND _cmd "-DinputDir=${_inputDirNative}")
	list (APPEND _cmd "-DoutputDir=${_outputDirNative}")
	list (APPEND _cmd "-Dlanguage=${DOCU_LANGUAGE}")
	list (APPEND _cmd "-DincludeLinkTrails=False")
	list (APPEND _cmd "-Dlocal=True")
	list (APPEND _cmd "-DcompleteHTMLQ=True")
	if (Mathematica_DEBUG)
		list (APPEND _cmd "-Ddebug=True")
	else()
		list (APPEND _cmd "-Ddebug=False")
	endif()
	list (APPEND _cmd RESULT_VARIABLE _result)
	if (Mathematica_DEBUG)
		message (STATUS "execute_process: ${_cmd}")
	endif()
	execute_process(${_cmd})
	message(STATUS "${DOCU_APP_NAME} ${DOCU_TYPE} generation result=${_result}")
endif()
