if (OS_LINUX)
    option(ENABLE_ICU "Enable ICU" ${ENABLE_LIBRARIES})
else ()
    option(ENABLE_ICU "Enable ICU" 0)
endif ()

if (NOT ENABLE_ICU)
    if(USE_INTERNAL_ICU_LIBRARY)
        message (${RECONFIGURE_MESSAGE_LEVEL} "Cannot use internal icu library with ENABLE_ICU=OFF")
    endif()
    message(STATUS "Build without ICU (support for collations and charset conversion functions will be disabled)")
    return()
endif()

option (USE_INTERNAL_ICU_LIBRARY "Set to FALSE to use system ICU library instead of bundled" ${NOT_UNBUNDLED})

if (NOT EXISTS "${ClickHouse_SOURCE_DIR}/contrib/icu/icu4c/LICENSE")
    if (USE_INTERNAL_ICU_LIBRARY)
        message (WARNING "submodule contrib/icu is missing. to fix try run: \n git submodule update --init")
        message (${RECONFIGURE_MESSAGE_LEVEL} "Can't find internal ICU")
        set (USE_INTERNAL_ICU_LIBRARY 0)
    endif ()
    set (MISSING_INTERNAL_ICU_LIBRARY 1)
endif ()

if(NOT USE_INTERNAL_ICU_LIBRARY)
    if (APPLE)
        set(ICU_ROOT "/usr/local/opt/icu4c" CACHE STRING "")
    endif()
    find_package(ICU COMPONENTS i18n uc data) # TODO: remove Modules/FindICU.cmake after cmake 3.7
    #set (ICU_LIBRARIES ${ICU_I18N_LIBRARY} ${ICU_UC_LIBRARY} ${ICU_DATA_LIBRARY} CACHE STRING "")
    if(ICU_FOUND)
        set(USE_ICU 1)
    else()
        message (${RECONFIGURE_MESSAGE_LEVEL} "Can't find system ICU")
    endif()
endif()

if (ICU_LIBRARY AND ICU_INCLUDE_DIR)
    set (USE_ICU 1)
elseif (NOT MISSING_INTERNAL_ICU_LIBRARY)
    set (USE_INTERNAL_ICU_LIBRARY 1)
    set (ICU_LIBRARIES icui18n icuuc icudata)
    set (USE_ICU 1)
endif ()

if(USE_ICU)
    message(STATUS "Using icu=${USE_ICU}: ${ICU_INCLUDE_DIR} : ${ICU_LIBRARIES}")
else()
    message(STATUS "Build without ICU (support for collations and charset conversion functions will be disabled)")
endif()
