message(STATUS "CMake version ${CMAKE_VERSION}")
# - Config file for the Qul package
# Compute paths
get_filename_component(Qul_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(QUL_CMAKE_MODULES_DIR "@CONF_CMAKE_MODULES_DIR@" CACHE INTERNAL "Modules directory")
set(QUL_APP_COMMON_DIR "@CONF_APP_COMMON_DIR@" CACHE INTERNAL "App common convenience package directory")
set(QUL_PRIVATE_DEFAULT_FONTS_DIR "@CONF_FONTS_DIR@" CACHE INTERNAL "Fonts directory")
set(QUL_BIN_DIR "@CONF_BIN_DIR@" CACHE INTERNAL "Binary directory")
set(QUL_LIB_DIR "@CONF_LIB_DIR@" CACHE INTERNAL "Library directory")
set(QUL_PLATFORM_BOARDS_DIR "${Qul_CMAKE_DIR}/../../../platform/boards" CACHE STRING "Directory that contains the platform sources")

# CMake does not provide the variable inside this file. Setting in manually.
set(Qul_SOURCE_DIR "${Qul_CMAKE_DIR}/../../../")

if(NOT EXISTS "${QUL_PLATFORM_BOARDS_DIR}")
    message(FATAL_ERROR "QUL_PLATFORM_BOARDS_DIR ${QUL_PLATFORM_BOARDS_DIR} does not exist.")
endif()

list(APPEND CMAKE_MODULE_PATH ${QUL_CMAKE_MODULES_DIR})

string(TOLOWER "${QUL_PLATFORM}" QUL_PLATFORM)

include("${CMAKE_CURRENT_LIST_DIR}/QulScopeFinalizerHelpers.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/QulMacros.cmake")

# For newer CMake versions, we use cmake_language(DEFER CALL) instead.
if(CMAKE_VERSION VERSION_LESS "3.19.0")
    variable_watch(CMAKE_CURRENT_LIST_DIR qul_watch_current_list_dir)
endif()

qul_private_init_build_type()

qul_private_check_compiler_version()

if(NOT QUL_COMPILER_NAME AND QUL_PLATFORM STREQUAL "qt")
  string(TOLOWER ${CMAKE_CXX_COMPILER_ID} QUL_COMPILER_NAME)
  string(TOLOWER ${CMAKE_CXX_COMPILER_ID} COMPILER_FOLDER_NAME)
endif()

if (NOT QUL_COMPILER_NAME)
  message(FATAL_ERROR "QUL_COMPILER_NAME not defined")
endif()

# Our library dependencies (contains definitions for IMPORTED targets)
if(NOT TARGET Qul::Core)
  if (NOT QUL_PLATFORM)
    message(FATAL_ERROR "QUL_PLATFORM not defined")
  endif()

  # Load platform default, which includes the architecture
  qul_private_find_and_get_board_manufacturer_name(${QUL_PLATFORM})
  include(${QUL_PLATFORM_BOARDS_DIR}/${BOARD_MANUFACTURER_NAME}/${QUL_PLATFORM}/cmake/BoardDefaults.cmake)

  include("${CMAKE_CURRENT_LIST_DIR}/AssetsDefaults.cmake")

  qul_private_determine_architecture() # This needs to happen before loading of the imported targets.

  set(QUL_CORE_TARGETS_PATH_BASE "${Qul_CMAKE_DIR}/QulCoreTargets/QulCoreTargets_${QUL_PLATFORM_ARCHITECTURE}_${QUL_COMPILER_NAME}")
  if (NOT EXISTS "${QUL_CORE_TARGETS_PATH_BASE}.cmake")
    message(WARNING "File ${QUL_CORE_TARGETS_PATH_BASE}.cmake does not exist.")
    message(FATAL_ERROR "Please install package for ${QUL_PLATFORM_ARCHITECTURE}")
  endif()
  include("${QUL_CORE_TARGETS_PATH_BASE}.cmake")

  message(STATUS "OS: ${QUL_OS}")

  qul_private_init_ipo()
  qul_private_init_pie()
  qul_private_setup_architecture()

  set(QUL_PLATFORM_TARGETS_PATH_BASE "${Qul_CMAKE_DIR}/QulPlatformTargets/QulPlatformTargets_${QUL_PLATFORM}_${QUL_COLOR_DEPTH}bpp_${CMAKE_HOST_SYSTEM_NAME}_${QUL_COMPILER_NAME}")
  if(NOT QUL_BUILD_PLATFORM)
      # Only import the prebuilt library if the it is not supposed to be built from sources
      include("${QUL_PLATFORM_TARGETS_PATH_BASE}.cmake" OPTIONAL)

      include("${Qul_CMAKE_DIR}/QulPlatformTargets/QulPlatformTargets_${QUL_PLATFORM}_extra_config.cmake" OPTIONAL)
  endif()
  qul_private_setup_platform()

  include(${Qul_CMAKE_DIR}/QulTargetPostProcess.cmake)

  set(SPARK_TARGETS_BASE_NAME "QulMonotypeSparkTargets_${QUL_PLATFORM_ARCHITECTURE}_${QUL_COMPILER_NAME}")
  include("${Qul_CMAKE_DIR}/QulMonotypeTargets/${SPARK_TARGETS_BASE_NAME}.cmake")
endif()

include("${CMAKE_CURRENT_LIST_DIR}/QulTranslationTools.cmake")
include("${QUL_APP_COMMON_DIR}/AppMacros.cmake")
set(QUL_GENERATORS "${CMAKE_CURRENT_LIST_DIR}/QulGenerators.cmake" CACHE FILEPATH "Location of the QulGenerators.cmake file")
include(${QUL_GENERATORS})

set(QUL_LIBRARIES Qul::Core)
