if(POLICY CMP0074)
    cmake_policy(SET CMP0074 NEW)
else()
    message(FATAL_ERROR "Policy CMP0074 not vailable. You need CMake 3.12 or newer to use Vcpkg to acquire dependencies!")
endif()

if(DEFINED WIN32) # unfortunately CMAKE_SYSTEM_NAME is not yet set here
    if (NOT DEFINED VCPKG_TARGET_TRIPLET)
        message(FATAL_ERROR "VCPKG_TARGET_TRIPLET not set")
    else()
        if (${VCPKG_TARGET_TRIPLET} MATCHES "x64-windows*")
            if (${VCPKG_TARGET_TRIPLET} STREQUAL "x64-windows-static")
                message(STATUS "Linking statically to C/C++ runtime on Windows")
                if(POLICY CMP0091)
                    cmake_policy(SET CMP0091 NEW)
                    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
                else()
                    message(FATAL_ERROR "Policy CMP0091 not vailable. You need CMake 3.16 or newer to configure static runtime linkage on Windows!")
                endif()
            else()
                if (NOT ${VCPKG_TARGET_TRIPLET} STREQUAL "x64-windows" AND NOT ${VCPKG_TARGET_TRIPLET} STREQUAL "x64-windows-static-md")
                    message(FATAL_ERROR "VCPKG_TARGET_TRIPLET ${VCPKG_TARGET_TRIPLET} not (yet) supported!")
                endif()
            endif()
        else()
            if (${VCPKG_TARGET_TRIPLET} MATCHES "x86-windows*")
                if (${VCPKG_TARGET_TRIPLET} STREQUAL "x86-windows-static")
                    message(STATUS "Linking statically to C/C++ runtime on Windows")
                    if(POLICY CMP0091)
                        cmake_policy(SET CMP0091 NEW)
                        set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
                    else()
                        message(FATAL_ERROR "Policy CMP0091 not vailable. You need CMake 3.16 or newer to configure static runtime linkage on Windows!")
                    endif()
                else()
                    if (NOT ${VCPKG_TARGET_TRIPLET} STREQUAL "x86-windows" AND NOT ${VCPKG_TARGET_TRIPLET} STREQUAL "x86-windows-static-md")
                        message(FATAL_ERROR "VCPKG_TARGET_TRIPLET ${VCPKG_TARGET_TRIPLET} not (yet) supported!")
                    endif()
                endif()
            endif()
        endif()
    endif()
endif()

message(STATUS "Using Vcpkg triplet ${VCPKG_TARGET_TRIPLET}")

if(DEFINED ENV{VCPKG_ROOT})
    if(DEFINED CMAKE_TOOLCHAIN_FILE)
        # need to be equal
        if(NOT ${CMAKE_TOOLCHAIN_FILE} STREQUAL "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
            message(FATAL_ERROR "VCPKG_TARGET_TRIPLET set but also CMAKE_TOOLCHAIN_FILE set (CMAKE_TOOLCHAIN_FILE = ${CMAKE_TOOLCHAIN_FILE}, ENV{VCPKG_ROOT} = $ENV{VCPKG_ROOT})")
        endif()
    else()
        set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
    endif()
else()
    if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
        message(FATAL_ERROR "VCPKG_TARGET_TRIPLET set but neither VCPKG_ROOT environment variable available nor CMAKE_TOOLCHAIN_FILE defined")
    endif()
endif()

message(STATUS "Using CMAKE_TOOLCHAIN_FILE = ${CMAKE_TOOLCHAIN_FILE}")
