cmake_minimum_required(VERSION 3.16..3.25)

set(@name@_known_comps static shared)
set(@name@_comp_static NO)
set(@name@_comp_shared NO)
foreach (@name@_comp IN LISTS ${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS)
    if (@name@_comp IN_LIST @name@_known_comps)
        set(@name@_comp_${@name@_comp} YES)
    else ()
        set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
            "@name@ does not recognize component `${@name@_comp}`.")
        set(${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)
        return()
    endif ()
endforeach ()

if (@name@_comp_static AND @name@_comp_shared)
    set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
        "@name@ `static` and `shared` components are mutually exclusive.")
    set(${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)
    return()
endif ()

set(@name@_static_targets "${CMAKE_CURRENT_LIST_DIR}/@name@-static-targets.cmake")
set(@name@_shared_targets "${CMAKE_CURRENT_LIST_DIR}/@name@-shared-targets.cmake")

macro(@name@_load_targets type)
    if (NOT EXISTS "${@name@_${type}_targets}")
        set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
            "@name@ `${type}` libraries were requested but not found.")
        set(${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)
        return()
    endif ()
    include("${@name@_${type}_targets}")
endmacro()

if (@name@_comp_static)
    @name@_load_targets(static)
elseif (@name@_comp_shared)
    @name@_load_targets(shared)
elseif (DEFINED @name@_SHARED_LIBS AND @name@_SHARED_LIBS)
    @name@_load_targets(shared)
elseif (DEFINED @name@_SHARED_LIBS AND NOT @name@_SHARED_LIBS)
    @name@_load_targets(static)
elseif (BUILD_SHARED_LIBS)
    if (EXISTS "${@name@_shared_targets}")
        @name@_load_targets(shared)
    else ()
        @name@_load_targets(static)
    endif ()
else ()
    if (EXISTS "${@name@_static_targets}")
        @name@_load_targets(static)
    else ()
        @name@_load_targets(shared)
    endif ()
endif ()
