include(vcpkg_common_functions)
set(WEBRTC_VERSION 20210105)
set(MASTER_COPY_SOURCE_PATH ${CURRENT_BUILDTREES_DIR}/src)

file(READ "${VCPKG_ROOT_DIR}/_env/EXTERNAL_WEBRTC_WIN_URLS.txt" EXTERNAL_WEBRTC_WIN_URLS)
file(READ "${VCPKG_ROOT_DIR}/_env/EXTERNAL_WEBRTC_WIN_SHA512.txt" EXTERNAL_WEBRTC_WIN_SHA512)
file(READ "${VCPKG_ROOT_DIR}/_env/EXTERNAL_WEBRTC_MAC_URLS.txt" EXTERNAL_WEBRTC_MAC_URLS)
file(READ "${VCPKG_ROOT_DIR}/_env/EXTERNAL_WEBRTC_MAC_SHA512.txt" EXTERNAL_WEBRTC_MAC_SHA512)
file(READ "${VCPKG_ROOT_DIR}/_env/EXTERNAL_WEBRTC_LINUX_URLS.txt" EXTERNAL_WEBRTC_LINUX_URLS)
file(READ "${VCPKG_ROOT_DIR}/_env/EXTERNAL_WEBRTC_LINUX_SHA512.txt" EXTERNAL_WEBRTC_LINUX_SHA512)

if (ANDROID)
   # this is handled by hifi_android.py
elseif (WIN32)
    vcpkg_download_distfile(
        WEBRTC_SOURCE_ARCHIVE
        URLS ${EXTERNAL_WEBRTC_WIN_URLS}
        SHA512 ${EXTERNAL_WEBRTC_WIN_SHA512}
        FILENAME webrtc-m84-20210105-windows.zip
    )
elseif (APPLE)
    vcpkg_download_distfile(
        WEBRTC_SOURCE_ARCHIVE
        URLS ${EXTERNAL_WEBRTC_MAC_URLS}
        SHA512 ${EXTERNAL_WEBRTC_MAC_SHA512}
        FILENAME webrtc-m78-osx.tar.gz
    )
else ()
    # else Linux desktop
    vcpkg_download_distfile(
        WEBRTC_SOURCE_ARCHIVE
        URLS ${EXTERNAL_WEBRTC_LINUX_URLS}
        SHA512 ${EXTERNAL_WEBRTC_LINUX_SHA512}
        FILENAME webrtc-m84-gcc-linux.tar.xz
    )
endif ()

vcpkg_extract_source_archive(${WEBRTC_SOURCE_ARCHIVE})

file(COPY ${MASTER_COPY_SOURCE_PATH}/webrtc/include DESTINATION ${CURRENT_PACKAGES_DIR})
file(COPY ${MASTER_COPY_SOURCE_PATH}/webrtc/lib DESTINATION ${CURRENT_PACKAGES_DIR})
file(COPY ${MASTER_COPY_SOURCE_PATH}/webrtc/share DESTINATION ${CURRENT_PACKAGES_DIR})
file(COPY ${MASTER_COPY_SOURCE_PATH}/webrtc/debug DESTINATION ${CURRENT_PACKAGES_DIR})
