#! /bin/bash
#
# Native version of pkg-config, adding native sysroot to installed packages
#

platform=${0%/sysroots/native/*}

# Export the cross-pkg-config environment, for the provided sysroot/bin/<target-triplet>-pkg-config
export_cross_pkg_config_setup() {
	local platform="$1"
	local sysroot=$platform/sysroots/native

#	PKG_CONFIG_LIBDIR=$platform_sysroot/usr/lib/pkgconfig
	PKG_CONFIG_PATH=
	local subdir sep=
	for subdir in /usr/lib /usr/share /usr/local/lib /usr/local/share; do
		dir="${sysroot}/${subdir}/pkgconfig"
		if [ -d "$dir" ]; then
			PKG_CONFIG_PATH="${PKG_CONFIG_PATH}${sep}${dir}"
			sep=':'
		fi
	done

	PKG_CONFIG_DISABLE_UNINSTALLED=set
#	PKG_CONFIG_SYSROOT_DIR="$sysroot"
#	PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=set
#	PKG_CONFIG_ALLOW_SYSTEM_LIBS=set

#	export PKG_CONFIG_LIBDIR
	export PKG_CONFIG_PATH
	export PKG_CONFIG_DISABLE_UNINSTALLED
#	export PKG_CONFIG_SYSROOT_DIR
#	export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS
#	export PKG_CONFIG_ALLOW_SYSTEM_LIBS
}
export_cross_pkg_config_setup $platform $cross_name

for path in ${PATH//:/ }; do
	if [[ " $path" =~ " $platform/sysroots/native/" ]]; then continue; fi
	if [ -x ${path%/}/pkg-config ]; then
		exec ${path%/}/pkg-config "$@" --define-prefix
	fi
done

echo "pkg-config: File not found" >&2
exit 1
