]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pearize.sh
- need PEAR_Command_Packaging
[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 echo "Processing $spec"
30
31 if [[ "$(rpm -q php-pear-PEAR_Command_Packaging)" = *is*not* ]]; then
32         echo >&2 "Please install php-pear-PEAR_Command_Packaging package"
33 fi
34
35 getsource() {
36         local spec="$1"
37         local NR="$2"
38         rpmbuild --nodigest --nosignature -bp --define 'prep %dump' $spec 2>&1 | awk  "/SOURCE$NR\t/ {print \$3}"
39 }
40
41 tarball=$(getsource $spec 0)
42 if [ -z "$tarball" ]; then
43         echo >&2 "Spec is missing Source0!"
44         exit 1
45 fi
46
47 if [ ! -f $tarball ]; then
48         ./builder -g "$spec"
49 fi
50
51 stmp=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
52 cat > $stmp <<'EOF'
53 @extra_headers@
54 Optional: @optional@
55 @optional-pkg@
56 @optional-ext@
57 License: @release_license@
58 State: @release_state@
59 EOF
60 pear make-rpm-spec --spec-template=$stmp --output=pearize.spec $tarball
61 template=pearize.spec
62 rm -f $stmp
63
64 mv $template .$template~
65 template=.$template~
66
67 # take as argument dependency in form NAME EQUALITY VERSION
68 # adds rpm epoch to VERSION if the package is installed and has epoch bigger than zero.
69 add_epoch() {
70         local dep="$@"
71         local pkg="$1"
72         local ver="$3"
73
74         # already have epoch
75         if [[ "$ver" = *:* ]]; then
76                 echo "$dep"
77                 return
78         fi
79
80         query=$(rpm -q --qf '%{epoch}\n' $pkg || :)
81         epoch=$(echo "$query" | grep -v 'installed' || :)
82         if [ "$epoch" ] && [ "$epoch" -gt 0 ]; then
83                 echo "$dep" | sed -e "s, [<>=]\+ ,&$epoch:,"
84         else
85                 echo "$dep"
86         fi
87 }
88
89 preamble=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
90 # take just main package preamble, preamble of tests (and other) subpackage(s) just confuses things.
91 sed -ne '/^Name:/,/^BuildRoot/p' $spec > $preamble
92
93 # create backup
94 bak=$(cp -fbv $spec $spec | awk '{print $NF}' | tr -d "['\`]" )
95
96 # parse requires
97 requires=$(grep '^Requires:' $template || :)
98 if [ -n "$requires" ]; then
99         echo "$requires" | while read tag dep; do
100                 dep=$(add_epoch $dep)
101                 if ! grep -q "^Requires:.*$dep" $preamble; then
102                         sed -i -e "/^BuildRoot/iRequires:\t$dep" $spec
103                 fi
104         done
105 fi
106
107 # parse conflicts
108 conflicts=$(grep '^Conflicts:' $template || :)
109 if [ -n "$conflicts" ]; then
110         echo "$conflicts" | while read tag dep; do
111                 dep=$(add_epoch $dep)
112                 if ! grep -q "^Conflicts:.*$req" $preamble; then
113                         sed -i -e "/^BuildRoot/iConflicts:\t$dep" $spec
114                 fi
115         done
116 fi
117
118 # parse optional deps
119 optional=$(grep '^Optional:' $template || :)
120 if [ -n "$optional" ]; then
121         echo "$optional" | while read tag dep; do
122                 for req in $dep; do
123                         m=$(grep "^%define.*_noautoreq" $spec || :)
124                         if [ -z "$m" ]; then
125                                 sed -i -e "/^BuildRoot:/{
126                                         a
127                                         a# exclude optional dependencies
128                                         a%define\       \       _noautoreq\     $req
129                                 }
130                                 " $spec
131                         else
132                                 m=$(echo "$m" | grep -o "$req" || :)
133                                 if [ -z "$m" ]; then
134                                         sed -i -e "/^%define.*_noautoreq/s,$, $req," $spec
135                                 fi
136                         fi
137                 done
138         done
139 fi
140 has_opt=$(egrep -c '^Optional-(pkg|ext):' $template || :)
141 if [ "$has_opt" -gt 0 ]; then
142         if ! grep -q '%{_docdir}/.*/optional-packages.txt' $spec; then
143                 sed -i -e '
144                 /^%files$/{
145                         i%post
146                         iif [ -f %{_docdir}/%{name}-%{version}/optional-packages.txt ]; then
147                         i\      cat %{_docdir}/%{name}-%{version}/optional-packages.txt
148                         ifi
149                         i
150                 }
151                 ' $spec
152         fi
153         if ! grep -q '%doc.*optional-packages.txt' $spec; then
154                 sed -i -e '
155                 /^%doc install.log/{
156                 s/$/ optional-packages.txt/
157                 }
158                 ' $spec
159         fi
160 fi
161
162 # parse state
163 state=$(awk '/^State:/{print $2}' $template)
164 sed -i -e "/^%define.*_status/{
165         /%define.*_status.*$state/!s/.*/%define\t\t_status\t\t$state/
166 }" $spec
167
168 rm -f $preamble
169
170 diff=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
171 if ! diff -u $bak $spec > $diff; then
172         vim -o $spec $diff
173         rm -f $diff
174 else
175         echo "$spec: No diffs"
176 fi
177 #exit 1
This page took 0.052399 seconds and 4 git commands to generate.