#########################################################################################
# megaglest_tests

SET(EXTERNAL_LIBS "")
SET(TARGET_NAME "megaglest_tests")

IF(BUILD_MEGAGLEST_TESTS)
	MESSAGE(STATUS "Will try to build MegaGlest unit tests")

	INCLUDE (CheckIncludeFiles)

	FIND_PACKAGE(CppUnit REQUIRED)
	INCLUDE_DIRECTORIES(${CPPUNIT_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${CPPUNIT_LIBRARY})
	ENDIF()

	FIND_PACKAGE(${SDL_VERSION_NAME} REQUIRED)
	INCLUDE_DIRECTORIES(${${SDL_VERSION_NAME}_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${${SDL_VERSION_NAME}_LIBRARY})
	ENDIF()

	if(WANT_USE_FriBiDi)
		find_package( FriBiDi )
		if(FRIBIDI_FOUND)
			add_definitions(-DHAVE_FRIBIDI)

			include_directories( ${FRIBIDI_INCLUDE_DIR} )
			SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${FRIBIDI_LIBRARIES})
		else()
			message("Could not find FriBiDi. Disabling FriBiDi support.")
		endif()
	endif()

    find_package(PkgConfig REQUIRED)
    IF(FORCE_STREFLOP_SOFTWRAPPER)
        pkg_search_module(STREFLOP streflop-soft)
    ELSE()
        IF(HAS_SSE_EXTENSIONS AND NOT ${FORCE_MAX_SSE_LEVEL} MATCHES "0")
            pkg_search_module(STREFLOP streflop-sse)
        ELSE()
            IF(HAS_X87_SUPPORT)
                pkg_search_module(STREFLOP streflop-x87)
            ELSE()
                pkg_search_module(STREFLOP streflop-soft)
            ENDIF()
        ENDIF()
    ENDIF()
    IF(NOT STREFLOP_FOUND)
        pkg_search_module(STREFLOP streflop)
    ENDIF()
    MESSAGE(STATUS "Search for Library STREFLOP result = ${STREFLOP_FOUND} libs: ${STREFLOP_LIBRARIES} include dirs: ${STREFLOP_INCLUDE_DIRS}")

    IF(FORCE_EMBEDDED_LIBS)
        SET(STREFLOP_FOUND OFF)
        MESSAGE(STATUS "FORCING USE of EMBEDDED Libraries...")
    ENDIF()

    IF(WANT_USE_STREFLOP)
        IF(STREFLOP_FOUND)
            INCLUDE_DIRECTORIES(${STREFLOP_INCLUDE_DIRS} ${STREFLOP_INCLUDE_DIRS}/streflop)
            SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${STREFLOP_LIBRARIES})

		    ADD_DEFINITIONS("-DUSE_STREFLOP_PKG")
        ENDIF()
    ENDIF()

	#########################################################################################
	# megaglest test code

	SET(DIRS_WITH_SRC
        ./
        shared_lib/graphics
        shared_lib/util
		shared_lib/xml)

    IF(NOT STREFLOP_FOUND)
	    SET(DIRS_WITH_SRC
            ${DIRS_WITH_SRC}
            shared_lib/streflop)
    ENDIF()
	
	SET(MG_INCLUDES_ROOT "./")
	SET(MG_SOURCES_ROOT "./")
	SET(MG_INCLUDE_FILES "")
	SET(MG_SOURCE_FILES "")

	SET(GLEST_LIB_INCLUDE_ROOT "../shared_lib/include/")
	SET(GLEST_LIB_INCLUDE_DIRS
                ${GLEST_LIB_INCLUDE_ROOT}platform/common
                ${GLEST_LIB_INCLUDE_ROOT}platform/posix
                ${GLEST_LIB_INCLUDE_ROOT}util
                ${GLEST_LIB_INCLUDE_ROOT}graphics
                ${GLEST_LIB_INCLUDE_ROOT}graphics/gl
                ${GLEST_LIB_INCLUDE_ROOT}graphics/iqm
                ${GLEST_LIB_INCLUDE_ROOT}sound
                ${GLEST_LIB_INCLUDE_ROOT}sound/openal
                ${GLEST_LIB_INCLUDE_ROOT}xml
                ${GLEST_LIB_INCLUDE_ROOT}xml/rapidxml
                ${GLEST_LIB_INCLUDE_ROOT}glew
                ${GLEST_LIB_INCLUDE_ROOT}lua
                ${GLEST_LIB_INCLUDE_ROOT}map

                ${PROJECT_SOURCE_DIR}/source/glest_game/graphics
                ${PROJECT_SOURCE_DIR}/source/glest_game/world
                ${PROJECT_SOURCE_DIR}/source/glest_game/sound
                ${PROJECT_SOURCE_DIR}/source/glest_game/type_instances
                ${PROJECT_SOURCE_DIR}/source/glest_game/types
                )

	IF(WANT_USE_STREFLOP)
        IF(NOT STREFLOP_FOUND)
		    SET(GLEST_LIB_INCLUDE_DIRS
			    ${GLEST_LIB_INCLUDE_DIRS}
			    ${GLEST_LIB_INCLUDE_ROOT}streflop
			    ${GLEST_LIB_INCLUDE_ROOT}streflop/libm_flt32_source)
        ENDIF()
	ENDIF()

	INCLUDE_DIRECTORIES( ${GLEST_LIB_INCLUDE_DIRS} )

	IF(WIN32)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/shared_lib/include/platform/posix)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/shared_lib/include/platform/win32)
		INCLUDE_DIRECTORIES( ${GLEST_LIB_INCLUDE_ROOT}platform/${SDL_VERSION_SNAME} )
	ELSE()
		INCLUDE_DIRECTORIES( ${GLEST_LIB_INCLUDE_ROOT}platform/${SDL_VERSION_SNAME} )
		SET(GLEST_LIB_INCLUDE_DIRS ${GLEST_LIB_INCLUDE_DIRS} ${GLEST_LIB_INCLUDE_ROOT}platform/${SDL_VERSION_SNAME})
		SET(GLEST_LIB_INCLUDE_DIRS ${GLEST_LIB_INCLUDE_DIRS} ${GLEST_LIB_INCLUDE_ROOT}platform/unix)
	ENDIF()

	FOREACH(DIR IN LISTS DIRS_WITH_SRC)
		set(INCLUDE_DIR_TO_GLOB ${MG_INCLUDES_ROOT}${DIR})
		INCLUDE_DIRECTORIES( ${INCLUDE_DIR_TO_GLOB} )
		IF(APPLE)
			#MESSAGE(STATUS "INCLUDE_DIR_TO_GLOB: ${INCLUDE_DIR_TO_GLOB}")
			FILE(GLOB INC_FILES_FROM_THIS_DIR ${INCLUDE_DIR_TO_GLOB}/*.h)
			#MESSAGE(STATUS "INCLUDE FILES GLOBBED: ${INC_FILES_FROM_THIS_DIR}")
			SET(MG_INCLUDE_FILES ${MG_INCLUDE_FILES} ${INC_FILES_FROM_THIS_DIR})
		ENDIF(APPLE)

		set(SRC_DIR_TO_GLOB ${MG_SOURCES_ROOT}${DIR})
		#MESSAGE(STATUS "SRC_DIR_TO_GLOB: ${SRC_DIR_TO_GLOB}")
		FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.cpp)
		#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
	
		set(SRC_DIR_TO_GLOB ${MG_SOURCES_ROOT}${DIR})
		#MESSAGE(STATUS "SRC_DIR_TO_GLOB: ${SRC_DIR_TO_GLOB}")
		FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.c)
		#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})

		IF(APPLE)
			FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.mm)
			#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
			SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
			FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.m)
			#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
			SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})	
		ENDIF(APPLE)
	ENDFOREACH(DIR)

	#MESSAGE(STATUS "Source files: ${MG_INCLUDE_FILES}")
	#MESSAGE(STATUS "Source files: ${MG_SOURCE_FILES}")
	#MESSAGE(STATUS "Include dirs: ${INCLUDE_DIRECTORIES}")

	IF(APPLE)
		SET(PLATFORM_SPECIFIC_DEFINES "-DHAVE_SYS_IOCTL_H")
	ELSEIF(WIN32)
		SET(PLATFORM_SPECIFIC_DEFINES "-DX11_AVAILABLE")
	ELSE()
		SET(PLATFORM_SPECIFIC_DEFINES "-DX11_AVAILABLE -DHAVE_SYS_IOCTL_H")
	ENDIF()

	SET_SOURCE_FILES_PROPERTIES(${MG_SOURCE_FILES} PROPERTIES COMPILE_FLAGS 
		"${PLATFORM_SPECIFIC_DEFINES} ${STREFLOP_PROPERTIES} ${CXXFLAGS}")
	
	SET_SOURCE_FILES_PROPERTIES(${MG_INCLUDE_FILES} PROPERTIES HEADER_FILE_ONLY 1)

	IF(WANT_DEV_OUTPATH)
		SET(EXECUTABLE_OUTPUT_PATH "${MEGAGLEST_FRIENDLY_OUTPUT_PATH}")
	ENDIF()

	MESSAGE(STATUS "EXTERNAL_LIBS = [${EXTERNAL_LIBS}]")

        MESSAGE(STATUS "***Note: The final output path for the MegaGlest engine will be [${EXECUTABLE_OUTPUT_PATH}]")

	SET_SOURCE_FILES_PROPERTIES(${MG_INCLUDE_FILES} PROPERTIES HEADER_FILE_ONLY 1)

	ADD_EXECUTABLE(${TARGET_NAME} ${MG_SOURCE_FILES} ${MG_INCLUDE_FILES})

	IF(NOT WIN32)
		IF(WANT_USE_STREFLOP AND NOT STREFLOP_FOUND)
			TARGET_LINK_LIBRARIES(${TARGET_NAME} ${MG_STREFLOP})
		ENDIF()
		TARGET_LINK_LIBRARIES(${TARGET_NAME} libmegaglest)
	ENDIF()

        TARGET_LINK_LIBRARIES(${TARGET_NAME} ${EXTERNAL_LIBS})
	IF(NOT "${CMAKE_GENERATOR}" STREQUAL "Xcode")
		# Run the unit tests after build
		IF(EXISTS ${XVFB_EXEC})
			MESSAGE("***-- Found xvfb-run: ${XVFB_EXEC} will run tests with it.")

			add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
				COMMAND ${XVFB_EXEC} --auto-servernum --server-num=770 ${EXECUTABLE_OUTPUT_PATH}${TARGET_NAME}
				COMMENT "***-- Found MegaGlest test runner: ${TARGET_NAME} about to run unit tests via xvfb...")
		ELSE()
			add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
				COMMAND ${EXECUTABLE_OUTPUT_PATH}${TARGET_NAME}
				COMMENT "***-- Found MegaGlest test runner: ${TARGET_NAME} about to run unit tests...")
		ENDIF()
	ENDIF()

ENDIF()
