set({{upperName}}_MODULE_DIR ${CMAKE_CURRENT_LIST_DIR})

if({{upperName}}_RUN_COPY_MODE)
    message(STATUS "Copy libs for: {{name}}...")
    FILE (GLOB_RECURSE LISTS_{{name}}_LIBS_DIR LIST_DIRECTORIES false ${INLINE_LIBS_PATH}/*)
    FOREACH (file ${LISTS_{{name}}_LIBS_DIR})
        file(RELATIVE_PATH file_name ${INLINE_LIBS_PATH} ${file})
        set(dest_file_name "${ROOT_PROJECT_BINARY_DIR}/bundle/lib/${file_name}")
        get_filename_component(dest_dir "${dest_file_name}" DIRECTORY)
        if (NOT EXISTS "${dest_file_name}")
          file(COPY ${file} DESTINATION ${dest_dir})
          message(STATUS "Copy lib file: ${dest_file_name} ...")
        endif()
    ENDFOREACH(file)
    return()
endif()

# Determine target processor name
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x64|x86_64)")
    SET({{upperName}}_CPU_NAME "x86_64")
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(ARM64|aarch64|ARM64EC|arm64ec|ARM64E|arm64e)")
    SET({{upperName}}_CPU_NAME "aarch64")
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(ARM|armv7hl|armv7l|armv7)")
    SET({{upperName}}_CPU_NAME "armv7hl")
ELSE()
    MESSAGE(FATAL_ERROR "Unsupported architecture \"${CMAKE_SYSTEM_PROCESSOR}\"")
ENDIF()
message(STATUS "Target CPU Name: ${{ '{' }}{{upperName}}_CPU_NAME}" )

{% for dependency in dependencies %}
if (NOT TARGET {{dependency}}::{{dependency}})
    find_package({{dependency}} REQUIRED)
endif()
{% endfor %}

{% if executables|length > 0 %}
function(define_imported_target libraries headers executables)
{% else %}
function(define_imported_target libraries headers)
{% endif %}
  set(common_lib_name {{name}}::{{name}})
  message(STATUS "Define imported target: {{name}}...")

  if (NOT TARGET ${common_lib_name})
    message(STATUS "Adding custom target: {{name}}...")
    add_library(${common_lib_name} INTERFACE IMPORTED GLOBAL)
    target_include_directories(${common_lib_name} INTERFACE ${headers})

{%- if source_files|length > 0 %}
    set(static_lib_name "{{name}}_static")
    add_library("${static_lib_name}"  STATIC
      {%- for file in source_files %}
        "${{ '{' }}{{upperName}}_MODULE_DIR}/{{file}}"
      {%- endfor -%}
    )
    target_include_directories(${static_lib_name} PUBLIC ${headers})
    set_target_properties(${static_lib_name} PROPERTIES POSITION_INDEPENDENT_CODE ON)
{% for dependency in dependencies %}
    target_link_libraries(${static_lib_name} PUBLIC {{dependency}}::{{dependency}})
{%- endfor %}
    target_link_libraries(${common_lib_name} INTERFACE "${static_lib_name}")
{% endif %}

{% for dependency in dependencies %}
    target_link_libraries(${common_lib_name} INTERFACE {{dependency}}::{{dependency}})
{%- endfor %}

{%- if not has_wrapper %}
    foreach (file ${libraries})
      get_filename_component(lib_name ${file} NAME)
      get_filename_component(lib_name_stem ${file} NAME_WE)

      set(target_name {{name}}_${lib_name_stem}_target)

      if (NOT TARGET ${target_name})
        add_library(${target_name} SHARED IMPORTED GLOBAL)
        set_target_properties(${target_name} PROPERTIES
          IMPORTED_LOCATION "${file}"
        )
      endif()
      target_link_libraries(${common_lib_name} INTERFACE ${target_name})
    endforeach()
{% endif %}

    set(libs_path ${{ '{' }}{{upperName}}_MODULE_DIR}/${{ '{' }}{{upperName}}_CPU_NAME})
    set(copy_libs_name {{name}}_copy_libs)
    if (NOT TARGET ${copy_libs_name})
      add_custom_target(${copy_libs_name}
        COMMAND ${CMAKE_COMMAND}
          -D{{upperName}}_RUN_COPY_MODE=ON
          "-DINLINE_LIBS_PATH=${libs_path}"
          "-DROOT_PROJECT_BINARY_DIR=${ROOT_PROJECT_BINARY_DIR}"
          -P "${CMAKE_CURRENT_LIST_FILE}"
        VERBATIM
        COMMENT "Copying {{name}} libraries into bundle dir (via -P self-reinvocation)"
      )
    endif()
    add_dependencies(${common_lib_name} ${copy_libs_name})

{% if executables|length > 0 %}
    FOREACH (FILE ${executables})
        install(FILES ${FILE} DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})
    ENDFOREACH(FILE)
{% endif %}
  endif()

  set({{name}}_LIBRARIES "${libraries}"
      CACHE STRING "Path to {{name}} library" FORCE)
  mark_as_advanced(FORCE {{name}}_LIBRARIES)
{% if executables|length > 0 %}
  set({{name}}_EXECUTABLES "${executables}"
      CACHE STRING "Path to {{name}} executable files" FORCE)
  mark_as_advanced(FORCE {{name}}_EXECUTABLES)
{% endif %}

  set({{name}}_INCLUDES "${headers}"
    CACHE STRING "Path to {{name}} headers" FORCE)
  mark_as_advanced(FORCE {{name}}_INCLUDES)

  set_property(GLOBAL PROPERTY {{name}}_PROPERTY_LIBS_PATH ${libs_path})
  set_property(GLOBAL PROPERTY {{name}}_PROPERTY_INCLUDE_PATH ${headers})

  set({{name}}_FOUND 1 CACHE INTERNAL "{{name}} found" FORCE)
endfunction()

{% if executables|length > 0 %}
if ({{name}}_LIBRARIES AND {{name}}_INCLUDES AND {{name}}_EXECUTABLES)
  define_imported_target("${{ '{' }}{{name}}_LIBRARIES}" "${{ '{' }}{{name}}_INCLUDES}" "${{ '{' }}{{name}}_EXECUTABLES}")
  return()
endif()
{% else %}
if ({{name}}_LIBRARIES AND {{name}}_INCLUDES)
  define_imported_target("${{ '{' }}{{name}}_LIBRARIES}" "${{ '{' }}{{name}}_INCLUDES}")
  return()
endif()
{% endif %}

set({{name}}_LIBRARIES_PATHS)
{% for file in libraries %}
list(APPEND {{name}}_LIBRARIES_PATHS ${{ '{' }}{{upperName}}_MODULE_DIR}/${{ '{' }}{{upperName}}_CPU_NAME}/{{file}})
{%- endfor %}

{% if executables|length > 0 %}
set({{name}}_EXECUTABLES_LIST {{executables}})
foreach (file ${{ '{' }}{{name}}_EXECUTABLES_LIST})
  list(APPEND {{name}}_EXECUTABLES_PATH ${{ '{' }}{{upperName}}_MODULE_DIR}/libexec/${{ '{' }}{{upperName}}_CPU_NAME}/${file})
endforeach()
{% endif %}

set({{name}}_HEADER_PATH ${{ '{' }}{{upperName}}_MODULE_DIR}/include)

# Returning the final results
include(FindPackageHandleStandardArgs)
{% if not has_wrapper and libraries|length > 0 %}
find_package_handle_standard_args(
  {{name}} DEFAULT_MSG "{{name}}_LIBRARIES_PATHS" "{{name}}_HEADER_PATH"
)
{% else %}
find_package_handle_standard_args(
  {{name}}
  REQUIRED_VARS {{name}}_HEADER_PATH
  REASON_FAILURE_MESSAGE "{{name}} headers not found in ${{ '{' }}{{name}}_HEADER_PATH}"
)
{% endif %}

if ({{name}}_FOUND)
{%- if executables|length > 0 %}
  define_imported_target("${{ '{' }}{{name}}_LIBRARIES_PATHS}" "${{ '{' }}{{name}}_HEADER_PATH}" "${{ '{' }}{{name}}_EXECUTABLES_PATH}")
{% else %}
  define_imported_target("${{ '{' }}{{name}}_LIBRARIES_PATHS}" "${{ '{' }}{{name}}_HEADER_PATH}")
{% endif -%}
elseif({{name}}_FIND_REQUIRED)
  message(FATAL_ERROR "Required {{name}} library not found")
endif()
