# FindFontConfig.cmake
# Author: Pradeep Garigipati <pradeep@arrayfire.com>
#
# Finds the fontconfig libraries
#
# Sets the following variables:
#          FontConfig_FOUND
#          FontConfig_INCLUDE_DIR
#          FontConfig_DYNAMIC_LIBRARY
#          FontConfig_STATIC_LIBRARY
#
# Usage:
# find_package(FontConfig)
# if (FontConfig_FOUND)
#    target_link_libraries(mylib PRIVATE FontConfig::FontConfig)
# endif (FontConfig_FOUND)
#
# OR if you want to link against the static library:
#
# find_package(FontConfig)
# if (FontConfig_FOUND)
#    target_link_libraries(mylib PRIVATE FontConfig::FontConfig_STATIC)
# endif (FontConfig_FOUND)
#
# NOTE: You do not need to include the FontConfig include directories since they
# will be included as part of the target_link_libraries command

set(PX ${CMAKE_STATIC_LIBRARY_PREFIX})
set(SX ${CMAKE_STATIC_LIBRARY_SUFFIX})

# use pkg-config to get the directories and then use these values
# in the find_path() and find_library() calls
if (NOT WIN32)
    find_package(PkgConfig)
    pkg_check_modules(FontConfigPkg QUIET fontconfig)
endif (NOT WIN32)

find_path(FontConfig_INCLUDE_DIR
    NAMES fontconfig/fontconfig.h
    PATHS
        /usr/include
        /usr/local/include
        /sw/include
        /opt/local/include
    HINTS
        ${FontConfigPkg_INCLUDE_DIRS} # Generated by pkg-config
    )

find_library(FontConfig_DYNAMIC_LIBRARY
    NAMES FontConfig fontconfig ${FontConfigPkg_LIBRARY}
    PATHS
        /usr/local
        /usr/X11
        /usr
        /sw
        /opt/local
        /usr/lib/x86_64-linux-gnu
    HINTS
        ${FontConfigPkg_LIBRARY_DIRS} # Generated by pkg-config
    PATH_SUFFIXES
        lib64
        lib
    )

find_library(FontConfig_STATIC_LIBRARY
    NAMES ${PX}fontconfig${SX} ${PX}FontConfig${SX} ${PX}${FontConfigPkg_LIBRARY}${SX}
    PATHS
        /usr/local
        /usr/X11
        /usr
        /sw
        /opt/local
        /usr/lib/x86_64-linux-gnu
    HINTS
        ${FontConfigPkg_LIBRARY_DIRS} # Generated by pkg-config
    PATH_SUFFIXES
        lib64
        lib
    )

mark_as_advanced(
    FontConfig_INCLUDE_DIR
    FontConfig_DYNAMIC_LIBRARY
    FontConfig_STATIC_LIBRARY
    )

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(FontConfig
    REQUIRED_VARS FontConfig_DYNAMIC_LIBRARY FontConfig_INCLUDE_DIR
    )

if (FontConfig_FOUND AND NOT TARGET FontConfig::FontConfig)
    add_library(FontConfig::FontConfig UNKNOWN IMPORTED)
    set_target_properties(FontConfig::FontConfig PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGE "C"
        IMPORTED_LOCATION ${FontConfig_DYNAMIC_LIBRARY}
        INTERFACE_INCLUDE_DIRECTORIES ${FontConfig_INCLUDE_DIR})

    if (FontConfig_STATIC_LIBRARY)
        add_library(FontConfig::FontConfig_STATIC UNKNOWN IMPORTED)
        set_target_properties(FontConfig::FontConfig_STATIC PROPERTIES
            IMPORTED_LINK_INTERFACE_LANGUAGE "C"
            IMPORTED_LOCATION "${FontConfig_STATIC_LIBRARY}"
            INTERFACE_INCLUDE_DIRECTORIES "${FontConfig_INCLUDE_DIR}")
    endif (FontConfig_STATIC_LIBRARY)
endif ()
