set(CURL_PROJECT_NAME curl_${CURL_VERSION})
set(CURL_BUILD_ROOT ${EXTERNAL_BUILD_ROOT}/builds/${CURL_PROJECT_NAME})

if (OPENSSL_ROOT_DIR)
  set(CURL_OPENSSL_EXTRA -DOPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR})
elseif(LINUX OR MINGW)
  set(CURL_OPENSSL_EXTRA -DOPENSSL_ROOT_DIR=${EXTERNAL_BUILD_ROOT})
endif()

set(CURL_CMAKE_ARGS
  ${CURL_OPENSSL_EXTRA}
  -DBUILD_CURL_EXE=OFF
  -DBUILD_SHARED_LIBS=OFF
  -DBUILD_TESTING=OFF
  -DCMAKE_BUILD_TYPE=${EXTERNAL_BUILD_TYPE}
  -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
  -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
  -DCMAKE_GENERATOR=${CMAKE_GENERATOR}
  -DCMAKE_INSTALL_PREFIX=${EXTERNAL_BUILD_ROOT}
  -DCMAKE_POSITION_INDEPENDENT_CODE=${CMAKE_POSITION_INDEPENDENT_CODE}
  -DCMAKE_SHARED_LINKER_FLAGS=${CMAKE_SHARED_LINKER_FLAGS}
  -DCURL_CA_BUNDLE=./cacert.pem
  -DCURL_CA_FALLBACK=ON
  -DCURL_DISABLE_LDAP=ON
  -DCURL_STATICLIB=ON
  -DCURL_USE_LIBPSL=OFF
  -DCURL_USE_LIBSSH2=OFF
  -DCURL_ZLIB=OFF
  -DENABLE_THREADED_RESOLVER=ON
  -DOPENSSL_USE_STATIC_LIBS=ON
  -DUSE_LIBIDN2=OFF
)

if (CMAKE_TOOLCHAIN_FILE)
  set(CURL_CMAKE_ARGS
    ${CURL_CMAKE_ARGS}
    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} 
  )
endif()

if (MINGW)
  set(CURL_CMAKE_ARGS
    ${CURL_CMAKE_ARGS}
    -DCURL_USE_OPENSSL=OFF
    -DUSE_WIN32_CRYPTO=ON
  )
else()
  set(CURL_CMAKE_ARGS
    ${CURL_CMAKE_ARGS}
    -DCURL_USE_OPENSSL=ON
  )
endif()

ExternalProject_Add(curl_project
  DOWNLOAD_NO_PROGRESS 1
  URL https://github.com/curl/curl/archive/curl-${CURL_VERSION}.tar.gz
  PREFIX ${CURL_BUILD_ROOT}
  CMAKE_ARGS ${CURL_CMAKE_ARGS}
)

set(REPERTORY_DEFINITIONS ${REPERTORY_DEFINITIONS} -DCURL_STATICLIB=ON -DCURL_DISABLE_LDAP=ON)

set(CURL_LIBRARIES libcurl${DEBUG_EXTRA2}${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(curl_project 
  openssl_project
  zlib_project
)
