2007-03-05 01:15:05 +03:00
#!/bin/sh -efu
2002-03-25 23:37:46 +03:00
#
2003-04-22 19:11:52 +04:00
# Copyright (C) 2002-2003 Dmitry V. Levin <ldv@altlinux.org>
2007-03-08 16:49:21 +03:00
# Copyright (C) 2007 Alexey Tourbin <at@altlinux.org>
2002-03-25 23:37:46 +03:00
#
# 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
#
2003-11-09 19:47:45 +03:00
. @RPMCONFIGDIR@/functions
2002-03-25 23:37:46 +03:00
2007-03-08 15:20:02 +03:00
FindByPath()
2002-03-25 23:37:46 +03:00
{
2007-03-08 15:20:02 +03:00
# Dependence name starts with `/'.
local f="$1" rep="$2" package; shift 2 || return
2002-03-25 23:37:46 +03:00
2007-03-08 15:20:02 +03:00
# Does it start with buildroot?
if [ -n "${RPM_BUILD_ROOT-}" ] && [ -z "${rep##$RPM_BUILD_ROOT*}" ]; then
Info "$f: invalid dependence: $rep"
return 1
fi
2003-04-22 19:11:52 +04:00
2007-03-08 15:20:02 +03:00
# Does it belong to buildroot?
if [ -n "${RPM_BUILD_ROOT-}" ] && [ -e "$RPM_BUILD_ROOT$rep" ]; then
Verbose "$f: $rep -> \$RPM_BUILD_ROOT$rep (skip)"
return
fi
2003-04-22 19:11:52 +04:00
2007-03-08 15:20:02 +03:00
# Is it an alternative?
if readlink "$rep" |grep -qs '^/etc/alternatives/'; then
Verbose "$f: $rep -> $rep (alternative)"
printf %s\\n "$rep"
return
fi
2003-04-22 19:11:52 +04:00
2007-03-08 16:49:21 +03:00
# Check for pkg contents complete index and pkg binary index.
local idx
for idx in "${RPM_PKG_CONTENTS_INDEX_ALL-}" "${RPM_PKG_CONTENTS_INDEX_BIN-}"; do
[ -n "$idx" ] && [ -s "$idx" ] && [ -r "$idx" ] || continue
package="$(awk -v "f=$rep" '($1 == f) {print $2}' "$idx" |sort -u)"
local n="$(IFS=$'\n'; set -- $package; echo $#)"
if [ "$n" = 1 ]; then
2007-03-08 15:20:02 +03:00
Verbose "$f: $rep -> $package (via content index)"
printf %s\\n "$package"
return
2007-03-08 16:49:21 +03:00
elif [ "$n" -gt 1 ]; then
Info "$f: $rep indexed by:$(echo '' $package)"
Verbose "$f: $rep -> $rep (raw, ambiguous, via content index)"
printf %s\\n "$rep"
2007-03-08 15:20:02 +03:00
return
fi
2007-03-08 16:49:21 +03:00
done
2003-04-22 19:11:52 +04:00
2007-03-08 15:20:02 +03:00
# Check package database.
if package="$(rpmquery --whatprovides --queryformat='%{NAME}\n' -- "$rep")"; then
package="$(printf %s "$package" |LC_COLLATE=C sort -u)"
local n="$(IFS=$'\n'; set -- $package; echo $#)"
if [ "$n" = 1 ]; then
Verbose "$f: $rep -> $package (via rpmdb)"
printf %s\\n "$package"
return
elif [ "$n" -gt 1 ]; then
Info "$f: $rep provided by:$(echo '' $package)"
2007-03-08 16:49:21 +03:00
Verbose "$f: $rep -> $rep (raw, ambiguous, via rpmdb)"
printf %s\\n "$rep"
return
2007-03-08 15:20:02 +03:00
fi
fi
2003-04-22 19:11:52 +04:00
2007-03-08 15:20:02 +03:00
# Not found; output raw dependence.
2007-03-08 16:49:21 +03:00
Verbose "$f: $rep -> $rep (raw, not found)"
2007-03-08 15:20:02 +03:00
printf %s\\n "$rep"
}
2003-04-22 19:11:52 +04:00
2007-03-08 15:20:02 +03:00
FindByName()
{
local f="$1" r="$2" rep package; shift 2 || return
2003-04-22 19:11:52 +04:00
2007-03-08 15:20:02 +03:00
# Check buildroot first.
if [ -n "${RPM_BUILD_ROOT-}" ]; then
local RPATH
RPATH="$(printf %s "$PATH" |sed -e "s|[^:]\+|$RPM_BUILD_ROOT&|g")"
if rep="$(PATH="$RPATH" /usr/bin/which -- "$r" 2>/dev/null)"; then
Verbose "$f: $r -> \$RPM_BUILD_ROOT${rep#$RPM_BUILD_ROOT} (skip)"
return
fi
fi
2003-04-22 19:11:52 +04:00
2007-03-08 15:20:02 +03:00
# 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
2007-03-08 18:26:28 +03:00
local out="$(awk -v r="$r" '{p=$1}
sub("^/(bin|sbin|usr/bin|usr/sbin|usr/X11R6/bin)/","",$1) && $1==r {
# sort paths by length, which gives the above order
print length(p) "\t" p "\t" $2 }' "$RPM_PKG_CONTENTS_INDEX_BIN" |
sort -n |sort -u -k3 |sort -n |cut -f2-)"
local n="$(IFS=$'\n'; set -- $out; echo $#)"
if [ "$n" = 1 ]; then
rep="$(IFS=$'\t\n'; set -- $out; printf %s "$1")"
package="$(IFS=$'\t\n'; set -- $out; printf %s "$2")"
Verbose "$r -> $rep -> $package (via content index)"
printf %s\\n "$package"
return
elif [ "$n" -gt 1 ]; then
Info "$f: $r indexed by:$(printf %s "$out" |sed -e 's/\t/ -> /; s/$/,/; $s/,$//' |xargs echo '')"
rep="$(IFS=$'\t\n'; set -- $out; printf %s "$1")"
package="$(IFS=$'\t\n'; set -- $out; printf %s "$2")"
local rep2="$(IFS=$'\t\n'; set -- $out; printf %s "$3")"
if [ "$rep" = "$rep2" ]; then
Verbose "$f: $r -> $rep -> $rep (raw, ambiguous, via content index)"
printf %s\\n "$rep"
return
else
Verbose "$f: $r -> $rep -> $package (via content index)"
2007-03-08 15:20:02 +03:00
printf %s\\n "$package"
return
2002-03-25 23:37:46 +03:00
fi
2007-03-08 18:26:28 +03:00
fi
2007-03-08 15:20:02 +03:00
fi
2007-03-08 16:18:41 +03:00
# Lookup in the host system.
if rep="$(/usr/bin/which --all --skip-tilde -- "$r" 2>/dev/null)"; then
local n="$(IFS=$'\n'; set -- $rep; echo $#)"
if [ "$n" -gt 1 ]; then
n="$(IFS=$'\n'; for f in $rep; do readlink -vm "$f"; done |sort -u |wc -l)"
[ "$n" = 1 ] || Info "$f: which $r:$(echo '' $rep)"
rep="$(IFS=$'\n'; set -- $rep; printf %s "$1")"
fi
if [ -n "$rep" ]; then
Verbose "$f: $r -> $rep -> ... (via which)"
FindByPath "$f" "$rep"
2007-03-08 15:20:02 +03:00
return
fi
fi
# Not found.
Verbose "$f: $r not found (skip)"
}
2007-03-06 21:08:06 +03:00
2007-03-08 15:20:02 +03:00
FindPackage()
{
local f="$1" r; shift || return
for r; do
[ -n "$r" ] || continue
if [ -z "${r##/*}" ]; then
FindByPath "$f" "$r"
else
FindByName "$f" "$r"
2002-03-25 23:37:46 +03:00
fi
done
}