cmake_minimum_required(VERSION 2.6)

####################
# Microsoft SDK
SET(KinectSDK_ROOT_DIR "$ENV{KINECTSDK_DIR}")
SET(KinectSDK_LIB_DIR "${KinectSDK_ROOT_DIR}lib/x86")
SET(KinectSDK_INCLUDE_DIR "${KinectSDK_ROOT_DIR}inc")

SET(KinectSDK_DLLS 
	"${KinectSDK_ROOT_DIR}/KinectCOMLib.dll"
)
SET(KinectSDK_LIBRARIES 
	"${KinectSDK_LIB_DIR}/MSRKinectNUI.lib"
)
SET(KinectSDK_INCLUDES 
	"${KinectSDK_INCLUDE_DIR}/MSR_NuiApi.h"
	"${KinectSDK_INCLUDE_DIR}/MSR_NuiImageCamera.h"
	"${KinectSDK_INCLUDE_DIR}/MSR_NuiProps.h"
	"${KinectSDK_INCLUDE_DIR}/MSR_NuiSkeleton.h"
	"${KinectSDK_INCLUDE_DIR}/MSRKinectAudio.h"
)

####################   Macro   #######################
MACRO(CHECK_FILES _FILES _DIR)
	SET(_MISSING_FILES)
	FOREACH(_FILE ${${_FILES}})
		IF(NOT EXISTS "${_FILE}")
			SET(KinectSDK_FOUND NO)
			get_filename_component(_FILE ${_FILE} NAME)
			SET(_MISSING_FILES "${_MISSING_FILES}${_FILE}, ")
		ENDIF()
	ENDFOREACH()
	IF(_MISSING_FILES)
		MESSAGE(STATUS "In folder \"${${_DIR}}\" not found files: ${_MISSING_FILES}")
		SET(KinectSDK_FOUND NO)
	ENDIF()
ENDMACRO(CHECK_FILES)

MACRO(CHECK_DIR _DIR)
	IF(NOT EXISTS "${${_DIR}}")
		MESSAGE(STATUS "Folder \"${${_DIR}}\" not found.")
		SET(KinectSDK_FOUND NO)
	ENDIF()
ENDMACRO(CHECK_DIR)

##################### Checking #######################
MESSAGE(STATUS "Searching KinectSDK.")
SET(KinectSDK_FOUND YES)

CHECK_DIR(KinectSDK_ROOT_DIR)
IF(KinectSDK_FOUND)
	CHECK_DIR(KinectSDK_LIB_DIR)
	CHECK_DIR(KinectSDK_INCLUDE_DIR)
	
	IF(KinectSDK_FOUND)
		CHECK_FILES(KinectSDK_DLLS KinectSDK_ROOT_DIR)
		CHECK_FILES(KinectSDK_LIBRARIES KinectSDK_LIB_DIR)
		CHECK_FILES(KinectSDK_INCLUDES KinectSDK_INCLUDE_DIR)
	ENDIF()
ENDIF()

MESSAGE(STATUS "KinectSDK_FOUND - ${KinectSDK_FOUND}.")

####################

# ITK
FIND_PACKAGE(ITK REQUIRED)
IF(ITK_FOUND)
	INCLUDE_DIRECTORIES(${ITK_INCLUDE_DIRS})
	INCLUDE(${ITK_USE_FILE})
ELSE()
	MESSAGE("ITK not found, but needed.")
ENDIF(ITK_FOUND)

# VTK
FIND_PACKAGE(VTK REQUIRED)
IF(VTK_FOUND)
	INCLUDE(${VTK_USE_FILE})
	INCLUDE_DIRECTORIES(${VTK_INCLUDE_DIRS})
ELSE()
	MESSAGE("VTK not found, but needed.")
ENDIF(VTK_FOUND)

# Qt4
FIND_PACKAGE(Qt4 REQUIRED)
IF(QT4_FOUND)
	INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR})
	INCLUDE(${QT_USE_FILE})	
ELSE()
	MESSAGE("QT4 not found, but needed.")
ENDIF(QT4_FOUND)



# RITK
FIND_PACKAGE(RITK REQUIRED)
IF(RITK_FOUND)
	INCLUDE_DIRECTORIES(${RITK_INCLUDE_DIR})
	INCLUDE(${RITK_USE_FILE})	
ELSE()
	MESSAGE("RITK not found, but needed.")
ENDIF(RITK_FOUND)

# CUDA
FIND_PACKAGE(CUDA)
IF(CUDA_FOUND)
ELSE()
	MESSAGE(FATAL_ERROR "CUDA not found, but needed.")
ENDIF(CUDA_FOUND)


#-----------------------------------------------------------------------------

SET(HDRS
	KinectSDKSourcePluginFactory.h
	KinectSDKSourcePlugin.h
	KinectSDKSourcePluginRImageSource.h
	KinectSDKSourcePluginWidget.h
	resource.h
	stdafx.h
	KinectSDKExport.h
)

SET(SRCS
	KinectSDKSourcePluginFactory.cxx
	KinectSDKSourcePlugin.cxx
	KinectSDKSourcePluginRImageSource.txx
	KinectSDKSourcePluginWidget.cxx
	KinectSDKInstantiations.cxx
)




#-----------------------------------------------------------------------------

# Qt
# all childs of qobject have to be put here
SET(MOC_HDRS 	
	KinectSDKSourcePlugin.h
	KinectSDKSourcePluginWidget.h
	KinectSDKSourcePluginFactory.h
)

SET(UIS
	KinectSDKSourcePluginWidget.ui
)

QT4_WRAP_UI(UI_HDRS ${UIS})	
QT4_WRAP_CPP(MOC_SRCS ${MOC_HDRS})

#-----------------------------------------------------------------------------

# Set the name for the library
SET(LIBRARY_NAME KinectSDKSourcePlugin)

# Declare as plugin library
SET(${LIBRARY_NAME}
	${LIBRARY_NAME}
	CACHE INTERNAL ""
)

# Set include directories
SET(INCLUDE_DIRS	
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_BINARY_DIR}
	${QT_QTOPENGL_INCLUDE_DIR}
	${KINECTIMAGEPLUGIN_INCLUDE_DIRS})

# Declare as plugin include directories
SET(PLUGIN_INCLUDE_DIRS
	${PLUGIN_INCLUDE_DIRS}
	${INCLUDE_DIRS}
	CACHE INTERNAL ""
)

# QT plugin
ADD_DEFINITIONS(-DQT_PLUGIN)
ADD_DEFINITIONS(-DQT_SHARED)
ADD_DEFINITIONS("/DKINECTSDK_SHAREDLIB_EXPORT")
INCLUDE_DIRECTORIES(${INCLUDE_DIRS} ${RITK_INCLUDE_DIRS} ${KinectSDK_INCLUDE_DIR})
ADD_LIBRARY(${LIBRARY_NAME} SHARED 
	${SRCS} 
	${HDRS} 
	${MOC_SRCS} 
	${UI_HDRS}
) 


TARGET_LINK_LIBRARIES(${LIBRARY_NAME} 
	debug ${RITK_COMMON_LIBRARY_DEBUG}
	debug ${RITK_CORE_LIBRARY_DEBUG}
	optimized ${RITK_COMMON_LIBRARY}
	optimized ${RITK_CORE_LIBRARY}
	${ITK_LIBRARIES} 
	${QT_LIBRARIES} 
	${KinectSDK_LIBRARIES}
	${KINECTIMAGEPLUGIN_LIBRARY}
	)

SET_TARGET_PROPERTIES(${LIBRARY_NAME} PROPERTIES 
	DEBUG_POSTFIX "d")

INSTALL(TARGETS ${LIBRARY_NAME}
	RUNTIME DESTINATION ${RITK_BINARY_DIR}/release/plugins CONFIGURATIONS RELEASE
)

INSTALL(TARGETS ${LIBRARY_NAME}
	RUNTIME DESTINATION ${RITK_BINARY_DIR}/debug/plugins CONFIGURATIONS DEBUG
)
