include("${CMAKE_CURRENT_LIST_DIR}/RealmTargets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/AcquireRealmDependency.cmake")

# Find dependencies
include(CMakeFindDependencyMacro)

if(ON)
    if(NOT OFF AND (ANDROID OR WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux"))
        # Use our own prebuilt OpenSSL
        realm_acquire_dependency(openssl 1.1.1w OPENSSL_CMAKE_INCLUDE_FILE)

        include(${OPENSSL_CMAKE_INCLUDE_FILE})
    endif()
    find_dependency(OpenSSL 1.1.1w)
endif()

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_dependency(Threads)

# Use Zlib for Sync, but allow integrators to override it
# Don't use find_library(ZLIB) on Apple platforms - it hardcodes the path per platform,
# so for an iOS build it'll use the path from the Device plaform, which is an error on Simulator.
# Just use -lz and let Xcode figure it out
if(TARGET Realm::Sync AND NOT APPLE AND NOT TARGET ZLIB::ZLIB)
    if(WIN32 OR (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND REALM_LINUX_TOOLCHAIN))
        find_package(ZLIB)
        if (NOT ZLIB_FOUND)
            realm_acquire_dependency(zlib 1.2.13 ZLIB_CMAKE_INCLUDE_FILE)
            include(${ZLIB_CMAKE_INCLUDE_FILE})
        endif()
    elseif(ANDROID)
        # On Android FindZLIB chooses the static libz over the dynamic one, but this leads to issues
        # (see https://github.com/android/ndk/issues/1179)
        # We want to link against the stub library instead of statically linking anyway,
        # so we hack find_library to only consider shared object libraries when looking for libz
        set(_CMAKE_FIND_LIBRARY_SUFFIXES_orig ${CMAKE_FIND_LIBRARY_SUFFIXES})
        set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
    endif()
    find_dependency(ZLIB)
    if(ANDROID)
        set(CMAKE_FIND_LIBRARY_SUFFIXES ${_CMAKE_FIND_LIBRARY_SUFFIXES_orig})
    endif()
endif()
