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

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


if(TARGET ${TGT_ARILES_VISITOR_LIB})
    add_dependencies(${VISITOR_TARGET_PREFIX}_${ARILES_VISITOR} ${TGT_ARILES_VISITOR_LIB})
    add_dependencies(${TGT_ARILES_VISITOR_LIB} TGT_ariles_copy_headers)

    # built-tree specific targets, not relocatable
    #export(TARGETS ${TGT_ARILES_VISITOR_LIB} FILE ${PROJECT_BINARY_DIR}/ariles-${ARILES_COMPONENT}-targets.cmake)
    #target_link_options(${TGT_ARILES_VISITOR_LIB} PRIVATE "LINKER:--exclude-libs=${ARILES_VISITOR_${ARILES_VISITOR}_LIBS}")

    set_target_properties(${TGT_ARILES_VISITOR_LIB} PROPERTIES LINK_LIBRARIES "${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)

    target_include_directories(${TGT_ARILES_VISITOR_LIB} PUBLIC
        $<BUILD_INTERFACE:${ARILES_CORE_BUILD_INCLUDES}/>
    )
    target_include_directories(${TGT_ARILES_VISITOR_LIB} PUBLIC
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/extra_visitors/${ARILES_VISITOR}/>
    )

    install(
        TARGETS ${TGT_ARILES_VISITOR_LIB} EXPORT ${ARILES_COMPONENT}_targets
        LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
        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/ariles-${ARILES_COMPONENT}/
        DESTINATION ${ARILES_COMPONENT_CMAKE_DIR}
        FILES_MATCHING PATTERN "${ARILES_COMPONENT}_targets-*.cmake"
    )

    set(ARILES_VISITOR_${ARILES_VISITOR}_LIBS "${TGT_ARILES_VISITOR_LIB};${ARILES_VISITOR_${ARILES_VISITOR}_LIBS}")
else()
    set_property(
        TARGET ${VISITOR_TARGET_PREFIX}_${ARILES_VISITOR}
        APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
        "${ARILES_CORE_BUILD_INCLUDES};${PROJECT_SOURCE_DIR}/extra_visitors/${ARILES_VISITOR}/")
endif()


set(ariles_LIBRARIES "ariles-${ARILES_COMPONENT}_LIBRARIES")
set(ariles_INCLUDE_DIRS "ariles-${ARILES_COMPONENT}_INCLUDE_DIRS")
set(ariles_LIBRARY_DIRS "ariles-${ARILES_COMPONENT}_LIBRARY_DIRS")

set(ARILES_LIBRARIES            "${ARILES_VISITOR_${ARILES_VISITOR}_LIBS}")
set(ARILES_INCLUDES             "")
set(ARILES_DEPENDENCY_INCLUDES  "")
set(ARILES_LIBRARY_DIRS         "${ARILES_VISITOR_${ARILES_VISITOR}_LIBRARY_DIRS}")


list(REMOVE_DUPLICATES ARILES_INCLUDES)
list(REMOVE_DUPLICATES ARILES_DEPENDENCY_INCLUDES)
list(REMOVE_DUPLICATES ARILES_LIBRARIES)
list(REMOVE_ITEM ARILES_INCLUDES "")
list(REMOVE_ITEM ARILES_DEPENDENCY_INCLUDES "")
list(REMOVE_ITEM ARILES_LIBRARIES "")


if (ARILES_INCLUDES)
    configure_package_config_file(  "${PROJECT_SOURCE_DIR}/cmake/arilesConfig.cmake.in"
                                    "${PROJECT_BINARY_DIR}/ariles-${ARILES_COMPONENT}-Config.cmake"
                                    INSTALL_DESTINATION "${ARILES_COMPONENT_CMAKE_DIR}"
                                    PATH_VARS ARILES_INCLUDES
                                    NO_SET_AND_CHECK_MACRO
                                    NO_CHECK_REQUIRED_COMPONENTS_MACRO)
else()
    configure_package_config_file(  "${PROJECT_SOURCE_DIR}/cmake/arilesConfig.cmake.in"
                                    "${PROJECT_BINARY_DIR}/ariles-${ARILES_COMPONENT}Config.cmake"
                                    INSTALL_DESTINATION "${ARILES_COMPONENT_CMAKE_DIR}"
                                    NO_SET_AND_CHECK_MACRO
                                    NO_CHECK_REQUIRED_COMPONENTS_MACRO)
endif()

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

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

if(ARILES_PKGCONFIG_INSTALL_PATH)
    string(REPLACE ";" " -I" ARILES_INCLUDES_FLAGS "${ARILES_INCLUDES}")
    set(ARILES_INCLUDES_FLAGS "-I${ARILES_INCLUDES_FLAGS}")
    string(REPLACE ";" " -l" ARILES_LIBRARIES_FLAGS "${ARILES_LIBRARIES}")
    string(REPLACE ";" " -L" ARILES_LIBRARIES_FLAGS_DIRS "${ARILES_LIBRARY_DIRS}")
    set(ARILES_LIBRARIES_FLAGS "-l${ARILES_LIBRARIES_FLAGS} -L${ARILES_LIBRARIES_FLAGS_DIRS}")
    configure_file("cmake/ariles.pc.in"             "${PROJECT_BINARY_DIR}/ariles-${ARILES_COMPONENT}.pc" @ONLY)

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

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

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