cmake_minimum_required(VERSION 3.1)

project(mcpelauncher-ui-qt LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# protobuf now requires c++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt6 COMPONENTS Core Quick Concurrent Svg REQUIRED)
find_package(OpenSSL)

option(ENABLE_UPDATE_CHECK "Enables update checking with a remote server")
set(UPDATE_CHECK_URL "" CACHE STRING "Specify the URL for the server to check for updates on")
set(UPDATE_CHECK_BUILD_ID "" CACHE STRING "Specify the numeral version number for the current build")
set(LAUNCHER_VERSIONDB_URL "" CACHE STRING "Specify the remote url of the versiondb to use")
set(LAUNCHER_VERSIONDB_PATH "" CACHE STRING "Specify a local versiondb checkout to embed into this binary")
set(LAUNCHER_CHANGE_LOG "" CACHE STRING "Specify the changelog to show after update")
set(LAUNCHER_VERSION_NAME "1.0.8" CACHE STRING "Specify the version name shown in settings")
set(LAUNCHER_VERSION_CODE "8" CACHE STRING "Specify the version code (integer) shown in settings")
set(LAUNCHER_FLATPAK_CONFIG_URL "" CACHE STRING "Specify the url to the flatpak config to compare the current version")
set(LAUNCHER_VERSION_COMPAT "" CACHE STRING "Specify Minecraft extraVersion compat level")
option(LAUNCHER_DISABLE_DEV_MODE "Disables the dev tab by default" ON)
option(LAUNCHER_ENABLE_GOOGLE_PLAY_LICENCE_CHECK "Enables checking license of the Game" ON)
option(LAUNCHER_MACOS_HAVE_ARMLAUNCHER "Enables use of the arm launcher" ON)
set(LAUNCHER_ENABLE_GLFW_DEFAULT ON)
if(APPLE)
    set(LAUNCHER_ENABLE_GLFW_DEFAULT OFF)
endif()
option(LAUNCHER_ENABLE_GLFW "Enables glfw integration" ${LAUNCHER_ENABLE_GLFW_DEFAULT})

find_package(Qt6LinguistTools)
set(LAUNCHER_LINGUIST_RESOURCES "")
if (Qt6LinguistTools_FOUND)
    file(GLOB TS_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.ts")
    if(UPDATE_TRANSLATIONS)
        qt6_create_translation(QM_FILES ${CMAKE_CURRENT_SOURCE_DIR} ${TS_FILES})
    else()
        qt6_add_translation(QM_FILES ${TS_FILES})
    endif()
    # Generate a .qrc file dynamically
    set(QRC_FILE "${CMAKE_CURRENT_BINARY_DIR}/translations.qrc")

    file(WRITE  ${QRC_FILE} "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
    file(APPEND ${QRC_FILE} "<RCC>\n")
    file(APPEND ${QRC_FILE} "  <qresource prefix=\"/translations\">\n")
    foreach(TS_FILE IN LISTS QM_FILES)
        # Extract just the filename (basename)
        get_filename_component(TS_BASENAME "${TS_FILE}" NAME)
        file(APPEND ${QRC_FILE} "    <file>${TS_BASENAME}</file>\n")
    endforeach()
    file(APPEND ${QRC_FILE} "  </qresource>\n")
    file(APPEND ${QRC_FILE} "</RCC>\n")
    set(LAUNCHER_LINGUIST_RESOURCES ${QRC_FILE} ${QM_FILES})
endif()

qt6_add_resources(RESOURCES qml.qrc)
set(RESOURCES ${RESOURCES} ${LAUNCHER_LINGUIST_RESOURCES})

if (NOT "${LAUNCHER_VERSIONDB_PATH}" STREQUAL "")
    configure_file(archivalversionlist.qrc ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
    configure_file("${LAUNCHER_VERSIONDB_PATH}/versions.armeabi-v7a.json.min" ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
    configure_file("${LAUNCHER_VERSIONDB_PATH}/versions.x86.json.min" ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
    configure_file("${LAUNCHER_VERSIONDB_PATH}/versions.arm64-v8a.json.min" ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
    configure_file("${LAUNCHER_VERSIONDB_PATH}/versions.x86_64.json.min" ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
    set(RESOURCES ${CMAKE_CURRENT_BINARY_DIR}/archivalversionlist.qrc ${RESOURCES})
endif()

add_executable(${PROJECT_NAME} main.cpp crash_handler.cpp crash_handler.h launcherapp.cpp launcherapp.h googleloginhelper.cpp googleloginhelper.h googleaccount.h versionmanager.cpp versionmanager.h apkextractiontask.cpp apkextractiontask.h zipextractiontask.cpp zipextractiontask.h googleplayapi.cpp googleplayapi.h googleversionchannel.cpp googleversionchannel.h googleapkdownloadtask.cpp googleapkdownloadtask.h downloadtask.cpp downloadtask.h modmanager.cpp modmanager.h gamelauncher.cpp gamelauncher.h profilemanager.cpp profilemanager.h qmlurlutils.h archivalversionlist.cpp archivalversionlist.h launchersettings.h troubleshooter.cpp troubleshooter.h updatechecker.cpp updatechecker.h supportedandroidabis.cpp supportedandroidabis.h cpuid.cpp cpuid.h gamepad.h encryption.h encrypted_file_login_cache.cpp encrypted_file_login_cache.h stdio_helper.cpp stdio_helper.h lumenconfig.cpp lumenconfig.h lumenprofiles.cpp lumenprofiles.h updatemanager.cpp updatemanager.h lumenupdater.cpp lumenupdater.h ${RESOURCES})
if (ENABLE_UPDATE_CHECK)
    target_compile_definitions(${PROJECT_NAME} PUBLIC UPDATE_CHECK UPDATE_CHECK_BUILD_ID=${UPDATE_CHECK_BUILD_ID} UPDATE_CHECK_URL="${UPDATE_CHECK_URL}")
endif()
if (NOT "${LAUNCHER_VERSIONDB_URL}" STREQUAL "")
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_VERSIONDB_URL="${LAUNCHER_VERSIONDB_URL}")
endif()
if (NOT "${LAUNCHER_CHANGE_LOG}" STREQUAL "")
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_CHANGE_LOG="${LAUNCHER_CHANGE_LOG}")
endif()
if (NOT "${LAUNCHER_VERSION_CODE}" STREQUAL "")
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_VERSION_CODE="${LAUNCHER_VERSION_CODE}")
endif()
if (NOT "${LAUNCHER_VERSION_NAME}" STREQUAL "")
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_VERSION_NAME="${LAUNCHER_VERSION_NAME}")
endif()
if (NOT "${LAUNCHER_FLATPAK_CONFIG_URL}" STREQUAL "")
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_FLATPAK_CONFIG_URL="${LAUNCHER_FLATPAK_CONFIG_URL}")
endif()
if (NOT "${LAUNCHER_VERSION_COMPAT}" STREQUAL "")
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_VERSION_COMPAT=${LAUNCHER_VERSION_COMPAT})
endif()
if(LAUNCHER_DISABLE_DEV_MODE)
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_DISABLE_DEV_MODE)
endif()
if(LAUNCHER_ENABLE_GOOGLE_PLAY_LICENCE_CHECK)
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_ENABLE_GOOGLE_PLAY_LICENCE_CHECK)
endif()
if(LAUNCHER_MACOS_HAVE_ARMLAUNCHER)
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_MACOS_HAVE_ARMLAUNCHER)
endif()
if(LAUNCHER_ENABLE_GLFW)
    include(ext/glfw.cmake)
    target_compile_definitions(${PROJECT_NAME} PUBLIC LAUNCHER_ENABLE_GLFW)
    target_link_libraries(${PROJECT_NAME} glfw)
endif()

if (APPLE)
    target_sources(${PROJECT_NAME} PUBLIC launcherapp_osx.mm supportedandroidabis.mm)
    target_link_libraries(${PROJECT_NAME} "-framework AppKit")
    if(ENABLE_SPARKLE_UPDATE_CHECK)
        target_sources(${PROJECT_NAME} PUBLIC sparkle_updatechecker.mm)
        target_compile_definitions(${PROJECT_NAME} PUBLIC SPARKLE_UPDATE_CHECK=1 SPARKLE_UPDATE_CHECK_URL="${SPARKLE_UPDATE_CHECK_URL}")
        target_link_libraries(${PROJECT_NAME} "-framework Sparkle")
    endif()
endif()

target_link_libraries(${PROJECT_NAME} Qt6::Core Qt6::Quick Qt6::Svg Qt6::Concurrent playdl-signin-ui-qt-lib mcpelauncher-extract-lib mcpelauncher-common mcpelauncher-apkinfo file-util gplayapi)

if(OpenSSL_FOUND)
    target_link_libraries(${PROJECT_NAME} OpenSSL::Crypto)
    target_sources(${PROJECT_NAME} PUBLIC encryption.cpp)
else()
    target_sources(${PROJECT_NAME} PUBLIC encryption_stub.cpp)
endif()

if (ENABLE_APPIMAGE_UPDATE_CHECK)
    target_compile_definitions(${PROJECT_NAME} PUBLIC APPIMAGE_UPDATE_CHECK=1)
    target_link_libraries(${PROJECT_NAME} z libappimageupdate)
endif()

install(TARGETS mcpelauncher-ui-qt RUNTIME COMPONENT mcpelauncher-ui-qt DESTINATION bin)
if (NOT APPLE)
    install(FILES mcpelauncher-ui-qt.desktop COMPONENT mcpelauncher-ui-qt DESTINATION share/applications)
    install(FILES Resources/lumen-icon.svg RENAME lumen-launcher.svg
            COMPONENT mcpelauncher-ui-qt DESTINATION share/pixmaps)
endif()
include(CPackSettings.cmake)
