# find Python, make sure we use the same version for interpreter and libraries
find_package(PythonInterp)
message(STATUS "Python version from interpreter is ${PYTHON_VERSION_STRING}")
set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
find_package(PythonLibs)

set(QT_USE_QTOPENGL TRUE)
find_package(Qt4)
find_package(OpenGL)

# make sure we have the right modules
if (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND AND QT4_FOUND AND OPENGL_FOUND)
	include(${QT_USE_FILE})
	add_definitions(${QT_DEFINITIONS})
	
	message(STATUS "Python libs and executable found, looking for boost::python")
	find_package(Boost COMPONENTS python)
	if (Boost_FOUND)
		message(STATUS "boost::python found, generating python bindings")
		include_directories(${PROJECT_SOURCE_DIR} ${PYTHON_INCLUDE_DIRS} ${Boost_INCLUDE_DIR})
		python_add_module(pyenki enki.cpp)
		target_link_libraries(pyenki enki enkiviewer ${QT_LIBRARIES} ${OPENGL_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
		# fix for old python_add_module
		set_target_properties(pyenki PROPERTIES PREFIX "")
		if (PYTHON_CUSTOM_TARGET)
			install(TARGETS pyenki LIBRARY DESTINATION ${PYTHON_CUSTOM_TARGET})
		else (PYTHON_CUSTOM_TARGET)
			if (PYTHON_DEB_INSTALL_TARGET)
				set(PYTHON_COMMAND "import sys; print 'lib/python'+str(sys.version_info[0])+'.'+str(sys.version_info[1])+'/dist-packages'")
			else (PYTHON_DEB_INSTALL_TARGET)
				set(PYTHON_COMMAND "from distutils.sysconfig import get_python_lib; print(get_python_lib(1, prefix='${CMAKE_INSTALL_PREFIX}'))")
			endif (PYTHON_DEB_INSTALL_TARGET)
			execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" "${PYTHON_COMMAND}" OUTPUT_VARIABLE PYTHON_SITE_MODULES OUTPUT_STRIP_TRAILING_WHITESPACE)
			install(TARGETS pyenki LIBRARY DESTINATION ${PYTHON_SITE_MODULES})
		endif (PYTHON_CUSTOM_TARGET)
	else (Boost_FOUND)
		message(WARNING "You need boost::python to generate python bindings")
	endif (Boost_FOUND)
else (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND AND QT4_FOUND AND OPENGL_FOUND)
	message(WARNING "Python libs or executable, or Qt4, or OpenGL not found, skipping Python bindings")
endif (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND AND QT4_FOUND AND OPENGL_FOUND)
