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