#!/usr/bin/bash
# This script redefines the macros that we need to overwrite based on the macro
# bodies that we anticipate.
# Author: Konrad Kleine <kkleine@redhat.com>
# Copyright (c) 2023 Red Hat.
set -e
# Adds a backslash to each new line unless it is a %{nil} line
function add_backslash() {
sed '/^%{nil}/!s,$, \\,'
}
rpm --eval "%%set_build_flags %{macrobody:set_build_flags}" \
| add_backslash
echo "%{?__llvm_pgo_instrumented_build_flags}"
echo
echo
rpm --eval "%%__spec_build_pre %{macrobody:__spec_build_pre}" \
| add_backslash
echo "%{?__llvm_pgo_instrumented_spec_build_pre}"
echo
echo
cat <<EOF
# Overriding __spec_build_post macro from /usr/lib/rpm/macros
%__spec_build_post \
%{?__llvm_pgo_instrumented_spec_build_post} \
%{___build_post}
EOF
echo
echo
rpm --eval "%%__os_install_post %{macrobody:__os_install_post}" \
| add_backslash \
| sed 's/%{nil}/%{__llvm_pgo_instrumented_os_install_post} \\\n%{nil}/'
echo
echo
rpm --eval "%%install %{macrobody:install}" \
| add_backslash \
| sed 's/%%install/%{__llvm_pgo_instrumented_install} \\\n%%install/'