include(ExternalProject)

set(ANDROID_SDK_ROOT $ENV{ANDROID_SDK_ROOT} CACHE PATH "Path to Android SDK")

set(ANDROID_PLATFORM_TOOLS_DIR "${ANDROID_SDK_ROOT}/platform-tools")

file(GLOB ANDROID_BUILD_TOOLS_VERSIONS "${ANDROID_SDK_ROOT}/build-tools/*")
list(POP_BACK ANDROID_BUILD_TOOLS_VERSIONS ANDROID_BUILD_TOOLS_LATEST)
set(ANDROID_BUILD_TOOLS_DIR "${ANDROID_BUILD_TOOLS_LATEST}" CACHE PATH "Path to Android build tools (i.e. where to find aapt2, etc)")

file(GLOB ANDROID_NDK_VERSIONS "${ANDROID_SDK_ROOT}/ndk/*")
list(POP_BACK ANDROID_NDK_VERSIONS ANDROID_NDK_LATEST)
set(ANDROID_NDK "${ANDROID_NDK_LATEST}" CACHE PATH "Path to Android NDK")

file(GLOB ANDROID_JAR_VERSIONS "${ANDROID_SDK_ROOT}/platforms/*")
list(POP_BACK ANDROID_JAR_VERSIONS ANDROID_JAR_LATEST)
set(ANDROID_JAR "${ANDROID_JAR_LATEST}/android.jar" CACHE PATH "Path to Android JAR")

set(LCMS_SRC_DIR "${LCMS_SRC_DIR}" CACHE PATH "Path to LCMS source")
set(FREETYPE_SRC_DIR "${FREETYPE_SRC_DIR}" CACHE PATH "Path to Freetype source")
set(PNG_SRC_DIR "${PNG_SRC_DIR_SRC_DIR}" CACHE PATH "Path to PNG source")
set(LAGUI_SRC_DIR "${LAGUI_SRC_DIR}" CACHE PATH "Path to LaGUI source")
set(EXECUTABLE_NAME "${EXECUTABLE_NAME}" CACHE STRING "Executable name")
set(EXECUTABLE_SRC_DIR "${EXECUTABLE_SRC_DIR}" CACHE PATH "Executable source")
set(ANDROID_NATIVE_API_LEVEL 24 CACHE STRING "Api level")

find_program(ADB       NAMES adb       REQUIRED PATHS ${ANDROID_PLATFORM_TOOLS_DIR})
find_program(AAPT2     NAMES aapt2     REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
find_program(APKSIGNER NAMES apksigner REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
find_program(ZIPALIGN  NAMES zipalign  REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})

set(APK_CONTENTS_ROOT "${CMAKE_CURRENT_BINARY_DIR}/apk")

macro(setup_variant VARIANT)

  ExternalProject_Add(freetype-${VARIANT}
    SOURCE_DIR ${FREETYPE_SRC_DIR}
    #EXCLUDE_FROM_ALL TRUE
    CMAKE_ARGS
    -DANDROID_ABI=${VARIANT}
    -DANDROID_NDK=${ANDROID_NDK}
    -DANDROID_STL=c++_static
    -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
    #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
    BUILD_ALWAYS True
  )

  ExternalProject_Add(png-${VARIANT}
    SOURCE_DIR ${PNG_SRC_DIR}
    #EXCLUDE_FROM_ALL TRUE
    CMAKE_ARGS
    -DANDROID_ABI=${VARIANT}
    -DANDROID_NDK=${ANDROID_NDK}
    -DANDROID_STL=c++_static
    -DPNG_SHARED=OFF
    -DHAVE_LD_VERSION_SCRIPT=OFF
    -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
    #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
    BUILD_ALWAYS True
  )

  ExternalProject_Add(lcms2-${VARIANT}
    SOURCE_DIR ${LCMS_SRC_DIR}
    #EXCLUDE_FROM_ALL TRUE
    CMAKE_ARGS
    -DANDROID_ABI=${VARIANT}
    -DANDROID_NDK=${ANDROID_NDK}
    -DANDROID_STL=c++_static
    -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
    #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
    BUILD_ALWAYS True
  )

  ExternalProject_Add(lagui-${VARIANT}
    SOURCE_DIR ${LAGUI_SRC_DIR}
    #EXCLUDE_FROM_ALL TRUE
    CMAKE_ARGS
    -DANDROID_ABI=${VARIANT}
    -DANDROID_NDK=${ANDROID_NDK}
    -DANDROID_STL=c++_static
    -DLAGUI_IGNORE_PACKAGES=True
    -DLAGUI_USE_GLES=True
    -DLAGUI_USE_PNG=True
    -DLAGUI_ANDROID=True
    -DLAGUI_USE_LUAJIT=False
    -DFREETYPE_SRC_DIR=${FREETYPE_SRC_DIR}
    -DPNG_SRC_DIR=${PNG_SRC_DIR}
    -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
    #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
    BUILD_ALWAYS True
  )

  ExternalProject_Add(${EXECUTABLE_NAME}-${VARIANT}
    SOURCE_DIR ${EXECUTABLE_SRC_DIR}
    #EXCLUDE_FROM_ALL TRUE
    CMAKE_ARGS
    -DANDROID_ABI=${VARIANT}
    -DANDROID_NDK=${ANDROID_NDK}
    -DANDROID_STL=c++_static
    -DLAGUI_SRC_DIR=${LAGUI_SRC_DIR}
    -DLAGUI_USE_GLES=True
    -DLAGUI_USE_LUAJIT=False
    -DLAGUI_USE_PNG=True
    -DLAGUI_ANDROID=True
    -DFREETYPE_SRC_DIR=${FREETYPE_SRC_DIR}
    -DLAGUI_SRC_DIR=${LAGUI_SRC_DIR}
    -DPNG_SRC_DIR=${PNG_SRC_DIR}
    -DLCMS_SRC_DIR=${LCMS_SRC_DIR}
    -DANDROID_COMPILE=True
    -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
    -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
    -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
    BUILD_ALWAYS True
    )

endmacro()
setup_variant(arm64-v8a)
#setup_variant(armeabi-v7a)
#setup_variant(x86)
setup_variant(x86_64)

set(MANIFEST "${CMAKE_CURRENT_SOURCE_DIR}/android/src/AndroidManifest.xml")
set(RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/android/res")
set(KEYSTORE "${CMAKE_CURRENT_SOURCE_DIR}/android/keystore.jks")

set(VALUES_STRING "${CMAKE_CURRENT_BINARY_DIR}/values_strings.arsc.flat")

include(CMakePrintHelpers)
cmake_print_variables(CMAKE_CURRENT_SOURCE_DIR)
cmake_print_variables(CMAKE_CURRENT_BINARY_DIR)


set(FINAL_APK "${CMAKE_CURRENT_BINARY_DIR}/app.apk")
set(RESOURCES_APK "${CMAKE_CURRENT_BINARY_DIR}/app.res.apk")
set(UNALIGNED_APK "${CMAKE_CURRENT_BINARY_DIR}/app.unaligned.apk")

# keytool -genkeypair -keystore keystore.jks -alias androidkey -validity 10000 -keyalg RSA -keysize 2048 -storepass android -keypass android

add_custom_command(
  OUTPUT ${VALUES_STRING}
  COMMAND ${AAPT2} compile ${RESOURCES}/values/strings.xml -o ${CMAKE_CURRENT_BINARY_DIR}
  DEPENDS ${RESOURCES}/values/strings.xml
  )
add_custom_command(
  OUTPUT ${RESOURCES_APK}
  COMMAND ${AAPT2} link ${VALUES_STRING} -o ${RESOURCES_APK} --manifest ${MANIFEST} -I ${ANDROID_JAR}
  DEPENDS ${MANIFEST} ${VALUES_STRING}
  )
add_custom_command(
  OUTPUT ${UNALIGNED_APK}
  COMMAND ${CMAKE_COMMAND} -E tar x ${RESOURCES_APK}
  COMMAND ${CMAKE_COMMAND} -E tar c ${UNALIGNED_APK} --format=zip .
  WORKING_DIRECTORY ${APK_CONTENTS_ROOT}
  DEPENDS
    ${RESOURCES_APK}
    #${EXECUTABLE_NAME}-armeabi-v7a
    ${EXECUTABLE_NAME}-arm64-v8a
    #${EXECUTABLE_NAME}-x86
    ${EXECUTABLE_NAME}-x86_64
    freetype-x86_64
    #freetype-x86
    freetype-arm64-v8a
    #freetype-armeabi-v7a
    png-x86_64
    png-arm64-v8a
  )
add_custom_command(
  OUTPUT ${FINAL_APK}
  COMMAND ${ZIPALIGN} -p -f -v 4 ${UNALIGNED_APK} ${FINAL_APK}
  COMMAND ${APKSIGNER} sign -ks ${KEYSTORE} --ks-key-alias androidkey --ks-pass pass:android --key-pass pass:android ${FINAL_APK}
  DEPENDS ${UNALIGNED_APK}
  )

add_custom_target(apk ALL DEPENDS ${FINAL_APK})

install(CODE "execute_process(COMMAND ${ADB} install ${FINAL_APK})")
