include(ExternalProject)

if (BUILD_PREFER_STATIC_LIBRARIES)
	add_definitions("-DZMQ_STATIC")
	set(CMAKE_PARAM_SHARED "-DBUILD_SHARED_LIBS=OFF")
	if ("${CMAKE_GENERATOR}" STREQUAL "Xcode")
		set(LIBRARY_SUFFIX "a")
		set(LIBRARY_PREFIX "Debug/")
	elseif (UNIX)
		set(LIBRARY_PREFIX "")
		set(LIBRARY_SUFFIX "a")
	elseif(WIN32)
		set(LIBRARY_SUFFIX "lib")
	endif()
	set(LIBZMQ_LIBRARY ${CMAKE_BINARY_DIR}/deps/libzmq/src/libzmq-build/lib/${LIBRARY_PREFIX}libzmq.${LIBRARY_SUFFIX})
else()
	set(CMAKE_PARAM_SHARED "-DBUILD_SHARED_LIBS=ON")
	if ("${CMAKE_GENERATOR}" STREQUAL "Xcode")
		set(LIBRARY_SUFFIX "dylib")
		set(LIBRARY_PREFIX "${CMAKE_BUILD_TYPE}/")
	elseif (APPLE)
		set(LIBRARY_SUFFIX "dylib")
	elseif (UNIX)
		set(LIBRARY_SUFFIX "so")
	elseif(WIN32)
		set(LIBRARY_SUFFIX "dll")
	endif()
	set(LIBZMQ_LIBRARY ${CMAKE_BINARY_DIR}/deps/libzmq/src/libzmq-build/lib/libzmq.${LIBRARY_SUFFIX})
endif()

if (NOT LIBRARY_SUFFIX)
	message(FATAL_ERROR "Unknown platform!")
endif()

set(ZeroMQ_INCLUDE_DIR ${CMAKE_BINARY_DIR}/deps/libzmq/src/libzmq/include)
set(LIBZMQ_BUILT ON)

if (CMAKE_TOOLCHAIN_FILE)
	set(CMAKE_PARAM_TOOLCHAIN "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")
endif()
if (ANDROID_ABI)
	set(CMAKE_PARAM_ANDROID_ABI "-DANDROID_ABI=${ANDROID_ABI}")
	set(ZMQ_HAVE_SOCK_CLOEXEC "-DZMQ_HAVE_SOCK_CLOEXEC=OFF")
	set(ZMQ_HAVE_SO_KEEPALIVE "-DZMQ_HAVE_SO_KEEPALIVE_EXITCODE=ON")
	set(ZMQ_HAVE_TCP_KEEPCNT "-DZMQ_HAVE_TCP_KEEPCNT=ON")
	set(ZMQ_HAVE_TCP_KEEPIDLE "-DZMQ_HAVE_TCP_KEEPIDLE=ON")
	set(ZMQ_HAVE_TCP_KEEPINTVL "-DZMQ_HAVE_TCP_KEEPINTVL=ON")
	set(ZMQ_HAVE_TCP_KEEPALIVE "-DZMQ_HAVE_TCP_KEEPALIVE=ON")

endif()

if (IOS)
	set(ZMQ_HAVE_SOCK_CLOEXEC "-DZMQ_HAVE_SOCK_CLOEXEC=OFF")
	set(ZMQ_HAVE_SO_KEEPALIVE "-DZMQ_HAVE_SO_KEEPALIVE_EXITCODE=ON")
	set(ZMQ_HAVE_TCP_KEEPCNT "-DZMQ_HAVE_TCP_KEEPCNT=ON")
	set(ZMQ_HAVE_TCP_KEEPIDLE "-DZMQ_HAVE_TCP_KEEPIDLE=ON")
	set(ZMQ_HAVE_TCP_KEEPINTVL "-DZMQ_HAVE_TCP_KEEPINTVL=ON")
	set(ZMQ_HAVE_TCP_KEEPALIVE "-DZMQ_HAVE_TCP_KEEPALIVE=ON")
endif()

if (ANDROID_NATIVE_API_LEVEL)
	set(CMAKE_PARAM_API_LEVEL "-DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}")
endif()


externalproject_add(libzmq
	URL https://github.com/zeromq/zeromq4-1/releases/download/v4.1.5/zeromq-4.1.5.zip
	URL_MD5 e6e2e3a7a07a04057fd2c9a1e519cca8
	BUILD_IN_SOURCE 0
	INSTALL_COMMAND ""
	CONFIGURE_COMMAND 
		${CMAKE_COMMAND} 
		-G ${CMAKE_GENERATOR}
		-D CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
		${CMAKE_PARAM_TOOLCHAIN} 
		${CMAKE_PARAM_ANDROID_ABI}
		${CMAKE_PARAM_API_LEVEL}
		${CMAKE_PARAM_SHARED}
		${ZMQ_HAVE_SO_KEEPALIVE}
		${ZMQ_HAVE_SOCK_CLOEXEC}
		${ZMQ_HAVE_TCP_KEEPCNT}
		${ZMQ_HAVE_TCP_KEEPIDLE}
		${ZMQ_HAVE_TCP_KEEPINTVL}
		${ZMQ_HAVE_TCP_KEEPALIVE}
		-DCMAKE_VERBOSE_MAKEFILE=OFF
		-DWITH_LIBSODIUM=OFF
		-DWITH_OPENPGM=OFF
		-DZMQ_BUILD_FRAMEWORK=OFF
		-DENABLE_CURVE=OFF
		-DZMQ_BUILD_TESTS=OFF
		<SOURCE_DIR>
	PATCH_COMMAND
		${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/contrib/patches/libzmq/version.rc.in" <SOURCE_DIR>/src/version.rc.in &&
		${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/contrib/patches/libzmq/CMakeLists.txt" <SOURCE_DIR>/CMakeLists.txt
	PREFIX ${CMAKE_BINARY_DIR}/deps/libzmq
)
