#!/bin/sh -euf

# Produce a version-switching cmake config in /usr.
# The config, if invoked by CMake, will look up ALTWRAP_LLVM_VERSION in the
# environment, substitute the prefix for that version and invoke the respective
# upstream CMake file.

make_proxy_config() {
spath="$1"
shift

prefix=/usr
bn="$(basename "$spath")"
mkdir -vp "$RPM_BUILD_ROOT${spath%$bn}"
echo "cat > $RPM_BUILD_ROOT$spath <<@@@EndOfNewCMakeConfig"
tee <<@@@EndOfNewCMakeConfig $RPM_BUILD_ROOT$spath
# This file is produced by ALT packaging.
# It wraps the real $bn, which is installed along with the version
# of LLVM requested with ALTWRAP_LLVM_VERSION environment variable.

if(NOT DEFINED ENV{ALTWRAP_LLVM_VERSION} OR ENV{ALTWRAP_LLVM_VERSION} MATCHES "^[ \t\r\n]+$")
	set(ENV{ALTWRAP_LLVM_VERSION} $RPM_LLVM_VERSION)
endif()
include("/usr/lib/llvm-\$ENV{ALTWRAP_LLVM_VERSION}${spath#$prefix}")
@@@EndOfNewCMakeConfig
echo "@@@EndOfNewCMakeConfig"
}

for i; do
	make_proxy_config "$i"
done
