# Copyright 2019-2022, Collabora, Ltd.
# Copyright 2019, Benjamin Saunders <ben.e.saunders@gmail.com>
# SPDX-License-Identifier: BSL-1.0

# Get input from main CMake script
set(MANIFEST_TEMPLATE @_genmanifest_MANIFEST_TEMPLATE@)
set(DESTINATION @_genmanifest_DESTINATION@)
set(OUT_FILENAME @_genmanifest_OUT_FILENAME@)
set(RUNTIME_TARGET @_genmanifest_RUNTIME_TARGET@)
set(INTERMEDIATE_MANIFEST @_genmanifest_INTERMEDIATE_MANIFEST@)

# Runtime install dir relative to install prefix
set(RELATIVE_RUNTIME_DIR @_genmanifest_RELATIVE_RUNTIME_DIR@)
# Runtime so/dll filename
set(RUNTIME_FILENAME @RUNTIME_FILENAME@)
# The relative path from the manifest dir to the runtime. Optional.
set(RUNTIME_DIR_RELATIVE_TO_MANIFEST
    @_genmanifest_RUNTIME_DIR_RELATIVE_TO_MANIFEST@)
# Config option
set(ABSOLUTE_RUNTIME_PATH @_genmanifest_ABSOLUTE_RUNTIME_PATH@)

if(ABSOLUTE_RUNTIME_PATH)
    # Absolute path to runtime
    message(STATUS "Installing OpenXR runtime manifest with absolute path to runtime")
    set(RUNTIME_PATH ${RELATIVE_RUNTIME_DIR}/${RUNTIME_FILENAME})
    if(NOT IS_ABSOLUTE ${RELATIVE_RUNTIME_DIR})
        set(RUNTIME_PATH ${CMAKE_INSTALL_PREFIX}/${RUNTIME_PATH})
    endif()
elseif(RUNTIME_DIR_RELATIVE_TO_MANIFEST)
    # Relative path to runtime.
    message(STATUS "Installing OpenXR runtime manifest with JSON-relative path to runtime")
    set(RUNTIME_PATH ${RUNTIME_DIR_RELATIVE_TO_MANIFEST}/${RUNTIME_FILENAME})
else()
    # Unqualified runtime filename: requires it exist on the system shared library search path.
    message(STATUS "Installing OpenXR runtime manifest with unqualified runtime filename (uses system search path)")
    set(RUNTIME_PATH ${RUNTIME_FILENAME})
endif()

if(WIN32)
	string(REPLACE "/" [[\\]] RUNTIME_PATH ${RUNTIME_PATH})
endif()

set(runtime_path ${RUNTIME_PATH})

if("x${CMAKE_INSTALL_COMPONENT}x" STREQUAL "xUnspecifiedx"
   OR NOT CMAKE_INSTALL_COMPONENT)

    # Create manifest
    configure_file("${MANIFEST_TEMPLATE}" "${INTERMEDIATE_MANIFEST}")

    # Install it
    file(
        INSTALL
        DESTINATION "${CMAKE_INSTALL_PREFIX}/${DESTINATION}"
        TYPE FILE FILES "${INTERMEDIATE_MANIFEST}")
endif()