# Utility functions shared by some validator shell scripts

# Returns a list of build flavors in the form "pkg1.spec\npkg2.spec", "package.spec foo\npackage.spec bar",
# for easy consumption by a "while read recipe flavor" loop.
# First argument is the source directory.
spec_build_flavors() {
	pushd "$1" >/dev/null
	for i in *.spec; do
		[ -e "$i" ] && echo "$i"
	done

	if [ -e "_multibuild" ]; then
		xmllint -xpath '(/multibuild/flavor | /multibuild/package)/text()' _multibuild | while read flavor; do
			if [ -e "${flavor}.spec" ]; then
				echo "${flavor}.spec $flavor"
				continue
			fi

			specs=(*.spec)
			if [ "${#specs[@]}" -eq 1 ] && [ -e "${specs[0]}" ]; then
				# Exactly one .spec file
				echo "${specs[0]} $flavor"
			fi
		done
	fi
	popd >/dev/null
}
