+preamble=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
+# take just main package preamble, preamble of tests (and other) subpackage(s) just confuses things.
+sed -ne '/^Name:/,/^BuildRoot/p' $spec > $preamble
+
+# create backup
+bak=$(LC_ALL=C cp -fbv $spec $spec | awk '{print $NF}' | tr -d "['\`]" )
+
+# ensure rpm-build-macros is present
+if ! grep -q "^BuildRequires:.*rpmbuild(macros)" $preamble; then
+ sed -i -e "/^BuildRequires:.*rpm-php-pearprov/aBuildRequires:\trpmbuild(macros) >= 1.300" $spec
+fi
+# parse requires
+requires=$(grep '^BuildRequires:' $template || :)
+if [ -n "$requires" ]; then
+ echo "$requires" | while read tag dep; do
+ dep=$(add_epoch $dep)
+ if ! grep -q "^BuildRequires:.*$dep" $preamble; then
+ sed -i -e "/^BuildRoot/iBuildRequires:\t$dep" $spec
+ fi
+ done
+fi
+
+requires=$(grep '^Requires:' $template || :)
+if [ -n "$requires" ]; then
+ echo "$requires" | while read tag dep; do
+ dep=$(add_epoch $dep)
+ if ! grep -q "^Requires:.*$dep" $preamble; then
+ dep=$(echo "$dep" | sed -e 's,php-pear-PEAR\b,php-pear-PEAR-core,')
+ sed -i -e "/^BuildRoot/iRequires:\t$dep" $spec
+ fi
+ done
+fi
+
+# parse conflicts
+conflicts=$(grep '^Conflicts:' $template || :)
+if [ -n "$conflicts" ]; then
+ echo "$conflicts" | while read tag dep; do
+ dep=$(add_epoch $dep)
+ if ! grep -q "^Conflicts:.*$req" $preamble; then
+ sed -i -e "/^BuildRoot/iConflicts:\t$dep" $spec
+ fi
+ done
+fi
+
+# parse optional deps
+optional=$(grep '^Suggests:' $template || :)
+if [ -n "$optional" ]; then
+ echo "$optional" | while read tag dep; do
+ dep=$(add_epoch $dep)
+ if ! grep -q "^Suggests:.*$dep" $preamble; then
+ sed -i -e "/^BuildRoot/iSuggests:\t$dep" $spec
+ fi
+
+ for req in $dep; do
+ case "$req" in
+ php-pear-*)
+ # convert pear package name to file pattern
+ req=$(echo "$req" | sed -e 's,^php-pear-,pear(,;y,_,/,;s,$,.*),')
+ ;;
+ *)
+ # process only php-pear-* packages
+ continue
+ ;;
+ esac
+
+ m=$(grep "^%define.*_noautoreq" $spec || :)
+ if [ -z "$m" ]; then
+ sed -i -e "/^BuildRoot:/{
+ a
+ a# exclude optional dependencies
+ a%define\ \ _noautoreq\ $req
+ }
+ " $spec
+ else
+ m=$(echo "$m" | grep -o "$req" || :)
+ if [ -z "$m" ]; then
+ sed -i -e "/^%define.*_noautoreq/s,$, $req," $spec
+ fi
+ fi
+ done
+ done
+fi