abinfo "Gathering list of libraries"
pushd "${PKGDIR}"/usr/lib/hugin
__LIBS=(*.so.*)
__GREP_ARGS=${__LIBS[@]/#/--regexp=}
popd

__add_rpath() {
	FILE="$1"
	if readelf -d "$FILE" 2>/dev/null | grep -q $__GREP_ARGS; then
		abinfo "Adding rpath to $FILE"
		patchelf --set-rpath '/usr/lib/hugin' "$FILE"
	fi
}

abinfo "Adding rpath for executables"
pushd "$PKGDIR"/usr/bin
for i in "$PKGDIR"/usr/bin/* "$PKGDIR"/usr/lib/hugin/*; do
	# If the executable is elf and refers to private libraries...
	__add_rpath "$i"
done
popd

unset __add_rpath
unset __LIBS
unset __GREP_ARGS
