########## MACROS ###########################################################################
#############################################################################################

function(conan_message MESSAGE_OUTPUT)
    if(NOT CONAN_CMAKE_SILENT_OUTPUT)
        message(${ARGV${0}})
    endif()
endfunction()


macro(conan_find_apple_frameworks FRAMEWORKS_FOUND FRAMEWORKS FRAMEWORKS_DIRS)
    if(APPLE)
        foreach(_FRAMEWORK ${FRAMEWORKS})
            # https://cmake.org/pipermail/cmake-developers/2017-August/030199.html
            find_library(CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND NAME ${_FRAMEWORK} PATHS ${FRAMEWORKS_DIRS} CMAKE_FIND_ROOT_PATH_BOTH)
            if(CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND)
                list(APPEND ${FRAMEWORKS_FOUND} ${CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND})
            else()
                message(FATAL_ERROR "Framework library ${_FRAMEWORK} not found in paths: ${FRAMEWORKS_DIRS}")
            endif()
        endforeach()
    endif()
endmacro()


function(conan_package_library_targets libraries package_libdir deps out_libraries out_libraries_target build_type package_name)
    unset(_CONAN_ACTUAL_TARGETS CACHE)
    unset(_CONAN_FOUND_SYSTEM_LIBS CACHE)
    foreach(_LIBRARY_NAME ${libraries})
        find_library(CONAN_FOUND_LIBRARY NAME ${_LIBRARY_NAME} PATHS ${package_libdir}
                     NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
        if(CONAN_FOUND_LIBRARY)
            conan_message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
            list(APPEND _out_libraries ${CONAN_FOUND_LIBRARY})
            if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
                # Create a micro-target for each lib/a found
                string(REGEX REPLACE "[^A-Za-z0-9.+_-]" "_" _LIBRARY_NAME ${_LIBRARY_NAME})
                set(_LIB_NAME CONAN_LIB::${package_name}_${_LIBRARY_NAME}${build_type})
                if(NOT TARGET ${_LIB_NAME})
                    # Create a micro-target for each lib/a found
                    add_library(${_LIB_NAME} UNKNOWN IMPORTED)
                    set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_FOUND_LIBRARY})
                    set(_CONAN_ACTUAL_TARGETS ${_CONAN_ACTUAL_TARGETS} ${_LIB_NAME})
                else()
                    conan_message(STATUS "Skipping already existing target: ${_LIB_NAME}")
                endif()
                list(APPEND _out_libraries_target ${_LIB_NAME})
            endif()
            conan_message(STATUS "Found: ${CONAN_FOUND_LIBRARY}")
        else()
            conan_message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
            list(APPEND _out_libraries_target ${_LIBRARY_NAME})
            list(APPEND _out_libraries ${_LIBRARY_NAME})
            set(_CONAN_FOUND_SYSTEM_LIBS "${_CONAN_FOUND_SYSTEM_LIBS};${_LIBRARY_NAME}")
        endif()
        unset(CONAN_FOUND_LIBRARY CACHE)
    endforeach()

    if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
        # Add all dependencies to all targets
        string(REPLACE " " ";" deps_list "${deps}")
        foreach(_CONAN_ACTUAL_TARGET ${_CONAN_ACTUAL_TARGETS})
            set_property(TARGET ${_CONAN_ACTUAL_TARGET} PROPERTY INTERFACE_LINK_LIBRARIES "${_CONAN_FOUND_SYSTEM_LIBS};${deps_list}")
        endforeach()
    endif()

    set(${out_libraries} ${_out_libraries} PARENT_SCOPE)
    set(${out_libraries_target} ${_out_libraries_target} PARENT_SCOPE)
endfunction()


########### FOUND PACKAGE ###################################################################
#############################################################################################

include(FindPackageHandleStandardArgs)

conan_message(STATUS "Conan: Using autogenerated Findflac.cmake")
set(flac_FOUND 1)
set(flac_VERSION "1.3.3")

find_package_handle_standard_args(flac REQUIRED_VARS
                                  flac_VERSION VERSION_VAR flac_VERSION)
mark_as_advanced(flac_FOUND flac_VERSION)

set(FLAC_COMPONENTS FLAC::FLAC++ FLAC::FLAC)

if(flac_FIND_COMPONENTS)
    foreach(_FIND_COMPONENT ${flac_FIND_COMPONENTS})
        list(FIND FLAC_COMPONENTS "FLAC::${_FIND_COMPONENT}" _index)
        if(${_index} EQUAL -1)
            conan_message(FATAL_ERROR "Conan: Component '${_FIND_COMPONENT}' NOT found in package 'FLAC'")
        else()
            conan_message(STATUS "Conan: Component '${_FIND_COMPONENT}' found in package 'FLAC'")
        endif()
    endforeach()
endif()

########### VARIABLES #######################################################################
#############################################################################################


set(FLAC_INCLUDE_DIRS "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_INCLUDE_DIR "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_INCLUDES "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_RES_DIRS )
set(FLAC_DEFINITIONS "-DFLAC__NO_DLL")
set(FLAC_LINKER_FLAGS_LIST
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>"
)
set(FLAC_COMPILE_DEFINITIONS "FLAC__NO_DLL")
set(FLAC_COMPILE_OPTIONS_LIST "" "")
set(FLAC_COMPILE_OPTIONS_C "")
set(FLAC_COMPILE_OPTIONS_CXX "")
set(FLAC_LIBRARIES_TARGETS "") # Will be filled later, if CMake 3
set(FLAC_LIBRARIES "") # Will be filled later
set(FLAC_LIBS "") # Same as FLAC_LIBRARIES
set(FLAC_SYSTEM_LIBS )
set(FLAC_FRAMEWORK_DIRS )
set(FLAC_FRAMEWORKS )
set(FLAC_FRAMEWORKS_FOUND "") # Will be filled later
set(FLAC_BUILD_MODULES_PATHS )

conan_find_apple_frameworks(FLAC_FRAMEWORKS_FOUND "${FLAC_FRAMEWORKS}" "${FLAC_FRAMEWORK_DIRS}")

mark_as_advanced(FLAC_INCLUDE_DIRS
                 FLAC_INCLUDE_DIR
                 FLAC_INCLUDES
                 FLAC_DEFINITIONS
                 FLAC_LINKER_FLAGS_LIST
                 FLAC_COMPILE_DEFINITIONS
                 FLAC_COMPILE_OPTIONS_LIST
                 FLAC_LIBRARIES
                 FLAC_LIBS
                 FLAC_LIBRARIES_TARGETS)

# Find the real .lib/.a and add them to FLAC_LIBS and FLAC_LIBRARY_LIST
set(FLAC_LIBRARY_LIST FLAC++ FLAC)
set(FLAC_LIB_DIRS "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/lib")

# Gather all the libraries that should be linked to the targets (do not touch existing variables):
set(_FLAC_DEPENDENCIES "${FLAC_FRAMEWORKS_FOUND} ${FLAC_SYSTEM_LIBS} Ogg::Ogg")

conan_package_library_targets("${FLAC_LIBRARY_LIST}"  # libraries
                              "${FLAC_LIB_DIRS}"      # package_libdir
                              "${_FLAC_DEPENDENCIES}"  # deps
                              FLAC_LIBRARIES            # out_libraries
                              FLAC_LIBRARIES_TARGETS    # out_libraries_targets
                              ""                          # build_type
                              "FLAC")                                      # package_name

set(FLAC_LIBS ${FLAC_LIBRARIES})

foreach(_FRAMEWORK ${FLAC_FRAMEWORKS_FOUND})
    list(APPEND FLAC_LIBRARIES_TARGETS ${_FRAMEWORK})
    list(APPEND FLAC_LIBRARIES ${_FRAMEWORK})
endforeach()

foreach(_SYSTEM_LIB ${FLAC_SYSTEM_LIBS})
    list(APPEND FLAC_LIBRARIES_TARGETS ${_SYSTEM_LIB})
    list(APPEND FLAC_LIBRARIES ${_SYSTEM_LIB})
endforeach()

# We need to add our requirements too
set(FLAC_LIBRARIES_TARGETS "${FLAC_LIBRARIES_TARGETS};Ogg::Ogg")
set(FLAC_LIBRARIES "${FLAC_LIBRARIES};Ogg::Ogg")

set(CMAKE_MODULE_PATH "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/" ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/" ${CMAKE_PREFIX_PATH})


########### COMPONENT FLAC VARIABLES #############################################

set(FLAC_FLAC_INCLUDE_DIRS "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_FLAC_INCLUDE_DIR "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_FLAC_INCLUDES "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_FLAC_LIB_DIRS "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/lib")
set(FLAC_FLAC_RES_DIRS )
set(FLAC_FLAC_DEFINITIONS "-DFLAC__NO_DLL")
set(FLAC_FLAC_COMPILE_DEFINITIONS "FLAC__NO_DLL")
set(FLAC_FLAC_COMPILE_OPTIONS_C "")
set(FLAC_FLAC_COMPILE_OPTIONS_CXX "")
set(FLAC_FLAC_LIBS FLAC)
set(FLAC_FLAC_SYSTEM_LIBS )
set(FLAC_FLAC_FRAMEWORK_DIRS )
set(FLAC_FLAC_FRAMEWORKS )
set(FLAC_FLAC_BUILD_MODULES_PATHS )
set(FLAC_FLAC_DEPENDENCIES Ogg::Ogg)
set(FLAC_FLAC_LINKER_FLAGS_LIST
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>"
)

########### COMPONENT FLAC++ VARIABLES #############################################

set(FLAC_FLAC++_INCLUDE_DIRS "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_FLAC++_INCLUDE_DIR "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_FLAC++_INCLUDES "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/include")
set(FLAC_FLAC++_LIB_DIRS "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/lib")
set(FLAC_FLAC++_RES_DIRS )
set(FLAC_FLAC++_DEFINITIONS )
set(FLAC_FLAC++_COMPILE_DEFINITIONS )
set(FLAC_FLAC++_COMPILE_OPTIONS_C "")
set(FLAC_FLAC++_COMPILE_OPTIONS_CXX "")
set(FLAC_FLAC++_LIBS FLAC++)
set(FLAC_FLAC++_SYSTEM_LIBS )
set(FLAC_FLAC++_FRAMEWORK_DIRS )
set(FLAC_FLAC++_FRAMEWORKS )
set(FLAC_FLAC++_BUILD_MODULES_PATHS )
set(FLAC_FLAC++_DEPENDENCIES FLAC::FLAC)
set(FLAC_FLAC++_LINKER_FLAGS_LIST
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>"
)


########## FIND PACKAGE DEPENDENCY ##########################################################
#############################################################################################

include(CMakeFindDependencyMacro)

if(NOT Ogg_FOUND)
    find_dependency(Ogg REQUIRED)
else()
    conan_message(STATUS "Conan: Dependency Ogg already found")
endif()


########## FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #######################################
#############################################################################################

########## COMPONENT FLAC FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #############

set(FLAC_FLAC_FRAMEWORKS_FOUND "")
conan_find_apple_frameworks(FLAC_FLAC_FRAMEWORKS_FOUND "${FLAC_FLAC_FRAMEWORKS}" "${FLAC_FLAC_FRAMEWORK_DIRS}")

set(FLAC_FLAC_LIB_TARGETS "")
set(FLAC_FLAC_NOT_USED "")
set(FLAC_FLAC_LIBS_FRAMEWORKS_DEPS ${FLAC_FLAC_FRAMEWORKS_FOUND} ${FLAC_FLAC_SYSTEM_LIBS} ${FLAC_FLAC_DEPENDENCIES})
conan_package_library_targets("${FLAC_FLAC_LIBS}"
                              "${FLAC_FLAC_LIB_DIRS}"
                              "${FLAC_FLAC_LIBS_FRAMEWORKS_DEPS}"
                              FLAC_FLAC_NOT_USED
                              FLAC_FLAC_LIB_TARGETS
                              ""
                              "FLAC_FLAC")

set(FLAC_FLAC_LINK_LIBS ${FLAC_FLAC_LIB_TARGETS} ${FLAC_FLAC_LIBS_FRAMEWORKS_DEPS})

set(CMAKE_MODULE_PATH "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/" ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/" ${CMAKE_PREFIX_PATH})

########## COMPONENT FLAC++ FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #############

set(FLAC_FLAC++_FRAMEWORKS_FOUND "")
conan_find_apple_frameworks(FLAC_FLAC++_FRAMEWORKS_FOUND "${FLAC_FLAC++_FRAMEWORKS}" "${FLAC_FLAC++_FRAMEWORK_DIRS}")

set(FLAC_FLAC++_LIB_TARGETS "")
set(FLAC_FLAC++_NOT_USED "")
set(FLAC_FLAC++_LIBS_FRAMEWORKS_DEPS ${FLAC_FLAC++_FRAMEWORKS_FOUND} ${FLAC_FLAC++_SYSTEM_LIBS} ${FLAC_FLAC++_DEPENDENCIES})
conan_package_library_targets("${FLAC_FLAC++_LIBS}"
                              "${FLAC_FLAC++_LIB_DIRS}"
                              "${FLAC_FLAC++_LIBS_FRAMEWORKS_DEPS}"
                              FLAC_FLAC++_NOT_USED
                              FLAC_FLAC++_LIB_TARGETS
                              ""
                              "FLAC_FLAC++")

set(FLAC_FLAC++_LINK_LIBS ${FLAC_FLAC++_LIB_TARGETS} ${FLAC_FLAC++_LIBS_FRAMEWORKS_DEPS})

set(CMAKE_MODULE_PATH "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/" ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH "C:/Users/Dolba/.conan/data/flac/1.3.3/_/_/package/b8a7c0e3d5f7f2dd0a4cf862cb60ff9f1b73be47/" ${CMAKE_PREFIX_PATH})


########## TARGETS ##########################################################################
#############################################################################################

########## COMPONENT FLAC TARGET #################################################

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
    # Target approach
    if(NOT TARGET FLAC::FLAC)
        add_library(FLAC::FLAC INTERFACE IMPORTED)
        set_target_properties(FLAC::FLAC PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                              "${FLAC_FLAC_INCLUDE_DIRS}")
        set_target_properties(FLAC::FLAC PROPERTIES INTERFACE_LINK_DIRECTORIES
                              "${FLAC_FLAC_LIB_DIRS}")
        set_target_properties(FLAC::FLAC PROPERTIES INTERFACE_LINK_LIBRARIES
                              "${FLAC_FLAC_LINK_LIBS};${FLAC_FLAC_LINKER_FLAGS_LIST}")
        set_target_properties(FLAC::FLAC PROPERTIES INTERFACE_COMPILE_DEFINITIONS
                              "${FLAC_FLAC_COMPILE_DEFINITIONS}")
        set_target_properties(FLAC::FLAC PROPERTIES INTERFACE_COMPILE_OPTIONS
                              "${FLAC_FLAC_COMPILE_OPTIONS_C};${FLAC_FLAC_COMPILE_OPTIONS_CXX}")
    endif()
endif()

########## COMPONENT FLAC++ TARGET #################################################

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
    # Target approach
    if(NOT TARGET FLAC::FLAC++)
        add_library(FLAC::FLAC++ INTERFACE IMPORTED)
        set_target_properties(FLAC::FLAC++ PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                              "${FLAC_FLAC++_INCLUDE_DIRS}")
        set_target_properties(FLAC::FLAC++ PROPERTIES INTERFACE_LINK_DIRECTORIES
                              "${FLAC_FLAC++_LIB_DIRS}")
        set_target_properties(FLAC::FLAC++ PROPERTIES INTERFACE_LINK_LIBRARIES
                              "${FLAC_FLAC++_LINK_LIBS};${FLAC_FLAC++_LINKER_FLAGS_LIST}")
        set_target_properties(FLAC::FLAC++ PROPERTIES INTERFACE_COMPILE_DEFINITIONS
                              "${FLAC_FLAC++_COMPILE_DEFINITIONS}")
        set_target_properties(FLAC::FLAC++ PROPERTIES INTERFACE_COMPILE_OPTIONS
                              "${FLAC_FLAC++_COMPILE_OPTIONS_C};${FLAC_FLAC++_COMPILE_OPTIONS_CXX}")
    endif()
endif()

########## GLOBAL TARGET ####################################################################

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
    if(NOT TARGET FLAC::FLAC)
        add_library(FLAC::FLAC INTERFACE IMPORTED)
    endif()
    set_property(TARGET FLAC::FLAC APPEND PROPERTY
                 INTERFACE_LINK_LIBRARIES "${FLAC_COMPONENTS}")
endif()

########## BUILD MODULES ####################################################################
#############################################################################################
########## COMPONENT FLAC BUILD MODULES ##########################################

foreach(_BUILD_MODULE_PATH ${FLAC_FLAC_BUILD_MODULES_PATHS})
    include(${_BUILD_MODULE_PATH})
endforeach()
########## COMPONENT FLAC++ BUILD MODULES ##########################################

foreach(_BUILD_MODULE_PATH ${FLAC_FLAC++_BUILD_MODULES_PATHS})
    include(${_BUILD_MODULE_PATH})
endforeach()
