MACRO(CHUCHO_APPEND_NON_PKG_CONFIG_DEPS CHUCHO_LIBS_VAR)
    FOREACH(LIB ${${CHUCHO_LIBS_VAR}})
        GET_FILENAME_COMPONENT(CHUCHO_LOCAL_LIB_PART ${LIB} DIRECTORY)
        LIST(APPEND CHUCHO_NON_PKG_CONFIG_DEP_DIRS "-L${CHUCHO_LOCAL_LIB_PART}")
        GET_FILENAME_COMPONENT(CHUCHO_LOCAL_LIB_PART ${LIB} NAME_WE)
        STRING(FIND ${CHUCHO_LOCAL_LIB_PART} "lib" CHUCHO_LOCAL_PREFIX_PART)
        IF(${CHUCHO_LOCAL_PREFIX_PART} EQUAL 0)
            STRING(SUBSTRING ${CHUCHO_LOCAL_LIB_PART} 3 -1 CHUCHO_LOCAL_LIB_PART)
        ENDIF()
        LIST(APPEND CHUCHO_NON_PKG_CONFIG_DEPS "-l${CHUCHO_LOCAL_LIB_PART}")
    ENDFOREACH()
ENDMACRO()

# Libs: is for our libraries
# Libs.private: is for libraries that do not have pkg-config support and should
#               be linked in a static build
# Requires: is for libraries that both have pkg-config support and have publicly
#           included files in Chucho
# Requires.private: is for libraries that have pkg-config support and do not have
#                   publicly included files in Chucho
MACRO(CHUCHO_PROCESS_PKG_DEPS)
    IF(CHUCHO_POSIX)
        IF(CURL_FOUND)
            LIST(APPEND CHUCHO_PKG_CONFIG_PUBLIC_DEPS libcurl)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS libcurl4)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES libcurl)
        ENDIF()
        IF(RUBY_FOUND)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS ruby)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS ruby)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES ruby)
        ENDIF()
        IF(PROTOBUF_FOUND)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS protobuf)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS libprotobuf-dev)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES protobuf)
        ENDIF()
        IF(CHUCHO_HAVE_CAPN_PROTO)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS capnp)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS libcapnp-dev)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES capnproto-devel)
        ENDIF()
        IF(CHUCHO_HAVE_FLATBUFFERS)
            CHUCHO_APPEND_NON_PKG_CONFIG_DEPS(FLATBUFFERS_LIBS)
        ENDIF()
        IF(ZLIB_FOUND)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS zlib)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS zlib1g)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES zlib)
        ENDIF()
        IF(BZIP2_FOUND)
            CHUCHO_APPEND_NON_PKG_CONFIG_DEPS(BZIP2_LIBRARIES)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS bzip2)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES bzip2)
        ENDIF()
        IF(LibArchive_FOUND)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS libarchive)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS libarchive-dev)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES libarchive-devel)
        ENDIF()
        IF(LIBLZMA_FOUND)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS liblzma)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS liblzma5)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES lzma-sdk)
        ENDIF()
        IF(CHUCHO_HAVE_LZ4)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS liblz4)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS liblz4-1)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES lz4-devel)
        ENDIF()
        IF(CHUCHO_HAVE_ZEROMQ)
            LIST(APPEND CHUCHO_PKG_CONFIG_PRIVATE_DEPS libzmq)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS libczmq4)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES zeromq)
        ENDIF()
        IF(CHUCHO_HAVE_ACTIVEMQ)
            CHUCHO_APPEND_NON_PKG_CONFIG_DEPS(ACTIVEMQ_LIBS)
            LIST(APPEND CHUCHO_NON_PKG_CONFIG_CLFAGS "-I${ACTIVEMQ_INCLUDE_DIR}")
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES activemq-cpp-devel)
        ENDIF()
        IF(CHUCHO_HAVE_RABBITMQ)
            LIST(APPEND CHUCHO_PKG_CONFIG_PUBLIC_DEPS librabbitmq)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS librabbitmq-dev)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES librabbitmq-devel)
        ENDIF()
        IF(AWSSDK_FOUND)
            CHUCHO_APPEND_NON_PKG_CONFIG_DEPS(AWSSDK_LIBS)
        ENDIF()
        IF(CHUCHO_HAVE_SOCI)
            CHUCHO_APPEND_NON_PKG_CONFIG_DEPS(SOCI_LIBS)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS libsoci-dev)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES soci-devel)
        ENDIF()
        IF(CHUCHO_HAVE_RDKAFKA)
            LIST(APPEND CHUCHO_PKG_CONFIG_PUBLIC_DEPS rdkafka)
            LIST(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS librdkafka-dev)
            LIST(APPEND CPACK_RPM_PACKAGE_REQUIRES librdkafka-devel)
        ENDIF()
        STRING(REPLACE ";" "," CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}")
        STRING(REPLACE ";" "," CPACK_RPM_PACKAGE_REQUIRES "${CPACK_RPM_PACKAGE_REQUIRES}")
        IF(DEFINED CHUCHO_NON_PKG_CONFIG_DEPS)
            LIST(INSERT CHUCHO_NON_PKG_CONFIG_DEPS 0 ${CHUCHO_NON_PKG_CONFIG_DEP_DIRS})
            LIST(REMOVE_DUPLICATES CHUCHO_NON_PKG_CONFIG_DEPS)
            LIST(INSERT CHUCHO_NON_PKG_CONFIG_DEPS 0 "Libs.private:")
            STRING(REPLACE ";" " " CHUCHO_NON_PKG_CONFIG_DEPS "${CHUCHO_NON_PKG_CONFIG_DEPS}")
            STRING(CONCAT CHUCHO_NON_PKG_CONFIG_DEPS ${CHUCHO_NON_PKG_CONFIG_DEPS} "\n")
        ENDIF()
        IF(DEFINED CHUCHO_NON_PKG_CONFIG_CFLAGS)
            LIST(REMOVE_DUPLICATES CHUCHO_NON_PKG_CONFIG_CFLAGS)
            LIST(INSERT CHUCHO_NON_PKG_CONFIG_CFLAGS 0 "Cflags:")
            STRING(REPLACE ";" " " CHUCHO_NON_PKG_CONFIG_CFLAGS "${CHUCHO_NON_PKG_CONFIG_CFLAGS}")
            STRING(CONCAT CHUCHO_NON_PKG_CONFIG_CFLAGS ${CHUCHO_NON_PKG_CONFIG_CFLAGS} "\n")
        ENDIF()
        IF(DEFINED CHUCHO_PKG_CONFIG_PUBLIC_DEPS)
            LIST(INSERT CHUCHO_PKG_CONFIG_PUBLIC_DEPS 0 "Requires:")
            STRING(REPLACE ";" " " CHUCHO_PKG_CONFIG_PUBLIC_DEPS "${CHUCHO_PKG_CONFIG_PUBLIC_DEPS}")
            STRING(CONCAT CHUCHO_PKG_CONFIG_PUBLIC_DEPS ${CHUCHO_PKG_CONFIG_PUBLIC_DEPS} "\n")
        ENDIF()
        IF(DEFINED CHUCHO_PKG_CONFIG_PRIVATE_DEPS)
            LIST(INSERT CHUCHO_PKG_CONFIG_PRIVATE_DEPS 0 "Requires.private:")
            STRING(REPLACE ";" " " CHUCHO_PKG_CONFIG_PRIVATE_DEPS "${CHUCHO_PKG_CONFIG_PRIVATE_DEPS}")
            STRING(CONCAT CHUCHO_PKG_CONFIG_PRIVATE_DEPS ${CHUCHO_PKG_CONFIG_PRIVATE_DEPS} "\n")
        ENDIF()
        CONFIGURE_FILE(libchucho.pc.in "${CMAKE_BINARY_DIR}/libchucho.pc" @ONLY)
        INSTALL(FILES "${CMAKE_BINARY_DIR}/libchucho.pc" DESTINATION lib/pkgconfig)
    ENDIF()
ENDMACRO()
