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

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

IF(BUILD_MEGAGLEST)
	MESSAGE(STATUS "Will try to build MegaGlest game")

	IF(WIN32)
		SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH}
				       ${PROJECT_SOURCE_DIR}/source/win32_deps/lib
				       ${PROJECT_SOURCE_DIR}/source/win32_deps/xerces-c-src_2_8_0/lib)

		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/openal-soft-1.12.854/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/openal-soft-1.12.854)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/xerces-c-src_2_8_0/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/libogg-1.2.1/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/lua-5.1/src)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/jpeg-8b)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/lpng141)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/zlib-1.2.5)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/curl-7.21.3/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/${SDL_WINDOWS_DIR_DINC}/include)
				       
		link_directories(${PROJECT_SOURCE_DIR}/source/win32_deps/lib)
		link_directories(${PROJECT_SOURCE_DIR}/source/win32_deps/xerces-c-src_2_8_0/lib)
	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()

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

	FIND_PACKAGE(LUA REQUIRED)
	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)
		FIND_PACKAGE(OpenSSL)
		IF(OPENSSL_FOUND)
		    INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
		ELSE()
		    MESSAGE(STATUS "**NOTE: OpenSSL is usually not required.")
		ENDIF()
	ENDIF()

	FIND_PACKAGE(CURL REQUIRED)
	INCLUDE(${CMAKE_SOURCE_DIR}/mk/cmake/Modules/CurlGetConfig.cmake)
	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()

	FIND_PACKAGE(GLEW REQUIRED)
	INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIRS})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${GLEW_LIBRARIES})
	ENDIF()

	IF(VLC_MIN_VERSION_MG)
		SET(LIBVLC_MIN_VERSION "${VLC_MIN_VERSION_MG}")
	ELSE()
		SET(LIBVLC_MIN_VERSION "1.1.0")
	ENDIF()
	IF(WANT_USE_VLC)
		FIND_PACKAGE(LIBVLC)
	ENDIF()
	IF(LIBVLC_FOUND AND NOT LIBVLC_VERSION STRLESS "${LIBVLC_MIN_VERSION}")
		MESSAGE(STATUS "**NOTE: LIBVLC found so intro videos are supported.")
		INCLUDE_DIRECTORIES(${LIBVLC_INCLUDE_DIR})
		IF(UNIX)
			SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${LIBVLCCORE_LIBRARY} ${LIBVLC_LIBRARY})
			ADD_DEFINITIONS(-DHAS_LIBVLC)
		ENDIF()
		IF(LIBVLC_VERSION_PRE_V2)
			ADD_DEFINITIONS(-DLIBVLC_VERSION_PRE_2)
		ENDIF()
		IF(LIBVLC_VERSION_PRE_1_1_13)
			ADD_DEFINITIONS(-DLIBVLC_VERSION_PRE_1_1_13)
		ENDIF()
	ELSE()
		IF(WANT_USE_VLC)
			MESSAGE(STATUS "**WARNING: LIBVLC NOT found so videos are NOT supported.")
		ELSE()
			MESSAGE(STATUS "LIBVLC disabled so videos are NOT supported.")
		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 game

	SET(DIRS_WITH_SRC
	   	ai
		facilities
		game
		global
		graphics
		gui
		main
		menu
		network
		sound
        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/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()

	SET(MG_SOURCE_FILES "")
	
	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(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
		FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.c)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
		FILE(GLOB INCLUDE_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.h)
		SET(MG_INCLUDE_FILES ${MG_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(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)

	# Force the git revision stamp (${GIT_COMMIT_COUNT}.${GIT_SHA1}) file to be touched so the compile stamp is always right
	IF(UNIX AND GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git/")
		add_custom_command(OUTPUT mg.tmp
			COMMAND touch ${PROJECT_SOURCE_DIR}/source/glest_game/facilities/game_util.cpp)
		add_custom_target(run ALL DEPENDS mg.tmp)
	ENDIF()

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

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

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

	SET(MG_STREFLOP "")
	IF(WANT_USE_STREFLOP AND NOT STREFLOP_FOUND)
		SET(MG_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 "** WARNING: 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 ${MG_STREFLOP} libmegaglest stdc++ moldname mingwex msvcrt user32 kernel32)
	ENDIF()
	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})

        SET(HELP2MAN_OUT_PATH ${EXECUTABLE_OUTPUT_PATH})
        IF("${EXECUTABLE_OUTPUT_PATH}" STREQUAL "")
                SET(HELP2MAN_OUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/")
        ENDIF()

	IF(EXISTS ${HELP2MAN})
		MESSAGE(STATUS "***-- Found help2man: ${HELP2MAN} about to build manpage in [${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE}]")

                SET(MG_MANPAGE_DESCRIPTION "3D multi-player real time strategy game")

		IF(EXISTS ${XVFB_EXEC})
			MESSAGE(STATUS "***-- Found xvfb-run: ${XVFB_EXEC} about to build manpage with it.")

			add_custom_target(${TARGET_NAME_MANPAGE} ALL
				COMMAND ${XVFB_EXEC} --auto-servernum --server-num=770 ${HELP2MAN} --name="${MG_MANPAGE_DESCRIPTION}" --section=6 -N -o ${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE} ${HELP2MAN_OUT_PATH}${TARGET_NAME}
			DEPENDS ${TARGET_NAME})
		ELSE()
			add_custom_target(${TARGET_NAME_MANPAGE} ALL
				COMMAND ${HELP2MAN} --name="${MG_MANPAGE_DESCRIPTION}" --section=6 -N -o ${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE} ${HELP2MAN_OUT_PATH}${TARGET_NAME}
			DEPENDS ${TARGET_NAME})
		ENDIF()
	ENDIF()

        # Requires an install prefix for the items below to work
        IF(NOT CMAKE_INSTALL_PREFIX STREQUAL "")
	        MESSAGE(STATUS "**Source package INSTALL TRIGGERED [${PROJECT_SOURCE_DIR}]")	

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

	        IF(EXISTS ${HELP2MAN} AND NOT "${MEGAGLEST_MANPAGE_INSTALL_PATH}" STREQUAL "")
         		MESSAGE(STATUS "**NOTE: Will Install manpage [${TARGET_NAME_MANPAGE}] to [${MEGAGLEST_MANPAGE_INSTALL_PATH}]")	

		        # Installation of the program manpage file
		        INSTALL(FILES 
		          "${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE}"
		          DESTINATION ${MEGAGLEST_MANPAGE_INSTALL_PATH})
	        ENDIF()

		IF(UNIX)
			INSTALL(PROGRAMS
				"${PROJECT_SOURCE_DIR}/mk/linux/start_megaglest_gameserver"
				DESTINATION ${MEGAGLEST_INI_INSTALL_PATH})
		ENDIF()
		INSTALL(FILES
			"${PROJECT_SOURCE_DIR}/mk/shared/glestkeys.ini"
			"${PROJECT_SOURCE_DIR}/mk/shared/servers.ini"
			DESTINATION ${MEGAGLEST_DATA_INSTALL_PATH})
		IF(WIN32)
			INSTALL(FILES
				"${PROJECT_SOURCE_DIR}/mk/windoze/glest.ini"
				DESTINATION ${MEGAGLEST_INI_INSTALL_PATH})
		ELSEIF(UNIX AND NOT APPLE)
			INSTALL(FILES
				"${PROJECT_SOURCE_DIR}/mk/linux/glest.ini"
				DESTINATION ${MEGAGLEST_INI_INSTALL_PATH})
		ELSEIF(UNIX AND APPLE)
			INSTALL(FILES
				"${PROJECT_SOURCE_DIR}/mk/macos/glest.ini"
				DESTINATION ${MEGAGLEST_INI_INSTALL_PATH})
			IF(WANT_SINGLE_INSTALL_DIRECTORY AND EXISTS "${PROJECT_SOURCE_DIR}/mk/macos/p7zip/")
				INSTALL(DIRECTORY
					"${PROJECT_SOURCE_DIR}/mk/macos/p7zip"
					DESTINATION ${MEGAGLEST_DATA_INSTALL_PATH})
			ENDIF()
			IF(WANT_SINGLE_INSTALL_DIRECTORY AND EXISTS "${PROJECT_SOURCE_DIR}/mk/macos/lib/")
				INSTALL(DIRECTORY
					"${PROJECT_SOURCE_DIR}/mk/macos/lib/"
					DESTINATION "${MEGAGLEST_DATA_INSTALL_PATH}/../../Frameworks/")
			ENDIF()
		ENDIF()

		INSTALL(FILES
			"${PROJECT_SOURCE_DIR}/mk/shared/megaglest.ico"
			DESTINATION ${MEGAGLEST_INI_INSTALL_PATH})
		IF(UNIX AND APPLE AND NOT WANT_SINGLE_INSTALL_DIRECTORY)
			INSTALL(FILES
				"${PROJECT_SOURCE_DIR}/mk/macos/bundle_resources/MegaGlest.icns"
				DESTINATION ${MEGAGLEST_ICON_INSTALL_PATH})
		ENDIF()
	ENDIF()
ENDIF()
