#!/bin/sh
#
# fix pkgconfig files to trim slashes, as ie. libdir=/usr/lib64/ rather than
# libdir=/usr/lib64 will make pkg-config --libs return -L/usr/lib64
#
# (c) Per Øyvind Karlsen <proyvind@moondrake.org> 2015
# (c) Andrey Bondrov <andrey.bondrov@rosalab.ru> 2016

if [ -z "$RPM_BUILD_ROOT" ]; then
	printf '%s\n' "No build root defined" >&2
	exit 1
fi

if [ ! -d "$RPM_BUILD_ROOT" ]; then
	printf '%s\n' "Invalid build root" >&2
	exit 1
fi

for pc in $(find "$RPM_BUILD_ROOT" -name \*.pc -type f); do
	export PKG_CONFIG_PATH="$(dirname $pc)"
	pcfile="$(basename $pc)"
	pcmodule="$(printf '%s\n' $pcfile|cut -d. -f1)"
	for variable in $(pkg-config --print-variables "$pcmodule"); do
		dir=$(pkg-config --variable="$variable" "$pcmodule")
		if [[ "$dir" == "/"* && -d "$dir" ]]; then
			if [ "$dir" != "$(realpath $dir)" ]; then
				parentdir=$(dirname ${dir})
				subdir=$(basename ${dir})
				regexp="-e s|\(${variable}=.*/${subdir}\)/.*\$|\1|g" 
				sed $regexp $pc |tr -s // / > $pc.new
				mv -f $pc.new $pc
			fi
		fi
	done
done
