#!/bin/sh -e
#
# Copyright (C) 2002-2003  Dmitry V. Levin <ldv@altlinux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

. /usr/lib/rpm/functions
ValidateBuildRoot

FindPackage()
{
	local f r rep package
	f="$1"
	shift

	for r in "$@"; do
		[ -n "$r" ] || continue
		if [ -z "${r##/*}" ]; then
			# Dependence name starts with `/'.

			# Does it start with buildroot?
			if [ -z "${r##$RPM_BUILD_ROOT*}" ]; then
				Info "$f: invalid dependence: $r"
				return 1
			fi

			rep="$r"
			# Does it belong to buildroot?
			if [ -e "$RPM_BUILD_ROOT$rep" ]; then
				continue
			fi

			# Is it an alternative?
			if readlink "$rep" |grep -qs '^/etc/alternatives/'; then
				printf %s\\n "$rep"
				continue
			fi

			# Check for pkg contents binary index.
			if [ -n "$RPM_PKG_CONTENTS_INDEX_BIN" ] &&
			   [ -s "$RPM_PKG_CONTENTS_INDEX_BIN" ] &&
			   [ -r "$RPM_PKG_CONTENTS_INDEX_BIN" ]; then
				package="$(awk -v "f=$rep" '{if ($1 == f) {print $2; exit}}' "$RPM_PKG_CONTENTS_INDEX_BIN")"
				if [ -n "$package" ]; then
					printf %s\\n "$package"
					continue
				fi
			fi

			# Check for pkg contents complete index.
			if [ -n "$RPM_PKG_CONTENTS_INDEX_ALL" ] &&
			   [ -s "$RPM_PKG_CONTENTS_INDEX_ALL" ] &&
			   [ -r "$RPM_PKG_CONTENTS_INDEX_ALL" ]; then
				package="$(awk -v "f=$rep" '{if ($1 == f) {print $2; exit}}' "$RPM_PKG_CONTENTS_INDEX_ALL")"
				if [ -n "$package" ]; then
					printf %s\\n "$package"
					continue
				fi
			fi

			# Check package database.
			if package="$(rpmquery --whatprovides --queryformat='%{NAME}\n' -- "$rep" |LC_COLLATE=C sort -u)"; then
				if [ -n "$package" ]; then
					printf %s\\n "$package"
					continue
				fi
			fi

			# Not found; output raw dependence.
			printf %s\\n "$rep"
		else
			# Check buildroot first.
			local RPATH
			RPATH="$(printf %s "$PATH" |sed -e "s|[^:]\+|$RPM_BUILD_ROOT&|g")"
			if [ -n "$(PATH="$RPATH" /usr/bin/which -- "$r" 2>/dev/null)" ]; then
				continue
			fi

			# Check for pkg contents binary index.
			if [ -n "$RPM_PKG_CONTENTS_INDEX_BIN" ] && [ -s "$RPM_PKG_CONTENTS_INDEX_BIN" ] && [ -r "$RPM_PKG_CONTENTS_INDEX_BIN" ]; then
				for location in /sbin /usr/sbin /bin /usr/bin /usr/X11R6/bin; do
					package="$(awk -v "f=$location/$r" '{if ($1 == f) {print $2; exit}}' "$RPM_PKG_CONTENTS_INDEX_BIN")"
					if [ -n "$package" ]; then
						printf %s\\n "$package"
						continue 2
					fi
				done
			fi

			# Lookup in host system.
			if ! rep="$(/usr/bin/which -- "$r" 2>/dev/null)"; then
				continue
			fi

			# Check package database.
			if package="$(rpmquery --whatprovides --queryformat='%{NAME}\n' -- "$rep" |LC_COLLATE=C sort -u)"; then
				if [ -n "$package" ]; then
					printf %s\\n "$package"
					continue
				fi
			fi
		fi
	done
}
