OPTION(K3D_INSTALL_AQSIS "Include Aqsis in the K-3D installer" OFF)
OPTION(K3D_INSTALL_BOOST "Include Boost in the K-3D installer" OFF)
OPTION(K3D_INSTALL_CGKIT "Include cgkit in the K-3D installer" OFF)
OPTION(K3D_INSTALL_GTKMM "Include gtkmm in the K-3D installer" OFF)
OPTION(K3D_INSTALL_PYGTK "Include PyGTK in the K-3D installer" OFF)
OPTION(K3D_INSTALL_PYOPENGL "Include PyOpenGL in the K-3D installer" OFF)
OPTION(K3D_INSTALL_PYTHON "Include Python in the K-3D installer" OFF)

K3D_CHECK_INSTALLED_PYTHON_DEPENDENCY(K3D_INSTALL_CGKIT)
K3D_CHECK_INSTALLED_PYTHON_DEPENDENCY(K3D_INSTALL_PYGTK)
K3D_CHECK_INSTALLED_PYTHON_DEPENDENCY(K3D_INSTALL_PYOPENGL)

#install mingwm10.dll
FIND_PROGRAM(K3D_MINGW_GCC mingw32-gcc.exe
	C:/mingw/bin
	DOC "Path to the MinGW GCC command")

GET_FILENAME_COMPONENT(K3D_MINGW_PATH ${K3D_MINGW_GCC} PATH)
INSTALL(FILES ${K3D_MINGW_PATH}/mingwm10.dll ${K3D_MINGW_PATH}/libstdc++-6.dll ${K3D_MINGW_PATH}/pthreadGC2.dll DESTINATION bin)

# manually copy the HTML docs, since actually generating these on mingw is a royal pain
INSTALL(DIRECTORY ${CMAKE_BINARY_DIR}/share/k3d/guide/html/ DESTINATION share/k3d/guide/html)

SET(K3D_QT_DIR "c:/qt" CACHE PATH "Root directory containing Qt")

IF(K3D_EXTRA_INSTALL_ROOT)
  	INSTALL(DIRECTORY
		${K3D_EXTRA_INSTALL_ROOT}/bin/
		DESTINATION bin
		FILES_MATCHING PATTERN "*.dll"
	)
ENDIF()

IF(K3D_INSTALL_AQSIS)
	FIND_PROGRAM(K3D_AQSIS_COMMAND aqsis.exe
		DOC "Path to an installed aqsis.exe."
		)

	IF(NOT K3D_AQSIS_COMMAND)
		MESSAGE(SEND_ERROR "Could not locate aqsis.exe.")
	ENDIF(NOT K3D_AQSIS_COMMAND)

	IF(NOT K3D_BUILD_BUNDLED_RENDERMAN_ENGINES_MODULE)
		MESSAGE(SEND_ERROR "K3D_INSTALL_AQSIS requires K3D_BUILD_BUNDLED_RENDERMAN_ENGINES_MODULE")
	ENDIF(NOT K3D_BUILD_BUNDLED_RENDERMAN_ENGINES_MODULE)

	EXECUTE_PROCESS(
		COMMAND ${K3D_AQSIS_COMMAND} --version
		OUTPUT_VARIABLE K3D_AQSIS_VERSION
		)
	IF(K3D_AQSIS_VERSION)
		STRING(REGEX REPLACE " \\(revision.*" "" K3D_AQSIS_VERSION ${K3D_AQSIS_VERSION})
		STRING(REGEX REPLACE "aqsis version " "" K3D_AQSIS_VERSION ${K3D_AQSIS_VERSION})
		K3D_CHECK_INSTALLED_VERSION(${K3D_AQSIS_VERSION} "Aqsis" "1.7.0")
	ELSE(K3D_AQSIS_VERSION)
		MESSAGE(SEND_ERROR "Error determining Aqsis version.")
	ENDIF(K3D_AQSIS_VERSION)

	GET_FILENAME_COMPONENT(K3D_AQSIS_PATH ${K3D_AQSIS_COMMAND} PATH)
	INSTALL(DIRECTORY ${K3D_AQSIS_PATH}/ DESTINATION bin
	    USE_SOURCE_PERMISSIONS
	    PATTERN aqsisrc EXCLUDE
		)
	INSTALL(FILES ${K3D_QT_DIR}/bin/QtCore4.dll ${K3D_QT_DIR}/bin/QtGui4.dll DESTINATION bin)
	# Install generic aqsisrc, omitting paths (those are set at runtime through command line options)
	INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/aqsisrc DESTINATION bin)
ENDIF(K3D_INSTALL_AQSIS)

IF(K3D_INSTALL_BOOST)
	INSTALL(DIRECTORY
		${Boost_LIBRARY_DIRS}/
		DESTINATION bin
		FILES_MATCHING PATTERN "*.dll"
		PATTERN "*-d-*" EXCLUDE
	)
ENDIF(K3D_INSTALL_BOOST)

IF(K3D_BUILD_COLLADA_IO_MODULE)
    INSTALL(FILES ${K3D_COLLADA_LIBRARY} DESTINATION bin)
ENDIF(K3D_BUILD_COLLADA_IO_MODULE)

IF(K3D_INSTALL_GTKMM)
	INCLUDE(K3DFindPkgConfig)
	
	PKG_CHECK_MODULES(ATK atk)
	PKG_CHECK_MODULES(ATKMM atkmm-1.6)
	PKG_CHECK_MODULES(CAIRO cairo)
	PKG_CHECK_MODULES(CAIROMM cairomm-1.0)
	PKG_CHECK_MODULES(GDK gdk-2.0)
	PKG_CHECK_MODULES(GDKGLEXT gdkglext-1.0)
	PKG_CHECK_MODULES(GDKGLEXTW gdkglext-win32-1.0)
	PKG_CHECK_MODULES(GDKMM gdkmm-2.4)
	PKG_CHECK_MODULES(GTK gtk+-2.0)
	PKG_CHECK_MODULES(GTKMM gtkmm-2.4)

	# This is an ugly hack, we should be able to detect this automatically, but I'm feeling lazy ...
	SET(K3D_GTK_DIR "c:/gtk" CACHE PATH "Root directory containing GTK/gtkmm")

	INSTALL(DIRECTORY ${K3D_GTK_DIR}/bin/ DESTINATION bin
		USE_SOURCE_PERMISSIONS FILES_MATCHING
		PATTERN libgtk*.dll
		PATTERN libgdk*.dll
		PATTERN libpango*.dll
		PATTERN libatk*.dll
		PATTERN libcairo*.dll
		PATTERN libgio*.dll
		PATTERN libglib*.dll
		PATTERN libsigc*.dll
		PATTERN libgmodule*.dll
		PATTERN libgobject*.dll
		PATTERN libgthread*.dll
		PATTERN libexpat*.dll
		PATTERN libfontconfig*.dll
		PATTERN libpng*.dll
		PATTERN libintl*.dll
		PATTERN libfreetype*.dll
		PATTERN libzlib*.dll
		PATTERN libgcc*.dll
		PATTERN gspawn*
		PATTERN zlib1.dll
		PATTERN intl.dll
		PATTERN freetype6.dll
		)

	INSTALL(DIRECTORY ${K3D_GTK_DIR}/etc/ DESTINATION etc
		USE_SOURCE_PERMISSIONS FILES_MATCHING
		PATTERN pango
		PATTERN gtk*
		)

	INSTALL(DIRECTORY ${K3D_GTK_DIR}/lib/gtk-2.0 DESTINATION ${K3D_LIBDIR}
		USE_SOURCE_PERMISSIONS
		)

	# Override the default gtkrc file with our own
	INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/gtkrc DESTINATION etc/gtk-2.0)
ENDIF(K3D_INSTALL_GTKMM)

IF(K3D_INSTALL_PYTHON)
	FIND_PROGRAM(K3D_PYTHON_COMMAND python.exe
		PATHS "c:/python26"
		DOC "Path to an installed python.exe."
		)
	
	IF(NOT K3D_PYTHON_COMMAND)
		MESSAGE(SEND_ERROR "Could not locate python.exe.")
	ENDIF(NOT K3D_PYTHON_COMMAND)

	FIND_PROGRAM(K3D_PYTHON_LIBRARY python26.dll
		DOC "Path to the installed python26.dll"
		)

	IF(NOT K3D_PYTHON_LIBRARY)
		MESSAGE(SEND_ERROR "Could not locate python26.dll.")
	ENDIF(NOT K3D_PYTHON_LIBRARY)

	EXECUTE_PROCESS(
		COMMAND ${K3D_PYTHON_COMMAND} -V
		ERROR_VARIABLE K3D_PYTHON_VERSION
		)

	IF(K3D_PYTHON_VERSION)
		STRING(REGEX REPLACE "^Python ([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1" K3D_PYTHON_VERSION ${K3D_PYTHON_VERSION})
		K3D_CHECK_INSTALLED_VERSION(${K3D_PYTHON_VERSION} "Python" "2.6.1")
	ELSE(K3D_PYTHON_VERSION)
		MESSAGE(SEND_ERROR "Error determining Python version.")
	ENDIF(K3D_PYTHON_VERSION)

	INSTALL(PROGRAMS
		${K3D_PYTHON_COMMAND}
		${K3D_PYTHON_LIBRARY}
		DESTINATION bin
		)

	GET_FILENAME_COMPONENT(K3D_PYTHON_PATH ${K3D_PYTHON_COMMAND} PATH)
	FILE(GLOB K3D_PYTHON_MODULES ${K3D_PYTHON_PATH}/Lib/*.py)
	INSTALL(PROGRAMS
		${K3D_PYTHON_MODULES}
		DESTINATION ${K3D_LIBDIR}
		)

	SET(K3D_PYTHON_SITE_PACKAGES ${K3D_PYTHON_PATH}/Lib/site-packages)

	IF(K3D_INSTALL_CGKIT)
		EXECUTE_PROCESS(
			COMMAND ${K3D_PYTHON_COMMAND} -c "import cgkit.cgkitinfo;print cgkit.cgkitinfo.version"
			OUTPUT_VARIABLE K3D_CGKIT_VERSION
			)

		IF(K3D_CGKIT_VERSION)
			STRING(REGEX REPLACE "^([0-9]+\\.[0-9]+\\.[0-9]+[^ ]*).*" "\\1" K3D_CGKIT_VERSION ${K3D_CGKIT_VERSION})
			K3D_CHECK_INSTALLED_VERSION(${K3D_CGKIT_VERSION} "cgkit" "2.0.0alpha8")
		ELSE(K3D_CGKIT_VERSION)
			MESSAGE(SEND_ERROR "Error determining cgkit version.")
		ENDIF(K3D_CGKIT_VERSION)

		INSTALL(DIRECTORY
			${K3D_PYTHON_SITE_PACKAGES}/cgkit
			DESTINATION ${K3D_LIBDIR}/site-packages
			PATTERN *.pyc EXCLUDE
			PATTERN *.pyo EXCLUDE
			)
	ENDIF(K3D_INSTALL_CGKIT)

	IF(K3D_INSTALL_PYGTK)
		EXECUTE_PROCESS(
			COMMAND ${K3D_PYTHON_COMMAND} -c "import gtk;print gtk.pygtk_version"
			OUTPUT_VARIABLE K3D_PYGTK_VERSION
			)

		IF(K3D_PYGTK_VERSION)
			STRING(REGEX REPLACE ".*([0-9]+), ([0-9]+), ([0-9]+).*" "\\1.\\2.\\3" K3D_PYGTK_VERSION ${K3D_PYGTK_VERSION})
			K3D_CHECK_INSTALLED_VERSION(${K3D_PYGTK_VERSION} "PyGTK" "2.12.1")
		ELSE(K3D_PYGTK_VERSION)
			MESSAGE(SEND_ERROR "Error determining PyGTK version.")
		ENDIF(K3D_PYGTK_VERSION)

		INSTALL(DIRECTORY
			${K3D_PYTHON_SITE_PACKAGES}/cairo
			DESTINATION ${K3D_LIBDIR}/site-packages
			PATTERN *.pyc EXCLUDE
			PATTERN *.pyo EXCLUDE
			)
		INSTALL(DIRECTORY
			${K3D_PYTHON_SITE_PACKAGES}/gtk-2.0
			DESTINATION ${K3D_LIBDIR}/site-packages
			PATTERN *.pyc EXCLUDE
			PATTERN *.pyo EXCLUDE
			)
		INSTALL(PROGRAMS
			${K3D_PYTHON_SITE_PACKAGES}/pygtk.py
			DESTINATION ${K3D_LIBDIR}/site-packages
			)
	ENDIF(K3D_INSTALL_PYGTK)

	IF(K3D_INSTALL_PYOPENGL)
		EXECUTE_PROCESS(
			COMMAND ${K3D_PYTHON_COMMAND} -c "import OpenGL.version;print OpenGL.version.__version__"
			OUTPUT_VARIABLE K3D_PYOPENGL_VERSION
			)
		IF(K3D_PYOPENGL_VERSION)
			STRING(REGEX REPLACE "^([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1" K3D_PYOPENGL_VERSION ${K3D_PYOPENGL_VERSION})
			K3D_CHECK_INSTALLED_VERSION(${K3D_PYOPENGL_VERSION} "PyOpenGL" "3.0.0")
		ELSE(K3D_PYOPENGL_VERSION)
			MESSAGE(SEND_ERROR "Error determining PyOpenGL version.")
		ENDIF(K3D_PYOPENGL_VERSION)

		INSTALL(DIRECTORY
			${K3D_PYTHON_SITE_PACKAGES}/OpenGL
			DESTINATION ${K3D_LIBDIR}/site-packages
			PATTERN *.pyc EXCLUDE
			PATTERN *.pyo EXCLUDE
			)
	ENDIF(K3D_INSTALL_PYOPENGL)

ENDIF(K3D_INSTALL_PYTHON)

ADD_SUBDIRECTORY(nsis)

