include (config.cmake)

macro(copy_files TARGET_NAME GLOBPAT SOURCE DESTINATION RECUR)
    IF(${RECUR})
        SET(RECURSE_PARAM GLOB_RECURSE)
    ELSEIF(NOT ${RECUR})
        SET(RECURSE_PARAM GLOB)
    ENDIF(${RECUR})
    file(${RECURSE_PARAM} COPY_FILES RELATIVE ${SOURCE} "${SOURCE}/${GLOBPAT}")

  add_custom_target(${TARGET_NAME} ALL COMMENT "Copying files: ${SOURCE}/${GLOBPAT}")

  foreach(FILENAME ${COPY_FILES})
    set(SRC "${SOURCE}/${FILENAME}")
    set(DST "${DESTINATION}/${FILENAME}")

    IF(IS_DIRECTORY ${SRC})
        add_custom_command(TARGET ${TARGET_NAME} COMMAND ${CMAKE_COMMAND} -E make_directory ${DST})
    ELSE(IS_DIRECTORY ${SRC})
        add_custom_command(TARGET ${TARGET_NAME} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SRC} ${DST})
    ENDIF(IS_DIRECTORY ${SRC})
  endforeach(FILENAME)
endmacro(copy_files)

macro(configure_files TARGET_NAME GLOBPAT SOURCE DESTINATION RECUR)
    IF(${RECUR})
        SET(RECURSE_PARAM GLOB_RECURSE)
    ELSEIF(NOT ${RECUR})
        SET(RECURSE_PARAM GLOB)
    ENDIF(${RECUR})
    file(${RECURSE_PARAM} CONFIGURE_FILES RELATIVE ${SOURCE} "${SOURCE}/${GLOBPAT}")

  add_custom_target(${TARGET_NAME} ALL COMMENT "Configuring files: ${SOURCE}/${GLOBPAT}")

  foreach(FILENAME ${CONFIGURE_FILES})
    set(SRC "${SOURCE}/${FILENAME}")
    set(DST "${DESTINATION}/${FILENAME}")

    IF(IS_DIRECTORY ${SRC})
        add_custom_command(TARGET ${TARGET_NAME} COMMAND ${CMAKE_COMMAND} -E make_directory ${DST})
    ELSE(IS_DIRECTORY ${SRC})
        configure_file(${SRC} ${DST} @ONLY)
    ENDIF(IS_DIRECTORY ${SRC})
  endforeach(FILENAME)
endmacro(configure_files)

macro(pixelboost_setup_library library)
	if (PIXELBOOST_BUILD_PLATFORM_OSX OR PIXELBOOST_BUILD_PLATFORM_IOS)
		set_target_properties(${library} PROPERTIES
			XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "gnu++0x"
			XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++"
			XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf-with-dsym"
			XCODE_ATTRIBUTE_COPY_PHASE_STRIP NO
			)

		if (PIXELBOOST_BUILD_PLATFORM_OSX)
			set_target_properties(${library} PROPERTIES
				XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET 10.7
				)
		endif (PIXELBOOST_BUILD_PLATFORM_OSX)

		if (PIXELBOOST_BUILD_PLATFORM_IOS)
			set_target_properties(${library} PROPERTIES
				XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET 5.0
				)
		endif (PIXELBOOST_BUILD_PLATFORM_IOS)
	endif (PIXELBOOST_BUILD_PLATFORM_OSX OR PIXELBOOST_BUILD_PLATFORM_IOS)
endmacro()

macro(pixelboost_setup_pre pixelboost_dir projectname identifier title)
	set (PIXELBOOST_ROOT_DIR "${CMAKE_SOURCE_DIR}/${pixelboost_dir}")
	set (PIXELBOOST_BUILD_NAME ${projectname})
	set (PIXELBOOST_BUILD_IDENTIFIER ${identifier})
	set (PIXELBOOST_BUILD_TITLE ${title})

	if (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)
		set (PIXELBOOST_LIBRARY_TYPE SHARED)
	else (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)
		set (PIXELBOOST_LIBRARY_TYPE STATIC)
	endif (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)

	add_definitions(-DPIXELBOOST_BUILD_NAME="${projectname}")
	add_definitions(-DPIXELBOOST_BUILD_IDENTIFIER="${identifier}")
	add_definitions(-DPIXELBOOST_BUILD_TITLE="${title}")

	option (PIXELBOOST_BUILD_PLATFORM_ANDROID "Build for Android Platform" FALSE)
	option (PIXELBOOST_BUILD_PLATFORM_IOS "Build for iOS Platform" FALSE)
	option (PIXELBOOST_BUILD_PLATFORM_OSX "Build for OSX Platform" FALSE)

	if (PIXELBOOST_BUILD_PLATFORM_OSX)
		add_definitions(-DPIXELBOOST_DISABLE_GAMECENTER
						-DPIXELBOOST_DISABLE_SOUND
						-DPIXELBOOST_PLATFORM_OSX)

		set(RESOURCES ${RESOURCES}
			${PIXELBOOST_DATA_DIRS})

		set(RESOURCES ${RESOURCES}
			${PIXELBOOST_ROOT_DIR}/engine/resources/platform/osx/MainView.xib
			${PIXELBOOST_ROOT_DIR}/engine/resources/platform/osx/MainMenu.xib
			)
	
		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ObjC")
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -x objective-c++")
	endif (PIXELBOOST_BUILD_PLATFORM_OSX)

	if (PIXELBOOST_BUILD_PLATFORM_IOS)
		add_definitions(-DPIXELBOOST_DISABLE_TESTFLIGHT
						-DPIXELBOOST_PLATFORM_IOS)

		set(RESOURCES ${RESOURCES}
			${PIXELBOOST_DATA_DIRS})

		set(RESOURCES ${RESOURCES}
			${PIXELBOOST_ROOT_DIR}/engine/resources/platform/ios/MainWindow.xib 
			${PIXELBOOST_ROOT_DIR}/engine/resources/platform/ios/MainWindow-iPad.xib 
			${PIXELBOOST_ROOT_DIR}/engine/resources/platform/ios/pixelboostViewController.xib
			${CMAKE_BINARY_DIR}/Default-568h@2x.png
			${CMAKE_BINARY_DIR}/Default-Landscape@2x~ipad.png
			${CMAKE_BINARY_DIR}/Default-Landscape~ipad.png
			${CMAKE_BINARY_DIR}/Default-Portrait@2x~ipad.png
			${CMAKE_BINARY_DIR}/Default-Portrait~ipad.png
			${CMAKE_BINARY_DIR}/Default.png
			${CMAKE_BINARY_DIR}/Default@2x.png
			${CMAKE_BINARY_DIR}/Icon-72.png
			${CMAKE_BINARY_DIR}/Icon-72@2x.png
			${CMAKE_BINARY_DIR}/Icon.png
			${CMAKE_BINARY_DIR}/Icon@2x.png
			)

		set(CMAKE_OSX_SYSROOT iphoneos)
		set(CMAKE_OSX_ARCHITECTURES "armv7;armv7s;i386")
		set(CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphoneos;-iphonesimulator")
		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ObjC")
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -x objective-c++")
	endif (PIXELBOOST_BUILD_PLATFORM_IOS)

	if (PIXELBOOST_BUILD_PLATFORM_ANDROID)
		add_definitions(-DPIXELBOOST_DISABLE_GAMECENTER
						-DPIXELBOOST_DISABLE_MONGOOSE
						-DPIXELBOOST_DISABLE_NETWORKING
						-DPIXELBOOST_PLATFORM_ANDROID
						-DPIXELBOOST_ANDROID_STORE_${PIXELBOOST_ANDROID_STORE}
						-D_GLIBCXX_HAS_GTHREADS)

		set(PIXELBOOST_ANDROID_STORE_${PIXELBOOST_ANDROID_STORE}_IFDEF "*/")
		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

		if((CMAKE_BUILD_TYPE STREQUAL "Debug") AND (NOT CMAKE_CONFIGURATION_TYPES))
			set(PIXELBOOST_ANDROID_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/build/android_debug")
			set(PIXELBOOST_ANDROID_MANIFEST "${PIXELBOOST_ROOT_DIR}/engine/resources/platform/android/AndroidManifest_debug.xml")
		else ()
			set(PIXELBOOST_ANDROID_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/build/android_release_${PIXELBOOST_ANDROID_TARGET}")
			set(PIXELBOOST_ANDROID_MANIFEST "${PIXELBOOST_ROOT_DIR}/engine/resources/platform/android/AndroidManifest_release.xml")
		endif ()

		configure_file("${PIXELBOOST_ROOT_DIR}/engine/resources/platform/android/custom_rules.xml" "${PIXELBOOST_ANDROID_BUILD_DIR}/custom_rules.xml" @ONLY)
		configure_file("${PIXELBOOST_ANDROID_MANIFEST}" "${PIXELBOOST_ANDROID_BUILD_DIR}/AndroidManifest.xml")
		configure_file("${PIXELBOOST_ROOT_DIR}/engine/resources/platform/android/res/values/strings.xml" "${PIXELBOOST_ANDROID_BUILD_DIR}/res/values/strings.xml")
	endif (PIXELBOOST_BUILD_PLATFORM_ANDROID)

	if (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)
		add_definitions(-DPIXELBOOST_DISABLE_MONGOOSE
						-DPIXELBOOST_DISABLE_NETWORKING
						-DPIXELBOOST_DISABLE_THREADING
						-DPIXELBOOST_PLATFORM_EMSCRIPTEN)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --preload-file data -s TOTAL_MEMORY=134217728")
		set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -s ASM_JS=1")
	endif (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)

	if (PIXELBOOST_BUILD_PLATFORM_WINDOWS)
		add_definitions(-DPIXELBOOST_DISABLE_CONSTEXPR)
	endif (PIXELBOOST_BUILD_PLATFORM_WINDOWS)

	add_subdirectory (${PIXELBOOST_ROOT_DIR} ${CMAKE_BINARY_DIR}/pixelboost)

	include_directories("${PIXELBOOST_HEADERS}")

	file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/data")
endmacro()

macro(pixelboost_setup_post sources)
	if (PIXELBOOST_BUILD_PLATFORM_ANDROID)
		add_library(pixelboost SHARED ${sources}
			"${PIXELBOOST_ROOT_DIR}/engine/src/platform/android/pixelboost/framework/main.cpp")
		set (PIXELBOOST_EXE_NAME pixelboost)

		copy_files (${PIXELBOOST_EXE_NAME}_CopyResources * ${CMAKE_SOURCE_DIR}/data ${CMAKE_BINARY_DIR}/assets/data 1)
		add_dependencies(${PIXELBOOST_EXE_NAME} ${PIXELBOOST_EXE_NAME}_CopyResources)
	else (PIXELBOOST_BUILD_PLATFORM_ANDROID)
		if (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)
			add_executable(${PIXELBOOST_BUILD_NAME}.html ${sources} "${PIXELBOOST_ROOT_DIR}/engine/src/platform/emscripten/pixelboost/framework/main.cpp")
			set (PIXELBOOST_EXE_NAME ${PIXELBOOST_BUILD_NAME}.html)
			target_link_libraries (${PIXELBOOST_EXE_NAME} pthread)
		else (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)
			add_executable(${PIXELBOOST_BUILD_NAME} ${sources} ${RESOURCES})
			set (PIXELBOOST_EXE_NAME ${PIXELBOOST_BUILD_NAME})
		endif (PIXELBOOST_BUILD_PLATFORM_EMSCRIPTEN)
	endif (PIXELBOOST_BUILD_PLATFORM_ANDROID)

	pixelboost_setup_library(${PIXELBOOST_EXE_NAME})
	target_link_libraries (${PIXELBOOST_EXE_NAME} pixelboost_engine pixelboost_shared enet freetype2 lua)

	if (PIXELBOOST_LIBRARY_TYPE STREQUAL "STATIC")
		target_link_libraries (${PIXELBOOST_EXE_NAME} Box2D physfs-static)
	else (PIXELBOOST_LIBRARY_TYPE STREQUAL "STATIC")
		target_link_libraries (${PIXELBOOST_EXE_NAME} Box2D_shared physfs)
	endif (PIXELBOOST_LIBRARY_TYPE STREQUAL "STATIC")

	if (PIXELBOOST_BUILD_PLATFORM_OSX)
		set_target_properties(${PIXELBOOST_EXE_NAME} PROPERTIES
			MACOSX_BUNDLE_GUI_IDENTIFIER ${PIXELBOOST_BUILD_IDENTIFIER}
			MACOSX_BUNDLE_INFO_PLIST "${PIXELBOOST_ROOT_DIR}/engine/resources/platform/osx/Info.plist.in"
			MACOSX_BUNDLE TRUE
			RESOURCE "${RESOURCES}"
			XCODE_ATTRIBUTE_COMBINE_HIDPI_IMAGES YES
			XCODE_ATTRIBUTE_INFOPLIST_PREPROCESS YES
			XCODE_ATTRIBUTE_INSTALL_PATH "$(LOCAL_APPS_DIR)"
			XCODE_ATTRIBUTE_SKIP_INSTALL "No"
		)
	endif (PIXELBOOST_BUILD_PLATFORM_OSX)

	if (PIXELBOOST_BUILD_PLATFORM_IOS)
		set_target_properties(${PIXELBOOST_EXE_NAME} PROPERTIES
			MACOSX_BUNDLE_GUI_IDENTIFIER ${PIXELBOOST_BUILD_IDENTIFIER}
			MACOSX_BUNDLE_INFO_PLIST "${PIXELBOOST_ROOT_DIR}/engine/resources/platform/ios/Info.plist.in"
			MACOSX_BUNDLE TRUE
			RESOURCE "${RESOURCES}"
			XCODE_ATTRIBUTE_INFOPLIST_PREPROCESS YES
			XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer"
			XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET 5.0
			XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2" # Universal app
			XCODE_ATTRIBUTE_INSTALL_PATH "$(LOCAL_APPS_DIR)"
			XCODE_ATTRIBUTE_SKIP_INSTALL "No"
		)
	endif (PIXELBOOST_BUILD_PLATFORM_IOS)
endmacro()
