if(NOT CPM_DOWNLOAD_VERSION)
  set(CPM_DOWNLOAD_VERSION 0.27.3)
endif()

set(CPM_DOWNLOAD_FILE "CPM_${CPM_DOWNLOAD_VERSION}.cmake")

if(CPM_SOURCE_CACHE)
  set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/${CPM_DOWNLOAD_FILE}")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
  set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/${CPM_DOWNLOAD_FILE}")
else()
  set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/${CPM_DOWNLOAD_FILE}")
endif()

set(CPM_DOWNLOAD_URL_BASE
    "https://github.com/TheLartians/CPM.cmake/releases/download")

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
  message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
  file(DOWNLOAD ${CPM_DOWNLOAD_URL_BASE}/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
       ${CPM_DOWNLOAD_LOCATION})
endif()

include(${CPM_DOWNLOAD_LOCATION})
