#######################################################################################################################
#
# cmake module for finding @PROJECT_NAME@
#
# returns:
#   @PROJECT_NAME@_FOUND        : true or false, depending on whether the package was found
#   @PROJECT_NAME@_VERSION      : the package version
#   @PROJECT_NAME@_INCLUDE_DIRS : path to the include directory
#   @PROJECT_NAME@_LIBRARY_DIRS : path to the library directory
#   @PROJECT_NAME@_LIBRARIES    : libraries the depending project needs to link against
#   @PROJECT_NAME@_CXX_FLAGS    : additional C++ compiler flags
#   @PROJECT_NAME@_LINKER_FLAGS : additional linker flags
#
# Note for exporting project:
# If @PROJECT_NAME@ sets PROVIDES_EXPORTED_TARGETS, we also generate target EXPORTS. In this case the its CMakeLists.txt
# must also have 
#  install(TARGETS  EXPORT ${PROJECT_NAME}Targets) 
# before. The target will be named ChimeraTK::${PROJECT_NAME} .
# We keep above mentioned return variables for compatibility, but finally, when all projects use imported targets,
# all execept _FOUND and _VERSION will be superfluous.
#
# @author Martin Killenberg, DESY
#
#######################################################################################################################

#######################################################################################################################
#
# IMPORTANT NOTE:
#
# DO NOT MODIFY THIS FILE inside a project. Instead update the project-template repository and pull the change from
# there. Make sure to keep the file generic, since it will be used by other projects, too.
#
# If you have modified this file inside a project despite this warning, make sure to cherry-pick all your changes
# into the project-template repository immediately.
#
#######################################################################################################################

# In case of packages with components, loading dependencies can cause trouble if required component list are differ.
# E.g. if Boost is required from the project, and also required from the dependency, but with less components, 
# and the dependency is resolved later, then Boost_LIBRARIES content will be different than expected.
# To protect against this, save state and restore it later.
# Since imported targets are never unloaded, the loaded dependency should still work.
set(Boost_LIBRARIES_savedState_@PROJECT_NAME@ ${Boost_LIBRARIES})

# this code loads public dependencies
@@PROJECT_NAME@_PUBLIC_DEPENDENCIES_L@
set(Boost_LIBRARIES ${Boost_LIBRARIES_savedState_@PROJECT_NAME@})

# Since this file is already part of the installation to be found, the configuration can be hard-coded at
# installation time
set(@PROJECT_NAME@_VERSION "@@PROJECT_NAME@_SOVERSION@")
set(@PROJECT_NAME@_PREFIX "@CMAKE_INSTALL_PREFIX@")

# Use a macro provided by CMake to check if all the listed arguments are valid and set @PROJECT_NAME@_FOUND accordingly.
# This is mainly important to check the version.
include(FindPackageHandleStandardArgs)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(@PROJECT_NAME@ REQUIRED_VARS @PROJECT_NAME@_PREFIX VERSION_VAR @PROJECT_NAME@_VERSION FOUND_VAR @PROJECT_NAME@_FOUND)

# switch for exported target. We don't do this automatically, because the calling CMakeLists.txt
# first must be edited so that it properly defines PUBLIC set of compile and link options
if(@PROVIDES_EXPORTED_TARGETS@)
    
    @PACKAGE_INIT@

    # include cmake's auto-generated exports file
    include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
    # this will set ${PROJECT_NAME}_FOUND if required components are missing
    check_required_components(@PROJECT_NAME@)
    
else()
    if(@@PROJECT_NAME@_HAS_LIBRARY@)
        # has true compiled library as output
        # The library itself must be "searched" using the FIND_LIBRARY command in the known install directory, to set
        # the variable properly
        FIND_LIBRARY(@PROJECT_NAME@_LIBRARY @PROJECT_NAME@
          @CMAKE_INSTALL_PREFIX@/lib
          NO_DEFAULT_PATH
        )
        if(NOT @PROJECT_NAME@_LIBRARY)
            set(@PROJECT_NAME@_FOUND FALSE)
            message(SEND_ERROR "FIND_LIBRARY returned error: ${@PROJECT_NAME@_LIBRARY}")
        else()
            # prepend it to lib list
            set(@PROJECT_NAME@_LIBRARIES ${@PROJECT_NAME@_LIBRARY} ${@PROJECT_NAME@_LIBRARIES})
        endif()
    endif()
endif() 

# Compatibility layer definitions.
# The following variables are generated automatically for projects supporting cmake-exports.
# Otherwise, values must be provided as inputs to create_cmake_config_files.
# The types of generated lists are precisely as in old DeviceAccess config
# inc dirs, lib dirs, libs are ";" separated
# cxx flags, link flags are " " separated
# ";" separated
set(@PROJECT_NAME@_INCLUDE_DIRS @@PROJECT_NAME@_INCLUDE_DIRS@)
# ";" separated
set(@PROJECT_NAME@_LIBRARY_DIRS @@PROJECT_NAME@_LIBRARY_DIRS@)
# ";" separated
set(@PROJECT_NAME@_LIBRARIES ${@PROJECT_NAME@_LIBRARIES} @@PROJECT_NAME@_LIBRARIES@)
# " " separated
set(@PROJECT_NAME@_CXX_FLAGS "@@PROJECT_NAME@_CXX_FLAGS@")
# " " separated
set(@PROJECT_NAME@_LINKER_FLAGS "@@PROJECT_NAME@_LINKER_FLAGS@ @@PROJECT_NAME@_LINK_FLAGS@")
# " " separated
set(@PROJECT_NAME@_LINK_FLAGS "@@PROJECT_NAME@_LINKER_FLAGS@ @@PROJECT_NAME@_LINK_FLAGS@")