]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pearize.sh
- merge
[packages/rpm-build-tools.git] / pearize.sh
1 #!/bin/sh
2 # updates php-pear .spec with Requires/Conflicts lines.
3 # the items are added randomly to the preamble, but once added their order is left intact.
4 # it is still better than nothing. if somebody wishes to add sorting in this
5 # script. i'd be just glad :)
6 #
7 # needs 'pear' for 'pear makerpm' command, ./builder for fetching sources.
8 # You should have all PEAR packages installed to get best results (needed for epoch autodetection)
9 #
10 # todo: adjust similiarily noautoreqdeps
11 # bugs: the beta portions in version deps could be wrong (php-4.3.0b1 and alike)
12 # see php-pear-DBA_Relational.spec
13 # Something strange: Requires:  php-common < 4:3:5.1
14 #
15 # NOTE: old version of this script which was used to convert to new package format is in CVS branch MIGRATE.
16 #
17 # Send blames and beer\b\b\b\bideas to glen@pld-linux.org
18
19 set -e
20 spec="$1"
21 if [ -z "$spec" ]; then
22         echo >&2 "Usage: $0 SPECFILE"
23         exit 0
24 fi
25 if [ ! -f "$spec" ]; then
26         echo >&2 "$spec doesn't exist?"
27         exit 1
28 fi
29
30 if [[ "$(rpm -q php-pear-PEAR_Command_Packaging)" == *is?not* ]]; then
31         echo >&2 "Please install php-pear-PEAR_Command_Packaging"
32         exit 1
33 fi
34 echo "Processing $spec"
35
36 getsource() {
37         local spec="$1"
38         local NR="$2"
39         rpmbuild --nodigest --nosignature -bp --define 'prep %dump' $spec 2>&1 | awk  "/SOURCE$NR\t/ {print \$3}"
40 }
41
42 tarball=$(getsource $spec 0)
43 if [ -z "$tarball" ]; then
44         echo >&2 "Spec is missing Source0!"
45         exit 1
46 fi
47
48 if [ ! -f $tarball ]; then
49         ./builder -g "$spec"
50 fi
51
52 stmp=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
53 cat > $stmp <<'EOF'
54 @extra_headers@
55 Optional: @optional@
56 @optional-pkg@
57 @optional-ext@
58 License: @release_license@
59 State: @release_state@
60 EOF
61 pear make-rpm-spec --spec-template=$stmp --output=pearize.spec $tarball
62 template=pearize.spec
63 rm -f $stmp
64
65 mv $template .$template~
66 template=.$template~
67
68 # take as argument dependency in form NAME EQUALITY VERSION
69 # adds rpm epoch to VERSION if the package is installed and has epoch bigger than zero.
70 add_epoch() {
71         local dep="$@"
72         local pkg="$1"
73         local ver="$3"
74
75         # already have epoch
76         if [[ "$ver" = *:* ]]; then
77                 echo "$dep"
78                 return
79         fi
80
81         query=$(rpm -q --qf '%{epoch}\n' $pkg || :)
82         epoch=$(echo "$query" | grep -v 'installed' || :)
83         if [ "$epoch" ] && [ "$epoch" -gt 0 ]; then
84                 echo "$dep" | sed -e "s, [<>=]\+ ,&$epoch:,"
85         else
86                 echo "$dep"
87         fi
88 }
89
90 preamble=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
91 # take just main package preamble, preamble of tests (and other) subpackage(s) just confuses things.
92 sed -ne '/^Name:/,/^BuildRoot/p' $spec > $preamble
93
94 # create backup
95 bak=$(cp -fbv $spec $spec | awk '{print $NF}' | tr -d "['\`]" )
96
97 # parse requires
98 requires=$(grep '^Requires:' $template || :)
99 if [ -n "$requires" ]; then
100         echo "$requires" | while read tag dep; do
101                 dep=$(add_epoch $dep)
102                 if ! grep -q "^Requires:.*$dep" $preamble; then
103                         sed -i -e "/^BuildRoot/iRequires:\t$dep" $spec
104                 fi
105         done
106 fi
107
108 # parse conflicts
109 conflicts=$(grep '^Conflicts:' $template || :)
110 if [ -n "$conflicts" ]; then
111         echo "$conflicts" | while read tag dep; do
112                 dep=$(add_epoch $dep)
113                 if ! grep -q "^Conflicts:.*$req" $preamble; then
114                         sed -i -e "/^BuildRoot/iConflicts:\t$dep" $spec
115                 fi
116         done
117 fi
118
119 # parse optional deps
120 optional=$(grep '^Optional:' $template || :)
121 if [ -n "$optional" ]; then
122         echo "$optional" | while read tag dep; do
123                 for req in $dep; do
124                         m=$(grep "^%define.*_noautoreq" $spec || :)
125                         if [ -z "$m" ]; then
126                                 sed -i -e "/^BuildRoot:/{
127                                         a
128                                         a# exclude optional dependencies
129                                         a%define\       \       _noautoreq\     $req
130                                 }
131                                 " $spec
132                         else
133                                 m=$(echo "$m" | grep -o "$req" || :)
134                                 if [ -z "$m" ]; then
135                                         sed -i -e "/^%define.*_noautoreq/s,$, $req," $spec
136                                 fi
137                         fi
138                 done
139         done
140 fi
141 has_opt=$(egrep -c '^Optional-(pkg|ext):' $template || :)
142 if [ "$has_opt" -gt 0 ]; then
143         if ! grep -q '%{_docdir}/.*/optional-packages.txt' $spec; then
144                 sed -i -e '
145                 /^%files$/{
146                         i%post
147                         iif [ -f %{_docdir}/%{name}-%{version}/optional-packages.txt ]; then
148                         i\      cat %{_docdir}/%{name}-%{version}/optional-packages.txt
149                         ifi
150                         i
151                 }
152                 ' $spec
153         fi
154         if ! grep -q '%doc.*optional-packages.txt' $spec; then
155                 sed -i -e '
156                 /^%doc install.log/{
157                 s/$/ optional-packages.txt/
158                 }
159                 ' $spec
160         fi
161 fi
162
163 # parse state
164 state=$(awk '/^State:/{print $2}' $template)
165 sed -i -e "/^%define.*_status/{
166         /%define.*_status.*$state/!s/.*/%define\t\t_status\t\t$state/
167 }" $spec
168
169 rm -f $preamble
170
171 diff=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
172 if ! diff -u $bak $spec > $diff; then
173         vim -o $spec $diff
174         rm -f $diff
175 else
176         echo "$spec: No diffs"
177 fi
178 #exit 1
This page took 0.060597 seconds and 4 git commands to generate.