string(REGEX REPLACE "_" "-" ARILES_COMPONENT "${ARILES_VISITOR}")
set(ARILES_COMPONENT_CMAKE_DIR "share/${PROJECT_NAME}-${ARILES_COMPONENT}/")

cpack_add_component(
    "${ARILES_COMPONENT}"
    DISPLAY_NAME "'${ARILES_VISITOR}' support for ${PROJECT_NAME}"
    DESCRIPTION "Enables support for '${ARILES_VISITOR}' in ${PROJECT_NAME}"
    DEPENDS "core"
    ARCHIVE_FILE "${CPACK_PACKAGE_NAME}-${ARILES_COMPONENT}-${CPACK_PACKAGE_VERSION}")

# ---
get_target_property(TARGET_TYPE ${TGT_ARILES_VISITOR_LIB} TYPE)

if(TARGET_TYPE STREQUAL "SHARED_LIBRARY" OR TARGET_TYPE STREQUAL "STATIC_LIBRARY")
    target_link_libraries(${TGT_ARILES_VISITOR_LIB} PUBLIC ${PROJECT_NAME}-core)

    target_include_directories(${TGT_ARILES_VISITOR_LIB} PUBLIC
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/extra_visitors/${ARILES_VISITOR}/>
    )
    target_include_directories(${TGT_ARILES_VISITOR_LIB} SYSTEM PRIVATE ${ARILES_VISITOR_${ARILES_VISITOR}_INCLUDES})

    target_link_libraries(${TGT_ARILES_VISITOR_LIB} PRIVATE ${ARILES_VISITOR_${ARILES_VISITOR}_LIBS})

    set_target_properties(${TGT_ARILES_VISITOR_LIB} PROPERTIES CXX_VISIBILITY_PRESET hidden)
    set_target_properties(${TGT_ARILES_VISITOR_LIB} PROPERTIES VISIBILITY_INLINES_HIDDEN YES)
else()
    target_link_libraries(${TGT_ARILES_VISITOR_LIB} INTERFACE ${PROJECT_NAME}-core)

    target_include_directories(${TGT_ARILES_VISITOR_LIB} INTERFACE
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/extra_visitors/${ARILES_VISITOR}/>
    )
endif()

target_include_directories(${TGT_ARILES_VISITOR_LIB} INTERFACE
    "$<INSTALL_INTERFACE:include/>"
    "$<INSTALL_INTERFACE:${${PROJECT_NAME}-${ARILES_COMPONENT}_INCLUDE_DIRS}>"
)
# ---


install(
    TARGETS ${TGT_ARILES_VISITOR_LIB} EXPORT ${ARILES_COMPONENT}_targets
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    COMPONENT ${ARILES_COMPONENT}
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    COMPONENT ${ARILES_COMPONENT}
)

install(
    EXPORT ${ARILES_COMPONENT}_targets
    DESTINATION ${ARILES_COMPONENT_CMAKE_DIR}
    COMPONENT ${ARILES_COMPONENT}
)

# BUG? if CMAKE_BUILD_TYPE is not set explicitly, configuration files are not installed
install(
    DIRECTORY ${PROJECT_BINARY_DIR}/CMakeFiles/Export/share/${PROJECT_NAME}-${ARILES_COMPONENT}/
    DESTINATION ${ARILES_COMPONENT_CMAKE_DIR}
    COMPONENT ${ARILES_COMPONENT}
    FILES_MATCHING PATTERN "${ARILES_COMPONENT}_targets-*.cmake"
)

set(ARILES_VISITOR_${ARILES_VISITOR}_LIBS "${TGT_ARILES_VISITOR_LIB};${ARILES_VISITOR_${ARILES_VISITOR}_LIBS}")

add_dependencies(${VISITOR_TARGET_PREFIX}_${ARILES_VISITOR} ${TGT_ARILES_VISITOR_LIB})


set(ariles_LIBRARIES "${PROJECT_NAME}-${ARILES_COMPONENT}_LIBRARIES")
set(ARILES_LIBRARIES "${TGT_ARILES_VISITOR_LIB}")
set(ARILES_FIND_CORE "find_package(${PROJECT_NAME}-core REQUIRED)")
if(ARILES_VISITOR_${ARILES_VISITOR}_LIBRARY_DIRS)
    set(ARILES_LINK_DIRECTORIES "link_directories(\"${ARILES_VISITOR_${ARILES_VISITOR}_LIBRARY_DIRS}\")")
endif()

configure_package_config_file(  "${PROJECT_SOURCE_DIR}/cmake/arilesConfig.cmake.in"
                                "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-${ARILES_COMPONENT}Config.cmake"
                                INSTALL_DESTINATION "${ARILES_COMPONENT_CMAKE_DIR}"
                                NO_SET_AND_CHECK_MACRO
                                NO_CHECK_REQUIRED_COMPONENTS_MACRO)

install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-${ARILES_COMPONENT}Config.cmake"
        DESTINATION "${ARILES_COMPONENT_CMAKE_DIR}"
        COMPONENT ${ARILES_COMPONENT})

write_basic_package_version_file(
    ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-${ARILES_COMPONENT}ConfigVersion.cmake
    VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
    COMPATIBILITY SameMajorVersion)
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-${ARILES_COMPONENT}ConfigVersion.cmake"
        DESTINATION "${ARILES_COMPONENT_CMAKE_DIR}"
        COMPONENT ${ARILES_COMPONENT})

if(ARILES_PKGCONFIG_INSTALL_PATH)
    set(ARILES_LIBRARIES            "${ARILES_VISITOR_${ARILES_VISITOR}_LIBS}")
    list(REMOVE_DUPLICATES ARILES_LIBRARIES)
    list(REMOVE_ITEM ARILES_LIBRARIES "")

    string(REPLACE ";" " -l" ARILES_LIBRARIES_FLAGS "${ARILES_LIBRARIES}")
    string(REPLACE ";" " -L" ARILES_LIBRARIES_FLAGS_DIRS "${ARILES_VISITOR_${ARILES_VISITOR}_LIBRARY_DIRS}")
    set(ARILES_LIBRARIES_FLAGS "-l${ARILES_LIBRARIES_FLAGS} -L${ARILES_LIBRARIES_FLAGS_DIRS}")
    configure_file("cmake/ariles.pc.in"             "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-${ARILES_COMPONENT}.pc" @ONLY)

    install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-${ARILES_COMPONENT}.pc"
            DESTINATION "${ARILES_PKGCONFIG_INSTALL_PATH}"
            COMPONENT ${ARILES_COMPONENT})
endif()


install (DIRECTORY "${PROJECT_SOURCE_DIR}/extra_visitors/${ARILES_VISITOR}/${PROJECT_NAME}/"
         DESTINATION "include/${PROJECT_NAME}/"
         COMPONENT "${ARILES_COMPONENT}")

set("DEB_CMAKE_FLAGS_${ARILES_COMPONENT}" "-DARILES_VISITOR_${ARILES_VISITOR}=ON -DARILES_ENABLE_CORE=OFF")

set(TGT_ARILES_VISITOR_LIB "")
set(ARILES_FIND_CORE "")
set(ARILES_LINK_DIRECTORIES "")
