OPTION(HTTRAQT_INSTALL_AQSIS "Include Aqsis in the HTTraQt installer" OFF)
OPTION(HTTRAQT_INSTALL_BOOST "Include Boost in the HTTraQt installer" OFF)
OPTION(HTTRAQT_INSTALL_CGKIT "Include cgkit in the HTTraQt installer" OFF)
OPTION(HTTRAQT_INSTALL_GTKMM "Include gtkmm in the HTTraQt installer" OFF)
OPTION(HTTRAQT_INSTALL_PYGTK "Include PyGTK in the HTTraQt installer" OFF)
OPTION(HTTRAQT_INSTALL_PYOPENGL "Include PyOpenGL in the HTTraQt installer" OFF)
OPTION(HTTRAQT_INSTALL_PYTHON "Include Python in the HTTraQt installer" OFF)

HTTRAQT_CHECK_INSTALLED_PYTHON_DEPENDENCY(HTTRAQT_INSTALL_CGKIT)
HTTRAQT_CHECK_INSTALLED_PYTHON_DEPENDENCY(HTTRAQT_INSTALL_PYGTK)
HTTRAQT_CHECK_INSTALLED_PYTHON_DEPENDENCY(HTTRAQT_INSTALL_PYOPENGL)

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

GET_FILENAME_COMPONENT(HTTRAQT_MINGW_PATH ${HTTRAQT_MINGW_GCC} PATH)
INSTALL(FILES ${HTTRAQT_MINGW_PATH}/mingwm10.dll DESTINATION bin)

IF(HTTRAQT_INSTALL_AQSIS)
	FIND_PROGRAM(HTTRAQT_AQSIS_COMMAND aqsis.exe
		DOC "Path to an installed aqsis.exe."
		)
	MARK_AS_ADVANCED(HTTRAQT_AQSIS_COMMAND)

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

	IF(NOT HTTRAQT_BUILD_BUNDLED_RENDERMAN_ENGINES_MODULE)
		MESSAGE(SEND_ERROR "HTTRAQT_INSTALL_AQSIS requires HTTRAQT_BUILD_BUNDLED_RENDERMAN_ENGINES_MODULE")
	ENDIF(NOT HTTRAQT_BUILD_BUNDLED_RENDERMAN_ENGINES_MODULE)

	EXECUTE_PROCESS(
		COMMAND ${HTTRAQT_AQSIS_COMMAND} --version
		OUTPUT_VARIABLE HTTRAQT_AQSIS_VERSION
		)
	IF(HTTRAQT_AQSIS_VERSION)
		STRING(REGEX REPLACE " \\(revision.*" "" HTTRAQT_AQSIS_VERSION ${HTTRAQT_AQSIS_VERSION})
		STRING(REGEX REPLACE "aqsis version " "" HTTRAQT_AQSIS_VERSION ${HTTRAQT_AQSIS_VERSION})
		HTTRAQT_CHECK_INSTALLED_VERSION(${HTTRAQT_AQSIS_VERSION} "Aqsis" "1.6.0")
	ELSE(HTTRAQT_AQSIS_VERSION)
		MESSAGE(SEND_ERROR "Error determining Aqsis version.")
	ENDIF(HTTRAQT_AQSIS_VERSION)

	GET_FILENAME_COMPONENT(HTTRAQT_AQSIS_PATH ${HTTRAQT_AQSIS_COMMAND} PATH)
	INSTALL(DIRECTORY ${HTTRAQT_AQSIS_PATH}/ DESTINATION bin
	    USE_SOURCE_PERMISSIONS
	    PATTERN aqsisrc EXCLUDE
		)
	# Install generic aqsisrc, omitting paths (those are set at runtime through command line options)
	INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/aqsisrc DESTINATION bin)
	#install msvcrt, needed since aqsis 1.4
	FIND_PROGRAM(VCREDIST_X86_2005 vcredist_x86_2005.exe DOC "Path to the Visual Studio C++ 2005 SP1 redistributable")
	INSTALL(FILES ${VCREDIST_X86_2005} DESTINATION redist)
	SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS
		"ExecWait '\\\"$INSTDIR\\\\redist\\\\vcredist_x86_2005.exe\\\"'")
ENDIF(HTTRAQT_INSTALL_AQSIS)

IF(HTTRAQT_INSTALL_BOOST)
	set(aPrevBoostLib "")
    foreach(aBoostLib ${Boost_LIBRARIES})
        if(NOT (aBoostLib STREQUAL "debug" OR aBoostLib STREQUAL "optimized" OR aPrevBoostLib STREQUAL "debug"))
            INSTALL(FILES ${aBoostLib} DESTINATION bin)
        endif()
        set(aPrevBoostLib ${aBoostLib})
    endforeach(aBoostLib)
ENDIF(HTTRAQT_INSTALL_BOOST)

IF(HTTRAQT_BUILD_COLLADA_IO_MODULE)
    INSTALL(FILES ${HTTRAQT_COLLADA_LIBRARY} DESTINATION bin)
ENDIF(HTTRAQT_BUILD_COLLADA_IO_MODULE)

IF(HTTRAQT_INSTALL_GTKMM)
	INCLUDE(HTTRAQTDFindPkgConfig)
	
	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)

	HTTRAQT_CHECK_INSTALLED_VERSION(${ATK_VERSION} "atk" "1.24.0")
	HTTRAQT_CHECK_INSTALLED_VERSION(${ATKMM_VERSION} "atkmm" "2.14.3")
	HTTRAQT_CHECK_INSTALLED_VERSION(${CAIRO_VERSION} "cairo" "1.8.6")
	HTTRAQT_CHECK_INSTALLED_VERSION(${CAIROMM_VERSION} "cairomm" "1.8.0")
	HTTRAQT_CHECK_INSTALLED_VERSION(${GDK_VERSION} "gdk" "2.14.7")
	HTTRAQT_CHECK_INSTALLED_VERSION(${GDKGLEXT_VERSION} "gdkglext" "1.2.0")
	HTTRAQT_CHECK_INSTALLED_VERSION(${GDKGLEXTW_VERSION} "gdkglext-win32" "1.2.0")
	HTTRAQT_CHECK_INSTALLED_VERSION(${GDKMM_VERSION} "gdkmm" "2.14.3")
	HTTRAQT_CHECK_INSTALLED_VERSION(${GTK_VERSION} "gtk" "2.14.7")
	HTTRAQT_CHECK_INSTALLED_VERSION(${GTKMM_VERSION} "gtkmm" "2.14.3")

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

	INSTALL(DIRECTORY ${HTTRAQT_GTK_DIR}/bin/ DESTINATION bin
		USE_SOURCE_PERMISSIONS
		PATTERN *.pdb EXCLUDE
		PATTERN *-config EXCLUDE
		PATTERN *glade* EXCLUDE
		PATTERN *xml++* EXCLUDE
		PATTERN atkmm-1.6d.dll EXCLUDE
		PATTERN gdkmm-2.4d.dll EXCLUDE
		PATTERN gettext.sh EXCLUDE
		PATTERN glib-gettextize EXCLUDE
		PATTERN glib-mkenums EXCLUDE
		PATTERN glibmm-2.4d.dll EXCLUDE
		PATTERN gtkaio.sh EXCLUDE
		PATTERN gtkautogen EXCLUDE
		PATTERN gtkconfigure EXCLUDE
		PATTERN gtkmm-2.4d.dll EXCLUDE
		PATTERN msg*.exe EXCLUDE
		PATTERN pangomm-1.4d.dll EXCLUDE
		PATTERN sigc-2.0d.dll EXCLUDE
		)

	INSTALL(DIRECTORY ${HTTRAQT_GTK_DIR}/etc/ DESTINATION etc
		USE_SOURCE_PERMISSIONS
		)

	INSTALL(DIRECTORY ${HTTRAQT_GTK_DIR}/lib/ DESTINATION ${HTTRAQT_LIBDIR}
		USE_SOURCE_PERMISSIONS
		PATTERN *.a EXCLUDE
		PATTERN *.lib EXCLUDE
		PATTERN *.pdb EXCLUDE
		PATTERN *glade* EXCLUDE
		PATTERN gdkmm-2.4 EXCLUDE
		PATTERN glib-2.0 EXCLUDE
		PATTERN glibmm-2.4 EXCLUDE
		PATTERN gtkglext-1.0 EXCLUDE
		PATTERN gtkmm-2.4 EXCLUDE
		PATTERN include EXCLUDE
		PATTERN libxml++-2.6 EXCLUDE
		PATTERN pkgconfig EXCLUDE
		PATTERN sigc++-2.0 EXCLUDE
		)

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

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

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

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

	EXECUTE_PROCESS(
		COMMAND ${HTTRAQT_PYTHON_COMMAND} -V
		ERROR_VARIABLE HTTRAQT_PYTHON_VERSION
		)

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

	INSTALL(PROGRAMS
		${HTTRAQT_PYTHON_COMMAND}
		${HTTRAQT_PYTHON_LIBRARY}
		DESTINATION bin
		)

	GET_FILENAME_COMPONENT(HTTRAQT_PYTHON_PATH ${HTTRAQT_PYTHON_COMMAND} PATH)
	FILE(GLOB HTTRAQT_PYTHON_MODULES ${HTTRAQT_PYTHON_PATH}/Lib/*.py)
	INSTALL(PROGRAMS
		${HTTRAQT_PYTHON_MODULES}
		DESTINATION ${HTTRAQT_LIBDIR}
		)

	SET(HTTRAQT_PYTHON_SITE_PACKAGES ${HTTRAQT_PYTHON_PATH}/Lib/site-packages)

	IF(HTTRAQT_INSTALL_CGKIT)
		EXECUTE_PROCESS(
			COMMAND ${HTTRAQT_PYTHON_COMMAND} -c "import cgkit.cgkitinfo;print cgkit.cgkitinfo.version"
			OUTPUT_VARIABLE HTTRAQT_CGKIT_VERSION
			)

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

		INSTALL(DIRECTORY
			${HTTRAQT_PYTHON_SITE_PACKAGES}/cgkit
			DESTINATION ${HTTRAQT_LIBDIR}/site-packages
			PATTERN *.pyc EXCLUDE
			PATTERN *.pyo EXCLUDE
			)
	ENDIF(HTTRAQT_INSTALL_CGKIT)

	IF(HTTRAQT_INSTALL_PYGTK)
		EXECUTE_PROCESS(
			COMMAND ${HTTRAQT_PYTHON_COMMAND} -c "import gtk;print gtk.pygtk_version"
			OUTPUT_VARIABLE HTTRAQT_PYGTK_VERSION
			)

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

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

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

		INSTALL(DIRECTORY
			${HTTRAQT_PYTHON_SITE_PACKAGES}/OpenGL
			DESTINATION ${HTTRAQT_LIBDIR}/site-packages
			PATTERN *.pyc EXCLUDE
			PATTERN *.pyo EXCLUDE
			)
	ENDIF(HTTRAQT_INSTALL_PYOPENGL)

	#Needed for Python 2.6
	FIND_PROGRAM(VCREDIST_X86_2008 vcredist_x86_2008.exe DOC "Path to the Visual Studio C++ 2008 SP1 redistributable")
	INSTALL(FILES ${VCREDIST_X86_2008} DESTINATION redist)
	SET(CPACK_NSIS_EXTRA_INSTALL_COMMANDS
		"${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}
			ExecWait '\\\"$INSTDIR\\\\redist\\\\vcredist_x86_2008.exe\\\"'")

ENDIF(HTTRAQT_INSTALL_PYTHON)

ADD_SUBDIRECTORY(nsis)

