]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- remove docdir field from preamble,
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.14. Adapter adapts .spec files for PLD.
4 # Copyright (C) 1999 Micha³ Kuratczyk <kura@pld.org.pl>
5
6 BEGIN {
7         preamble = 1
8         boc = 2                 # Beggining of %changelog
9         bod = 0                 # Beggining of %description
10         tw = 77                 # Descriptions width
11         groups_file = ENVIRON["HOME"] "/rpm/groups" # File with rpm groups
12
13         # Temporary file for changelog section
14         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
15
16         # Is 'date' macro already defined?
17         if (is_there_line("%define date"))
18                 date = 1
19         
20         "rpm --eval %_prefix"   | getline prefix
21         "rpm --eval %_bindir"   | getline bindir
22         "rpm --eval %_sbindir"  | getline sbindir
23         "rpm --eval %_libdir"   | getline libdir
24         "rpm --eval %_sysconfdir" | getline sysconfdir
25         "rpm --eval %_datadir"  | getline datadir
26         "rpm --eval %_includedir" | getline includedir
27         "rpm --eval %_mandir"   | getline mandir
28         "rpm --eval %_infodir"  | getline infodir
29 }
30
31 # There should be a comment with CVS keywords on the first line of file.
32 FNR == 1 {
33         if (!/# \$Revision:/)           # If this line is already OK?
34                 print "# $" "Revision:$, " "$" "Date:$"         # No
35         else {
36                 print $0                                                # Yes
37                 next                            # It is enough for first line
38         }
39 }
40
41 # If the latest line matched /%files/
42 defattr == 1 {
43         if ($0 !~ /defattr/)    # If no %defattr
44                 print "%defattr(644,root,root,755)"     # Add it
45         else
46                 $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
47         defattr = 0
48 }
49
50 /^%define/ {
51         if ($2 == "_applnkdir")
52                 next                                    
53 }
54
55 # descriptions:
56 /^%description/, (/^%[a-z]+/ && !/^%description/) {
57         preamble = 0
58
59         if (/^%description/)
60                 bod++
61         
62         # Define _prefix and _mandir if it is X11 application
63         if (/^%description$/ && x11 == 1) {
64                 print "%define\t\t_prefix\t\t/usr/X11R6"
65                 print "%define\t\t_mandir\t\t%{_prefix}/man\n"
66                 prefix = "/usr/X11R6"
67                 x11 = 2
68         }
69
70         # Collect whole text of description
71         if (description == 1 && !/^%[a-z]+/ && !/^%description/) {
72                 description_text = description_text $0 " "
73                 next
74         }
75  
76         # Formt description to the length of tw (default == 77)
77         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
78                 n = split(description_text, dt, / /)
79                 for (i = 1; i <= n; i++) {
80                         if (length(line) + length(dt[i]) + 1 < tw)
81                                 line = line dt[i] " "
82                         else {
83                                 sub(/[ ]+$/, "", line)
84                                 print line
85                                 line = ""
86                                 i--
87                         }
88                 }
89
90                 sub(/[ ]+$/, "", line)
91                 print line "\n"
92                 line = ""
93                 delete dt
94                 description_text = ""
95                 if (bod == 2) {
96                         bod = 1
97                         description = 1
98                 } else {
99                         bod = 0
100                         description = 0
101                 }
102         } else
103                 description = 1
104 }
105
106 # %prep section:
107 /^%prep/, (/^%[a-z]+$/ && !/^%prep/) {
108         preamble = 0
109         
110         # Add '-q' to %setup
111         if (/^%setup/ && !/-q/)
112                 sub(/^%setup/, "%setup -q")
113 }
114
115 # %build section:
116 /^%build/, (/^%[a-z]+$/ && !/^%build/) {
117         preamble = 0
118
119         use_macros()
120 }
121
122 # %install section:
123 /^%install/, (/^[a-z]+$/ && !/^%install/) {
124         preamble = 0
125         
126         use_macros()
127
128         # 'install -d' instead 'mkdir -p'
129         if (/mkdir -p/)
130                 sub(/mkdir -p/, "install -d")
131                 
132         # No '-u root' or '-g root' for 'install'
133         if (/^install/ && /-[ug][ \t]*root/)
134                 gsub(/-[ug][ \t]*root /, "")
135         
136         if (/^install/ && /-m[ \t]*644/)
137                 gsub(/-m[ \t]*644 /, "")
138         
139         # No lines contain 'chown' or 'chgrp', which changes
140         # owner/group to 'root'
141         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
142                 next
143         
144         # No lines contain 'chmod' if it sets the modes to '644'
145         if ($1 ~ /chmod/ && $2 ~ /644/)
146                 next
147         
148         # 'gzip -9nf' for compressing
149         if ($1 ~ /gzip|bzip2/) {
150                 if ($2 ~ /^-/)
151                         sub(/-[A-Za-z0-9]+ /, "", $0)
152                 sub($1, "gzip -9nf")
153         }
154 }
155
156 # %files section:
157 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
158         preamble = 0
159         
160         if ($0 ~ /^%files/)
161                 defattr = 1
162         
163         use_macros()
164 }
165
166 # %changelog section:
167 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
168         preamble = 0
169         
170         # There should be some CVS keywords on the first line of %changelog.
171         if (boc == 1) {
172                 if (!/PLD Team/) {
173                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
174                         printf "All persons listed below can be reached at " > changelog_file
175                         print "<cvs_login>@pld.org.pl\n" > changelog_file
176                         print "$" "Log:$" > changelog_file
177                 }
178                 boc = 0
179         }
180         
181         # Define date macro.
182         if (boc == 2) {
183                 if (date == 0) {
184                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
185                         print " date +\"%a %b %d %Y\"`)" > changelog_file
186                 }
187         boc--
188         }
189
190         if (!/^%[a-z]+$/ || /changelog/)
191                 print > changelog_file
192         else
193                 print
194         next
195 }
196
197 # preambles:
198 preamble == 1 {
199         # There should not be a space after the name of field
200         # and before the colon.
201         sub(/[ \t]*:/, ":")
202         
203         field = tolower($1)
204
205         if (field ~ /packager:|distribution:|docdir:|prefix:/)
206                 next
207         
208         if (field ~ /buildroot:/)
209                 $2 = "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
210
211         if (field ~ /group:/) {
212                 format_preamble()
213                 print $0
214                 
215                 translate_group($2)
216                 close(groups_file)
217                 
218                 if ($2 ~ /^X11/ && x11 == 0)    # Is it X11 application?
219                        x11 = 1
220
221                 next    # Line is already formatted and printed
222         }
223                 
224         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
225         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
226                 $1 = "License:"
227         
228         if (field ~ /name:/)
229                 name = $2
230
231         if (field ~ /version:/)
232                 version = $2
233
234         # Use %{name} and %{version} in the filenames in "Source:"
235         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
236                 n = split($2, url, /\//)
237                 filename = url[n]
238                 sub(name, "%{name}", url[n])
239                 sub(version, "%{version}", url[n])
240                 sub(filename, url[n], $2)
241         }
242
243         if (field ~ /source:/)
244                 $1 = "Source0:" 
245
246         if (field ~ /patch:/)
247                 $1 = "Patch0:"
248         
249         format_preamble()
250         
251         if ($1 ~ /%define/) {
252                 # Do not add %define of _prefix if it already is.
253                 if ($2 ~ /_prefix/) {
254                         prefix = $3
255                         x11 = 2
256                 }
257                 if ($2 ~ /_bindir/ && !/_sbindir/)
258                         bindir = $3
259                 if ($2 ~ /_sbindir/)
260                         sbindir = $3
261                 if ($2 ~ /_libdir/)
262                         libdir = $3
263                 if ($2 ~ /_sysconfdir/)
264                         sysconfdir = $3
265                 if ($2 ~ /_datadir/)
266                         datadir = $3
267                 if ($2 ~ /_includedir/)
268                         includedir = $3
269                 if ($2 ~ /_mandir/)
270                         mandir = $3
271                 if ($2 ~ /_infodir/)
272                         infodir = $3
273         }
274 }
275
276 {
277         preamble = 1
278         
279         print
280 }
281
282 END {
283         if (changelog_file) {
284                 close(changelog_file)
285                 while ((getline < changelog_file) > 0)
286                         print
287                 system("rm -f " changelog_file)
288         }
289
290         if (boc == 1) {
291                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
292                 printf "All below listed persons can be reached on "
293                 print "<cvs_login>@pld.org.pl\n"
294                 print "$" "Log:$"
295         }
296 }
297
298 # This function uses grep to determine if there is line (in the current file)
299 # which matches regexp.
300 function is_there_line(line, l)
301 {
302         command = "grep \"" line "\" " ARGV[1]
303         command | getline l
304         close(command)
305
306         if (l != "")
307                 return 1
308         else
309                 return 0
310 }
311
312 # This function prints translated names of groups.
313 function translate_group(group)
314 {
315         for(;;) {
316                 result = getline line < groups_file
317                 
318                 if (result == -1) {
319                         print "######\t\t" groups_file ": no such file"
320                         return
321                 }
322
323                 if (result == 0) {
324                         print "######\t\t" "Unknown group!"
325                         return
326                 }
327                 
328                 if (line == group) {
329                         found = 1
330                         continue
331                 }
332
333                 if (found == 1)
334                         if (line ~ /\[[a-z][a-z]\]:/) {
335                                 split(line, g, /\[|\]|\:/)
336                                 if (!is_there_line("^Group(" g[2] "):"))
337                                                 printf("Group(%s):%s\n", g[2], g[4])
338                         } else {
339                                 found = 0
340                                 return
341                         }
342         }
343 }
344
345 # There should be one or two tabs after the colon.
346 function format_preamble()
347 {
348         sub(/:[ \t]*/, ":")
349         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
350                 if (RLENGTH < 8)
351                         sub(/:/, ":\t\t")
352                 else
353                         sub(/:/, ":\t")
354         }
355 }
356
357 # Replace directly specified directories with macros
358 function use_macros()
359 {
360         gsub(bindir, "%{_bindir}")
361         gsub("%{_prefix}/bin", "%{_bindir}")
362         gsub("%{prefix}/bin", "%{_bindir}")
363
364         gsub(sbindir, "%{_sbindir}")
365         gsub("%{prefix}/sbin", "%{_sbindir}")
366         gsub("%{_prefix}/sbib", "%{_sbindir}")
367
368         gsub(libdir, "%{_libdir}")
369         gsub("%{prefix}/lib", "%{_libdir}")
370         gsub("%{_prefix}/lib", "%{_libdir}")
371
372         gsub(sysconfdir, "%{_sysconfdir}")
373
374         gsub(datadir, "%{_datadir}")
375         gsub("%{prefix}/share", "%{_datadir}")
376         gsub("%{_prefix}/share", "%{_datadir}")
377
378         gsub(includedir, "%{_includedir}")
379         gsub("%{prefix}/include", "%{_includedir}")
380         gsub("%{_prefix}/include", "%{_includedir}")
381
382         gsub(mandir, "%{_mandir}")
383         gsub("%{prefix}/man", "%{_mandir}")
384         gsub("%{_prefix}/man", "%{_mandir}")
385
386         gsub(infodir, "%{_infodir}")
387         gsub("%{prefix}/info", "%{_infodir}")
388         gsub("%{_prefix}/info", "%{_infodir}")
389
390         if (prefix != "/") {
391                 gsub(prefix, "%{_prefix}")
392                 gsub("%{prefix}", "%{_prefix}")
393         }
394
395         gsub("%{PACKAGE_VERSION}", "%{version}")
396         gsub("%{PACKAGE_NAME}", "%{name}")
397 }
398
This page took 0.067632 seconds and 4 git commands to generate.