#########################################################################################
# common libraries

SET(EXTERNAL_LIBS "")
SET(TARGET_NAME "glest")
SET(TARGET_NAME_MANPAGE "${TARGET_NAME}.6")

IF(BUILD_GLEST)
	MESSAGE(STATUS "Build ${TARGET_NAME} = YES")

	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++11")
	add_definitions("-DDATADIR=${INSTALL_DIR_DATA}")

	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()

	FIND_PACKAGE(OpenGL REQUIRED)
	INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${OPENGL_LIBRARY})
	ENDIF()

        IF(WANT_USE_XercesC)
	        FIND_PACKAGE(XercesC REQUIRED)
	        INCLUDE_DIRECTORIES(${XERCESC_INCLUDE})
	        IF(UNIX)
		        SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${XERCESC_LIBRARY})
	        ENDIF()
        ENDIF()

	FIND_PACKAGE(OpenAL REQUIRED)
	INCLUDE_DIRECTORIES(${OPENAL_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${OPENAL_LIBRARY})
	ENDIF()

	FIND_PACKAGE(OGG REQUIRED)
	INCLUDE_DIRECTORIES(${OGG_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${VORBIS_FILE_LIBRARY})
		IF(STATIC_OGG)
			SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${OGG_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY})
		ENDIF()
	ENDIF()

	include(FindLua)
	INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${LUA_LIBRARIES})
	ENDIF()

	FIND_PACKAGE(JPEG REQUIRED)
	INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${JPEG_LIBRARY})
	ENDIF()

	FIND_PACKAGE(PNG REQUIRED)
	INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${PNG_LIBRARY})
	ENDIF()

	IF(WANT_USE_OpenSSL)
		INCLUDE_DIRECTORIES(/usr/local/opt/openssl)
		set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
		include(FindOpenSSL)
		IF(OPENSSL_FOUND)
		    INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
		ELSE()
		    MESSAGE(STATUS "**NOTE: OpenSSL is usually not required.")
		ENDIF()
	ENDIF()

	include(FindCURL)
	INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
	IF(UNIX)
		IF(NOT OPENSSL_FOUND)
			# default
			SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${CURL_LIBRARIES})
		ELSE()
		  # mageia
			SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${CURL_LIBRARIES} ${OPENSSL_LIBRARIES})
		ENDIF()
	ENDIF()

	include(FindGLEW)
	INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIRS})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${GLEW_LIBRARIES})
	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()

	#########################################################################################
	# game
	SET(GLEST_SOURCE_FILES "")
	# libircclient
	IF(FORCE_EMBEDDED_LIBS)
		SET(FORCE_USE_EMBEDDED_Ircclient ON)
	ELSE()
		OPTION(FORCE_USE_EMBEDDED_Ircclient "Use embedded to source code version of ircclient" OFF)
	ENDIF()

	IF(NOT FORCE_USE_EMBEDDED_Ircclient)
		MESSAGE(STATUS "*** Searching for libircclient since WANT_STATIC_LIBS is: ${WANT_STATIC_LIBS} ...")
		FIND_PACKAGE(Ircclient)
	ENDIF()

	if(NOT IRCCLIENT_FOUND OR FORCE_USE_EMBEDDED_Ircclient)
		message(STATUS "Using included libbircclient sources")
		SET(GLEST_SOURCE_FILES ${GLEST_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/external/libircclient/src/libircclient.c)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/external/libircclient/include)
	else()
		MESSAGE(STATUS "*** Using libircclient found in [${IRCCLIENT_INCLUDE_DIR}] IRCCLIENT_FOUND [${IRCCLIENT_FOUND}] IRCCLIENT_LIBRARY [${IRCCLIENT_LIBRARY}]")
		INCLUDE_DIRECTORIES(${IRCCLIENT_INCLUDE_DIR})
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${IRCCLIENT_LIBRARY})
	endif()

	SET(DIRS_WITH_SRC
		ai
		facilities
		game
		global
		graphics
		gui
		main
		menu
		network
		sound
		steam
		steamshim
		type_instances
		types
		world)

	SET(GLEST_LIB_INCLUDE_ROOT "../shared_lib/include/")
	SET(GLEST_LIB_INCLUDE_DIRS
		${GLEST_LIB_INCLUDE_ROOT}compression
		${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)

	IF(WANT_USE_STREFLOP AND 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()

	INCLUDE_DIRECTORIES( ${GLEST_LIB_INCLUDE_DIRS} )

	#INCLUDE_DIRECTORIES( ${GLEST_LIB_INCLUDE_ROOT}platform/${SDL_VERSION_SNAME} )

	IF(WIN32)
		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)
		INCLUDE_DIRECTORIES( ${DIR} )

		set(SRC_DIR_TO_GLOB ${DIR})
		FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.cpp)
		SET(GLEST_SOURCE_FILES ${GLEST_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
		FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.c)
		SET(GLEST_SOURCE_FILES ${GLEST_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
		FILE(GLOB INCLUDE_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.h)
		SET(GLEST_INCLUDE_FILES ${GLEST_INCLUDE_FILES} ${INCLUDE_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(GLEST_SOURCE_FILES ${GLEST_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(GLEST_SOURCE_FILES ${GLEST_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
		ENDIF(APPLE)
	ENDFOREACH(DIR)

	IF(WIN32)
		SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-subsystem,console -mconsole")
	ENDIF()

	SET_SOURCE_FILES_PROPERTIES(${GLEST_INCLUDE_FILES} PROPERTIES HEADER_FILE_ONLY 1)

	ADD_EXECUTABLE(${TARGET_NAME} ${GLEST_SOURCE_FILES} ${GLEST_INCLUDE_FILES})

	SET(GLEST_STREFLOP "")
	IF(WANT_USE_STREFLOP AND NOT STREFLOP_FOUND)
		SET(GLEST_STREFLOP "streflop")
	ENDIF()

	if(WANT_USE_GoogleBreakpad)
		find_package( GoogleBreakpad )
		if(BREAKPAD_EXCEPTION_HANDLER_FOUND AND BREAKPAD_EXCEPTION_HANDLER_LIBRARIES)
			add_definitions(-DHAVE_GOOGLE_BREAKPAD)

			include_directories( ${BREAKPAD_EXCEPTION_HANDLER_INCLUDE_DIR} )
			SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${BREAKPAD_EXCEPTION_HANDLER_LIBRARIES})

		else()
			message(STATUS "** MESSAGE: Could not find GoogleBreakpad. Disabling GoogleBreakpad support.")
		endif()
	endif()

	IF(WIN32)
		TARGET_LINK_LIBRARIES(${TARGET_NAME} stdc++ gcc odbc32 wsock32 winspool winmm shell32 comctl32 ctl3d32  advapi32 wsock32 opengl32 glu32 ole32 oleaut32 uuid mingw32 ddraw dsound dxguid ws2_32 iphlpapi wsock32 libogg libvorbis libvorbisfile zlib jpeg libpng xerces-c2_8_0 OpenAL32 libcurl winmm gdi32 opengl32 glu32 ${SDL_VERSION_NAME} ${SDL_VERSION_NAME}main lua5.1 ${GLEST_STREFLOP} libglest stdc++ moldname mingwex msvcrt user32 kernel32)
	ENDIF()

	IF(NOT WIN32)
		IF(WANT_USE_STREFLOP AND NOT STREFLOP_FOUND)
			TARGET_LINK_LIBRARIES(${TARGET_NAME} ${GLEST_STREFLOP})
		ENDIF()
		TARGET_LINK_LIBRARIES(${TARGET_NAME} libglest)
	ENDIF()
	TARGET_LINK_LIBRARIES(${TARGET_NAME} ${EXTERNAL_LIBS})

	# Installation of the program
	INSTALL(TARGETS ${TARGET_NAME} DESTINATION "${INSTALL_DIR_BIN}")

	# Installation of the program manpage file
	INSTALL(FILES
	"${PROJECT_SOURCE_DIR}/man/${TARGET_NAME_MANPAGE}"
	DESTINATION ${INSTALL_DIR_MAN})

	IF(UNIX)
		INSTALL(PROGRAMS
			"${PROJECT_SOURCE_DIR}/mk/linux/start_gameserver"
			DESTINATION ${INSTALL_DIR_INI})
	ENDIF()
	INSTALL(FILES
		"${PROJECT_SOURCE_DIR}/mk/shared/glestkeys.ini"
		DESTINATION ${INSTALL_DIR_DATA})
	IF(WIN32)
		INSTALL(FILES
			"${PROJECT_SOURCE_DIR}/mk/windows/glest.ini"
			DESTINATION ${INSTALL_DIR_INI})
	ELSEIF(UNIX AND NOT APPLE AND NOT FLATPAK_INSTALL)
		INSTALL(FILES
			"${PROJECT_SOURCE_DIR}/mk/shared/glest.ini"
			DESTINATION ${INSTALL_DIR_INI})
    ELSEIF(UNIX AND NOT APPLE AND FLATPAK_INSTALL)
		INSTALL(FILES
			"${PROJECT_SOURCE_DIR}/mk/flatpak/glest.ini"
			DESTINATION ${INSTALL_DIR_INI})
	ELSEIF(UNIX AND APPLE)
		INSTALL(FILES
			"${PROJECT_SOURCE_DIR}/mk/shared/glest.ini"
			DESTINATION ${INSTALL_DIR_INI})
	ENDIF()

	IF(UNIX AND APPLE)
		INSTALL(FILES
			"${PROJECT_SOURCE_DIR}/mk/macos/bundle_resources/Glest.icns"
			DESTINATION ${INSTALL_DIR_ICON})
	ENDIF()

	# Installation of icon
	INSTALL(FILES
		"${PROJECT_SOURCE_DIR}/mk/shared/glest.ico"
		DESTINATION ${INSTALL_DIR_INI})

	# These files must be in the same directory as the glest binary
	# when "make install" is NOT used.
	#
	#
	add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
		COMMAND cp "${PROJECT_SOURCE_DIR}/mk/shared/glest.ini" "${CMAKE_BINARY_DIR}"
		COMMAND cp "${PROJECT_SOURCE_DIR}/mk/shared/glestkeys.ini" "${CMAKE_BINARY_DIR}"
		COMMAND cp "${PROJECT_SOURCE_DIR}/mk/shared/glest-dev.ini" "${CMAKE_BINARY_DIR}")

ENDIF()
