set(ANDROID_NDK_ROOT ${ANDROID_NDK})

if (VERBOSE)
	set(ANT_FLAGS -v)
	set(ZIPALIGN_FLAGS -v)
	set(KEYTOOL_FLAGS -v)
	set(ANDROID_TOOL_FLAGS "${ANDROID_TOOL_FLAGS} -v")
endif()
if (GRADLE)
	set(ANDROID_TOOL_FLAGS "${ANDROID_TOOL_FLAGS} -g")
endif()

if (NOT ANDROID_NDK_ROOT)
	message(FATAL_ERROR "ANDROID_NDK environment variable is not set")
endif()
message(STATUS "Android NDK root: ${ANDROID_NDK_ROOT}")

if (RELEASE)
	set(ANT_TARGET release)
	set(ANT_INSTALL_TARGET installr)
	set(MANIFEST_DEBUGGABLE false)
else()
	set(ANT_TARGET debug)
	set(ANT_INSTALL_TARGET installd)
	set(MANIFEST_DEBUGGABLE true)
endif()

# find the ant tool
find_host_program(ANDROID_ANT ant)
if (ANDROID_ANT)
	message(STATUS "ant found")
else()
	message(FATAL_ERROR "ant not found in path!")
endif()

if (GRADLE)
	find_host_program(ANDROID_GRADLE gradle)
	if (ANDROID_GRADLE)
		message(STATUS "gradle found")
	else()
		message(FATAL_ERROR "gradle not found in path!")
	endif()
endif()

find_host_program(JARSIGNER "jarsigner")
find_host_program(ANDROID_NDK_STACK "ndk-stack" HINTS ${ANDROID_NDK_ROOT})

find_host_program(ANDROID_ADB adb)
if (ANDROID_ADB)
	message(STATUS "adb tool found")
else()
	message(FATAL_ERROR "adb tool not found in path!")
endif()

set(DEBUG_KEYSTORES "$ENV{HOME}/.android/debug.keystore" "$ENV{HOMEPATH}/.android/debug.keystore")
foreach(DEBUG_KEYSTORE ${DEBUG_KEYSTORES})
	if (EXISTS ${DEBUG_KEYSTORE})
		set(ANDROID_DEBUG_KEYSTORE ${DEBUG_KEYSTORE})
	endif()
endforeach()

set(ANDROID_DEBUG_ALIAS androiddebugkey)
set(ANDROID_DEBUG_STOREPASS android)
set(ANDROID_DEBUG_KEYPASS android)
if ("${ANDROID_DEBUG_KEYSTORE}" STREQUAL "")
	find_host_program(KEYTOOL keytool)
	if (KEYTOOL)
		message(STATUS "keytool found (${KEYTOOL})- generate debug keystore at $ENV{HOME}/.android with storepass ${ANDROID_DEBUG_STOREPASS}, alias ${ANDROID_DEBUG_ALIAS}, keypass ${ANDROID_DEBUG_KEYPASS}")
		file(MAKE_DIRECTORY $ENV{HOME}/.android)
		execute_process(COMMAND ${KEYTOOL} -genkeypair ${KEYTOOL_FLAGS} -keystore $ENV{HOME}/.android/debug.keystore -storepass ${ANDROID_DEBUG_STOREPASS} -alias ${ANDROID_DEBUG_ALIAS} -keypass ${ANDROID_DEBUG_KEYPASS} -dname "CN=Android Debug,O=Android,C=US" RESULT_VARIABLE exitcode)
		execute_process(COMMAND ${KEYTOOL} --help)
		message(STATUS "keytool exited with ${exitcode}")
		set(DEBUG_KEYSTORE "$ENV{HOME}/.android/debug.keystore")
		if (NOT EXISTS ${DEBUG_KEYSTORE})
			message(FATAL_ERROR "Could not create debug keystore")
		else()
			set(ANDROID_DEBUG_KEYSTORE ${DEBUG_KEYSTORE})
		endif()
	else()
		message(FATAL_ERROR "keytool not found in path! no debug keystore found")
	endif()
else()
	message(STATUS "Found android debug keystore at ${ANDROID_DEBUG_KEYSTORE}")
endif()

set(ANDROID_RELEASE_KEYSTORE $ENV{SHERVANATOR_KEYSTORE_PATH})
set(ANDROID_RELEASE_ALIAS Shervanator)
set(ANDROID_RELEASE_STOREPASS $ENV{SHERVANATOR_KEYSTORE_PASSWD})
set(ANDROID_RELEASE_KEYPASS $ENV{SHERVANATOR_ALIAS_PASSWD})

if (RELEASE)
	if (NOT DEFINED ENV{SHERVANATOR_KEYSTORE_PASSWD})
		message(FATAL_ERROR "No keystore password set")
	endif()
	if (NOT DEFINED ENV{SHERVANATOR_KEYSTORE_PATH})
		message(FATAL_ERROR "No keystore path set")
	endif()
	set(ANDROID_KEYSTORE ${ANDROID_RELEASE_KEYSTORE})
	set(ANDROID_ALIAS ${ANDROID_RELEASE_ALIAS})
	set(ANDROID_STOREPASS ${ANDROID_RELEASE_STOREPASS})
	set(ANDROID_KEYPASS ${ANDROID_RELEASE_KEYPASS})
else()
	set(ANDROID_KEYSTORE ${ANDROID_DEBUG_KEYSTORE})
	set(ANDROID_ALIAS ${ANDROID_DEBUG_ALIAS})
	set(ANDROID_STOREPASS ${ANDROID_DEBUG_STOREPASS})
	set(ANDROID_KEYPASS ${ANDROID_DEBUG_KEYPASS})
endif()

add_definitions(-DGL_GLEXT_PROTOTYPES)

set(CMAKE_C_STANDARD_LIBRARIES "-landroid -llog -lGLESv1_CM -lGLESv2")
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES}")
