cmake_minimum_required(VERSION 3.22.1)
project(critterfolio_android LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(ExternalProject)

set(DEPS_INSTALL ${CMAKE_BINARY_DIR}/deps/install)

# ExternalProject_Add spawns a completely separate CMake invocation for each
# dependency, so none of AGP's Android toolchain settings carry over
# automatically - we have to forward them explicitly to every sub-build.
set(ANDROID_TOOLCHAIN_ARGS
  -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
  -DANDROID_ABI=${ANDROID_ABI}
  -DANDROID_PLATFORM=${ANDROID_PLATFORM}
  -DANDROID_STL=c++_shared
  -DCMAKE_BUILD_TYPE=Release
  -DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL}
  -DCMAKE_PREFIX_PATH=${DEPS_INSTALL}
  -DBUILD_SHARED_LIBS=OFF
  -DCMAKE_CXX_STANDARD=17
  -DCMAKE_CXX_STANDARD_REQUIRED=ON
  -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH
  -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH
  -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH
)

find_package(SDL3 REQUIRED CONFIG)
find_package(SDL3_image REQUIRED CONFIG)

ExternalProject_Add(freetype_ext
  GIT_REPOSITORY https://github.com/freetype/freetype.git
  GIT_TAG VER-2-13-3
  CMAKE_ARGS ${ANDROID_TOOLCHAIN_ARGS}
    -DFT_DISABLE_ZLIB=TRUE
    -DFT_DISABLE_BZIP2=TRUE
    -DFT_DISABLE_PNG=TRUE
    -DFT_DISABLE_HARFBUZZ=TRUE
    -DFT_DISABLE_BROTLI=TRUE
)

ExternalProject_Add(lunasvg_ext
  GIT_REPOSITORY https://github.com/sammycage/lunasvg.git
  GIT_TAG v3.2.1
  CMAKE_ARGS ${ANDROID_TOOLCHAIN_ARGS}
  DEPENDS freetype_ext
)

# Lua ships no official CMakeLists, and we don't want its standalone lua/luac
# CLI targets (those need readline). We compile just the library sources.
ExternalProject_Add(lua_ext
  GIT_REPOSITORY https://github.com/lua/lua.git
  GIT_TAG v5.4.8
  CONFIGURE_COMMAND ""
  BUILD_IN_SOURCE 1
  BUILD_COMMAND sh -c "${CMAKE_C_COMPILER} --target=${CMAKE_C_COMPILER_TARGET} --sysroot=${CMAKE_SYSROOT} -c -O2 -fPIC -DLUA_USE_POSIX $(ls *.c | grep -v -e '^lua.c$' -e '^luac.c$') && ${CMAKE_AR} rcs liblua.a *.o"
  INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${DEPS_INSTALL}/lib ${DEPS_INSTALL}/include
    COMMAND ${CMAKE_COMMAND} -E copy liblua.a ${DEPS_INSTALL}/lib/
    COMMAND ${CMAKE_COMMAND} -E copy lua.h lauxlib.h lualib.h luaconf.h ${DEPS_INSTALL}/include/
)

ExternalProject_Add(sqlitecpp_ext
  GIT_REPOSITORY https://github.com/SRombauts/SQLiteCpp.git
  GIT_TAG 3.3.3
  CMAKE_ARGS ${ANDROID_TOOLCHAIN_ARGS}
    -DSQLITECPP_INTERNAL_SQLITE=ON
    -DSQLITECPP_RUN_CPPLINT=OFF
    -DSQLITECPP_RUN_CPPCHECK=OFF
)

ExternalProject_Add(toml11_ext
  GIT_REPOSITORY https://github.com/ToruNiina/toml11.git
  GIT_TAG v3.8.1
  CMAKE_ARGS ${ANDROID_TOOLCHAIN_ARGS}
)

ExternalProject_Add(rmlui_ext
  GIT_REPOSITORY https://github.com/mikke89/RmlUi.git
  GIT_TAG master
  CMAKE_ARGS ${ANDROID_TOOLCHAIN_ARGS}
    -DFreetype_ROOT=${DEPS_INSTALL}
    -DRMLUI_LUA_BINDINGS=ON
    -DRMLUI_LUA_BINDINGS_LIBRARY=lua
    -DRMLUI_SVG_PLUGIN=ON
    -DRMLUI_SAMPLES=OFF
    -DBUILD_TESTING=OFF
  DEPENDS freetype_ext lunasvg_ext lua_ext
)

set(PROJECT_SRC_DIR ${CMAKE_SOURCE_DIR}/../../src)
set(RMLUI_BACKEND_DIR ${CMAKE_SOURCE_DIR}/../../vendor/rmlui_backend)

add_library(critterfolio SHARED
  ${PROJECT_SRC_DIR}/main.cpp
  ${PROJECT_SRC_DIR}/database.cpp
  ${PROJECT_SRC_DIR}/ui.cpp
  ${PROJECT_SRC_DIR}/image_utils.cpp
  ${RMLUI_BACKEND_DIR}/RmlUi_Backend_SDL_SDLrenderer.cpp
  ${RMLUI_BACKEND_DIR}/RmlUi_Platform_SDL.cpp
  ${RMLUI_BACKEND_DIR}/RmlUi_Renderer_SDL.cpp
)

add_dependencies(critterfolio freetype_ext lunasvg_ext lua_ext sqlitecpp_ext toml11_ext rmlui_ext)

target_include_directories(critterfolio PRIVATE
  ${PROJECT_SRC_DIR}
  ${RMLUI_BACKEND_DIR}
  ${DEPS_INSTALL}/include
)

target_compile_definitions(critterfolio PRIVATE
  RMLUI_SDL_VERSION_MAJOR=3
  RMLUI_STATIC_LIB
)

# Linked by explicit path rather than find_package(), since find_package()
# runs during THIS configure pass - before any ExternalProject_Add step
# above has actually built or installed anything (those only run later,
# during the actual build). This is what the superbuild pattern is for.
target_link_directories(critterfolio PRIVATE ${DEPS_INSTALL}/lib)

target_link_libraries(critterfolio PRIVATE
  SDL3::SDL3
  SDL3_image::SDL3_image
  rmlui
  rmlui_debugger
  rmlui_lua
  lua
  freetype
  lunasvg
  plutovg
  SQLiteCpp
  sqlite3
)

target_link_options(critterfolio PRIVATE "-Wl,-u,SDL_main")
