###############################################################################
# 
#  Copyright (2008) Alexander Stukowski
#
#  This file is part of OVITO (Open Visualization Tool).
#
#  OVITO 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 2 of the License, or
#  (at your option) any later version.
#
#  OVITO 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/>.
#
###############################################################################

# Make sure that the scripting plugin is also built.
IF(NOT OVITO_BUILD_PLUGIN_SCRIPTING)
	MESSAGE(FATAL_ERROR "Conflict: Cannot build the graphical user interface for the scripting plugin without building the scripting plugin. The build variable OVITO_BUILD_PLUGIN_SCRIPTING_GUI cannot be set to ON when the variable OVITO_BUILD_PLUGIN_SCRIPTING is set to OFF. Please resolve in the CMake settings editor.")
ENDIF(NOT OVITO_BUILD_PLUGIN_SCRIPTING)

# Find QScintilla library
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
FIND_PACKAGE(QScintilla2)
IF(NOT QSCINTILLA2_FOUND)
	MESSAGE(FATAL_ERROR "The QScintilla library is required for the graphical scripting interface but was not found on your system. Please install this library or specify the location of the library manually.")
ENDIF(NOT QSCINTILLA2_FOUND)
INCLUDE_DIRECTORIES(${QSCINTILLA2_INCLUDE_DIR})

# Enable precompiled headers when using the GCC compiler.
IF(OVITO_USE_PRECOMPILED_HEADERS AND CMAKE_COMPILER_IS_GNUCXX)
	INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ENDIF(OVITO_USE_PRECOMPILED_HEADERS AND CMAKE_COMPILER_IS_GNUCXX)

# This is needed to export the symbols in this shared library.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMAKING_MODULE_SCRIPTING_GUI")

# The header files that need to be processed by the QT meta-object compiler.
SET(Scripting_gui_MOC_HDRS
	ui/ScriptUtility.h
	ui/ScriptEditor.h
)

# The regular source files of this library.
SET(Scripting_gui_SRCS
	ui/ScriptUtility.cpp
	ui/ScriptEditor.cpp	
)

# Use the QT meta-object compiler to generate additional code.
QT4_WRAP_CPP(Scripting_gui_MOC_SRCS ${Scripting_gui_MOC_HDRS} OPTIONS -DBOOST_TT_HAS_OPERATOR_HPP_INCLUDED)

# Create plugin library.
ADD_LIBRARY(ScriptingGUI
	PrecompiledHeader.cpp
	${Scripting_gui_SRCS}
	${Scripting_gui_MOC_SRCS}	
)

# Generate precompiled header
IF(OVITO_USE_PRECOMPILED_HEADERS)
	IF(MSVC)
		SET_SOURCE_FILES_PROPERTIES(${Scripting_gui_SRCS} PROPERTIES COMPILE_FLAGS "/Yuscripting_gui/ScriptingGUI.h /Fpscripting_gui.pch")
		SET_SOURCE_FILES_PROPERTIES(PrecompiledHeader.cpp PROPERTIES COMPILE_FLAGS "/Ycscripting_gui/ScriptingGUI.h /Fpscripting_gui.pch")
	ENDIF(MSVC)
	IF(CMAKE_COMPILER_IS_GNUCXX)
		ADD_PRECOMPILED_HEADER(ScriptingGUI ${CMAKE_SOURCE_DIR}/src/core/Core.h)
	ENDIF(CMAKE_COMPILER_IS_GNUCXX)
ENDIF(OVITO_USE_PRECOMPILED_HEADERS)

# Link required libraries
TARGET_LINK_LIBRARIES(ScriptingGUI Base Core)

# Link the target against the Qt libraries. 
TARGET_LINK_LIBRARIES(ScriptingGUI ${QT_LIBRARIES})

# Link to base scripting plugin. 
TARGET_LINK_LIBRARIES(ScriptingGUI Scripting)

# Add QScintilla library
TARGET_LINK_LIBRARIES(ScriptingGUI ${QSCINTILLA2_LIBRARIES})

# Assign an absolute install path to this dynamic link library.
IF(APPLE)
	SET_TARGET_PROPERTIES(ScriptingGUI PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${OVITO_RELATIVE_PLUGINS_DIRECTORY}")
ENDIF(APPLE)

# Copy Plugin manifest to destination directory.
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/resources/ScriptingGUI.manifest.xml"
               "${OVITO_PLUGINS_DIRECTORY}/ScriptingGUI.manifest.xml")

# On Window the third-party library DLLs are usually not part of the system. 
# We need to copy them to the OVITO directory.
IF(WIN32)
	# Copy library DLL to output directory.
	STRING(REPLACE ".lib" ".dll" QSCINTILLA2_DLL ${QSCINTILLA2_LIBRARIES})
	CONFIGURE_FILE("${QSCINTILLA2_DLL}" "${OVITO_BINARY_DIRECTORY}" COPYONLY)
	INSTALL(FILES "${QSCINTILLA2_DLL}" DESTINATION "${OVITO_RELATIVE_BINARY_DIRECTORY}")
ENDIF(WIN32)			   
			   
# This plugin will be part of the installation package.
IF(NOT OVITO_MONOLITHIC_BUILD)
	INSTALL(TARGETS ScriptingGUI DESTINATION "${OVITO_RELATIVE_PLUGINS_DIRECTORY}")
ENDIF(NOT OVITO_MONOLITHIC_BUILD)
INSTALL(FILES "${OVITO_PLUGINS_DIRECTORY}/ScriptingGUI.manifest.xml" DESTINATION "${OVITO_RELATIVE_PLUGINS_DIRECTORY}")
