2003-11-09 17:26:05 +03:00
#!/bin/sh -e
#
# Copyright (C) 2003 Dmitry V. Levin <ldv@altlinux.org>
2007-03-06 17:33:08 +03:00
# Copyright (C) 2007 Alexey Tourbin <at@altlinux.org>
2003-11-09 17:26:05 +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 17:27:11 +03:00
unset CDPATH ||:
2013-01-11 02:38:52 +04:00
export LC_ALL=C
2003-11-09 17:27:11 +03:00
PROG="${0##*/}"
2007-03-04 21:47:34 +03:00
case "${RPM_SCRIPTS_DEBUG:-0}" in
0) unset RPM_SCRIPTS_DEBUG ||: ;;
1|2) ;;
*) RPM_SCRIPTS_DEBUG=3; set -x ;;
esac
2008-01-22 19:51:11 +03:00
Fatal()
2003-11-09 17:27:11 +03:00
{
2008-01-22 19:51:11 +03:00
echo "${0##*/}: ERROR: $*" >&2
exit 1
2003-11-09 17:27:11 +03:00
}
2007-12-03 11:33:32 +03:00
Warning()
{
echo "${0##*/}: WARNING: $*" >&2
}
2008-01-22 19:51:11 +03:00
Info()
2003-11-09 17:27:11 +03:00
{
2008-01-22 19:51:11 +03:00
echo "${0##*/}: $*" >&2
2003-11-09 17:27:11 +03:00
}
2003-11-09 19:09:42 +03:00
2007-03-04 21:47:34 +03:00
Verbose()
{
[ "${RPM_SCRIPTS_DEBUG:-0}" -ge 1 ] || return 0
2007-09-29 18:22:15 +04:00
Info "$@"
2007-03-04 21:47:34 +03:00
}
Debug()
{
[ "${RPM_SCRIPTS_DEBUG:-0}" -ge 2 ] || return 0
2007-09-29 18:22:15 +04:00
Info "$@"
2007-03-04 21:47:34 +03:00
}
2003-11-09 19:09:42 +03:00
ValidateBuildRoot()
{
2008-02-19 02:38:42 +03:00
[ -n "${RPM_BUILD_ROOT-}" ] ||
Fatal 'RPM_BUILD_ROOT not set'
2003-11-09 19:09:42 +03:00
[ -n "$(printf %s "$RPM_BUILD_ROOT" |tr -d ' /.')" ] ||
2008-02-19 02:38:42 +03:00
Fatal "bogus RPM_BUILD_ROOT=$RPM_BUILD_ROOT"
local real_buildroot
# There could be simply no %install section...
# I pretend that non-existent buildroot is just canonical enough.
real_buildroot=$(readlink -vm -- "$RPM_BUILD_ROOT")
[ "$RPM_BUILD_ROOT" = "$real_buildroot" ] ||
Fatal "non-canonical RPM_BUILD_ROOT=$RPM_BUILD_ROOT real_buildroot=$real_buildroot"
2003-11-09 19:09:42 +03:00
}
2007-03-06 17:31:31 +03:00
[ -z "${RPM_BUILD_ROOT-}" ] || ValidateBuildRoot
readonly RPM_BUILD_ROOT
2007-03-06 17:33:08 +03:00
SetupMethods()
{
local suffix="$1" inm="$2"; shift 2
# do not stat(2) here so as to bypass buildreq
2007-11-18 07:59:03 +03:00
local allm=$(ls "$(dirname "$0")"/ |sed -n "/^[^.]/s/[.]$suffix$//p")
2007-03-06 17:33:08 +03:00
[ -n "$allm" ] || Fatal "no $suffix methods available"
allm=$(echo '' $allm '')
2007-09-29 18:22:15 +04:00
local m outm="$allm"
2007-03-06 17:33:08 +03:00
for m in $(IFS="$IFS,"; echo '' $inm); do
case "$m" in
2007-09-24 03:14:24 +04:00
*[!0-9A-Za-z_-]*)
2007-03-06 17:33:08 +03:00
Fatal "invalid $suffix method name $m"
;;
yes|auto|all)
outm=$allm
;;
no|none|off|false|skip)
outm=
;;
no*)
m=${m#no}
outm=$(echo "$outm" |sed "s/ $m / /g")
;;
*)
[ -z "${allm##* $m *}" ] || Fatal "$suffix method $m not available"
[ -n "$outm" -a -z "${outm##* $m *}" ] || outm=" $outm $m "
;;
esac
done
2007-09-29 18:22:15 +04:00
echo $outm |tr -s ' ' ','
2007-03-06 17:33:08 +03:00
}
RunMethods()
{
2007-11-18 07:59:03 +03:00
local m_m m_dir m_suffix="$1" m_inm="$2"; shift 2
m_dir=$(dirname "$0")
2007-09-29 18:22:15 +04:00
for m_m in $(IFS="$IFS,"; echo '' $m_inm); do
2007-11-18 07:59:03 +03:00
"$@" "$m_dir/$m_m.$m_suffix"
2007-03-06 17:33:08 +03:00
done
}
2007-03-06 18:03:53 +03:00
2007-11-18 02:38:17 +03:00
# Canonicalize each path component with respect to symbolic links, except
# for the last. E.g. /etc/init.d/functions -> /etc/rc.d/init.d/functions.
2007-08-27 20:41:02 +04:00
CanonPath()
{
2007-11-18 02:38:17 +03:00
local f0="${1:?}" f1 f2; shift
[ -z "${f0##/*}" ] || f0=$PWD/$f0
# First pass: "blind" cleanup-only canonicalization.
f1=$(readlink -vm "/-$$-/$f0")
f1=${f1##/-$$-}
2007-11-23 11:45:45 +03:00
[ -n "$f1" ] || f1=/
2007-11-18 02:38:17 +03:00
[ "$f0" = "$f1" ] || Verbose "CanonPath1: $f0 -> $f1"
# Second pass: adjust parent directory.
# Note that -L test was not reliable before the first pass.
if [ -L "$f1" ]; then
2007-08-27 20:41:02 +04:00
local d
2007-11-18 02:38:17 +03:00
d=$(dirname -- "$f1")
2007-08-27 20:41:02 +04:00
d=$(readlink -vm -- "$d")
2007-11-18 02:38:17 +03:00
f2="${d%/}/${f1##*/}"
2007-08-27 20:41:02 +04:00
else
2007-11-18 02:38:17 +03:00
f2=$(readlink -vm -- "$f1")
2007-08-27 20:41:02 +04:00
fi
2007-11-18 02:38:17 +03:00
[ "$f1" = "$f2" ] || Verbose "CanonPath2: $f1 -> $f2"
2007-08-27 20:41:02 +04:00
echo "$f2"
}
2007-03-06 18:03:53 +03:00
ArgvFileAction()
{
local av_action="$1"; shift
if [ $# -gt 0 ]; then
2007-09-29 18:22:15 +04:00
local av_argv
2007-03-06 18:03:53 +03:00
av_argv=$(getopt -n "$PROG" -o v -l help,verbose -- "$@")
eval set -- "$av_argv"
while :; do
case "$1" in
--help) type Usage >/dev/null 2>&1 && Usage 0 ||
echo "Usage: $PROG [-v|--verbose] [FILE...]"
exit 0 ;;
-v|--verbose)
export RPM_SCRIPTS_DEBUG=$((${RPM_SCRIPTS_DEBUG:-0}+1))
shift ;;
--) shift
break ;;
*) Fatal "unrecognized option: $1"
esac
done
[ "${RPM_SCRIPTS_DEBUG:-0}" -lt 3 ] || set -x
fi
local av_f
if [ $# -gt 0 ]; then
for av_f; do
2007-08-27 20:41:02 +04:00
[ -n "$av_f" -a -z "${av_f##/*}" ] || av_f=$(CanonPath "$av_f")
2007-03-06 18:03:53 +03:00
Debug "processing $av_f"
2010-06-14 03:01:53 +04:00
"$av_action" "$av_f" </dev/null
2007-03-06 18:03:53 +03:00
done
else
[ -t 0 ] && Info "reading file list from standard input"
while IFS= read -r av_f; do
2007-08-27 20:41:02 +04:00
[ -n "$av_f" -a -z "${av_f##/*}" ] || av_f=$(CanonPath "$av_f")
2007-03-06 18:03:53 +03:00
Debug "processing $av_f"
2010-06-14 03:01:53 +04:00
"$av_action" "$av_f" </dev/null
2007-03-06 18:03:53 +03:00
done
fi
}
2008-01-23 01:22:45 +03:00
PackagedFiles()
{
if [ -n "${RPM_BUILD_ROOT-}" ]; then
[ -z "${RPM_SUBPACKAGE_NAME-}" ] ||
2008-02-18 18:26:21 +03:00
cat "$RPM_BUILD_ROOT/.files:$RPM_SUBPACKAGE_NAME" 2>/dev/null ||:
2008-01-23 01:22:45 +03:00
else
rpmquery --list -f -- "$@" 2>/dev/null ||:
fi
}
2008-07-08 01:42:14 +04:00
# Quote given arguments for sed basic regular expression.
# Usage example: sed "s/$(quote_sed_regexp "$var_pattern")/$(quote_sed_regexp "$var_replacement")/"
# Taken from libshell.
quote_sed_regexp()
{
local out="$*"
if [ -z "${out##*[\[\].*&^\$\\\\/]*}" ]; then
out="$(printf %s "$out" |sed -e 's/[].*&^$[\/]/\\&/g')" ||
return 1
fi
printf %s "$out"
}