]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- Honours redefinitions of "_prefix". E.g. will not replace "/usr/bin" with
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.21. Adapter adapts .spec files for PLD.
4 #
5 # Copyright (C) 1999, 2000 PLD-Team <pld-list@pld.org.pl>
6 # Authors:
7 #       Micha³ Kuratczyk <kura@pld.org.pl>
8 #       Sebastian Zagrodzki <s.zagrodzki@mimuw.edu.pl>
9 #       Tomasz K³oczko <kloczek@rudy.mif.pg.gda.pl>
10 #       Artur Frysiak <wiget@pld.org.pl>
11 #       Michal Kochanowicz <mkochano@ee.pw.edu.pl>
12 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
13
14 BEGIN {
15         preamble = 1            # Is it part of preamble? Default - yes
16         boc = 2                 # Beggining of %changelog
17         bod = 0                 # Beggining of %description
18         tw = 70                 # Descriptions width
19
20         # File with rpm groups
21         "rpm --eval %_topdir" | getline groups_file
22         groups_file = groups_file "/groups"
23
24         # Temporary file for changelog section
25         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
26
27         # Is 'date' macro already defined?
28         if (is_there_line("%define date"))
29                 date = 1
30         
31         # Load rpm macros
32         "rpm --eval %_prefix"   | getline prefix
33         "rpm --eval %_bindir"   | getline bindir
34         "rpm --eval %_sbindir"  | getline sbindir
35         "rpm --eval %_libdir"   | getline libdir
36         "rpm --eval %_sysconfdir" | getline sysconfdir
37         "rpm --eval %_datadir"  | getline datadir
38         "rpm --eval %_includedir" | getline includedir
39         "rpm --eval %_mandir"   | getline mandir
40         "rpm --eval %_infodir"  | getline infodir
41 }
42
43 # There should be a comment with CVS keywords on the first line of file.
44 FNR == 1 {
45         if (!/# \$Revision:/)   # If this line is already OK?
46                 print "# $" "Revision:$, " "$" "Date:$" # No
47         else {
48                 print $0                                # Yes
49                 next            # It is enough for first line
50         }
51 }
52
53 # If the latest line matched /%files/
54 defattr == 1 {
55         if ($0 !~ /defattr/)    # If no %defattr
56                 print "%defattr(644,root,root,755)"     # Add it
57         else
58                 $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
59         defattr = 0
60 }
61
62 # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
63 /^%define/ {
64         if ($2 == "_applnkdir")
65                 next                                    
66 }
67
68 # descriptions:
69 /^%description/, (/^%[a-z]+/ && !/^%description/) {
70         preamble = 0
71
72         if (/^%description/) {
73                 bod++
74                 format_line = ""
75                 format_indent = -1
76         }
77
78         # Define _prefix and _mandir if it is X11 application
79         if (/^%description$/ && x11 == 1) {
80                 print "%define\t\t_prefix\t\t/usr/X11R6"
81                 print "%define\t\t_mandir\t\t%{_prefix}/man\n"
82                 prefix = "/usr/X11R6"
83                 x11 = 2
84         }
85         
86         # Format description
87         if (description == 1 && !/^%[a-z]+/ && !/^%description/) {
88                 if (/^[ \t]*$/) {
89                         format_flush(format_line, format_indent)
90                         print ""
91                         format_line = ""
92                         format_indent = -1
93                 } else if (/^[ \t]*[-\*][ \t]*/) {
94                         format_flush(format_line, format_indent)
95                         match($0, /^[ \t]*/)    
96                         format_indent = RLENGTH
97                         match($0, /^[ \t]*[-\*][ \t]/)
98                         format_line = substr($0, RLENGTH)
99                 } else 
100                         format_line = format_line " " $0
101                 next
102         }
103  
104         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
105                 format_flush(format_line, format_indent)
106                 if (bod == 2) {
107                         bod = 1
108                         description = 1
109                 } else {
110                         bod = 0
111                         description = 0
112                 }
113         } else
114                 description = 1
115 }
116
117 # %prep section:
118 /^%prep/, (/^%[a-z]+$/ && !/^%prep/) {
119         preamble = 0
120         
121         # Add '-q' to %setup
122         if (/^%setup/ && !/-q/)
123                 sub(/^%setup/, "%setup -q")
124 }
125
126 # %build section:
127 /^%build/, (/^%[a-z]+$/ && !/^%build/) {
128         preamble = 0
129
130         use_macros()
131 }
132
133 # %install section:
134 /^%install/, (/^%[a-z]+$/ && !/^%install/) {
135         
136         preamble = 0
137         
138         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+\${?RPM_BUILD_ROOT}?/ && did_clean==0) {
139                 did_clean=1
140                 print "rm -rf $RPM_BUILD_ROOT"
141                 next
142         }
143
144         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_clean==0) {
145                 print "rm -rf $RPM_BUILD_ROOT"
146                 did_clean=1
147         }
148         
149         use_macros()
150         
151         # 'install -d' instead 'mkdir -p'
152         if (/mkdir -p/)
153                 sub(/mkdir -p/, "install -d")
154                 
155         # No '-u root' or '-g root' for 'install'
156         if (/^install/ && /-[ug][ \t]*root/)
157                 gsub(/-[ug][ \t]*root /, "")
158         
159         if (/^install/ && /-m[ \t]*644/)
160                 gsub(/-m[ \t]*644 /, "")
161         
162         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
163         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
164                 next
165         
166         # No lines contain 'chmod' if it sets the modes to '644'
167         if ($1 ~ /chmod/ && $2 ~ /644/)
168                 next
169         
170         # 'gzip -9nf' for compressing
171         if ($1 ~ /gzip|bzip2/) {
172                 if ($2 ~ /^-/)
173                         sub(/-[A-Za-z0-9]+ /, "", $0)
174                 sub($1, "gzip -9nf")
175         }
176 }
177
178 # Scripts
179 /^%pre /, (/^[a-z]+$/ && !/^%pre /) { preamble = 0 }
180 /^%preun/, (/^[a-z]+$/ && !/^%preun/) { preamble = 0 }
181 /^%post /, (/^[a-z]+$/ && !/^%post /) { preamble = 0 }
182 /^%postun/, (/^[a-z]+$/ && !/^%postun/) { preamble = 0 }
183         
184 # %files section:
185 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
186         preamble = 0
187         
188         if ($0 ~ /^%files/)
189                 defattr = 1
190         
191         use_macros()
192 }
193
194 # %changelog section:
195 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
196         preamble = 0
197         has_changelog = 1
198         # There should be some CVS keywords on the first line of %changelog.
199         if (boc == 1) {
200                 if (!/PLD Team/) {
201                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
202                         printf "All persons listed below can be reached at " > changelog_file
203                         print "<cvs_login>@pld.org.pl\n" > changelog_file
204                         print "$" "Log:$" > changelog_file
205                 }
206                 boc = 0
207         }
208         
209         # Define date macro.
210         if (boc == 2) {
211                 if (date == 0) {
212                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
213                         print " date +\"%a %b %d %Y\"`)" > changelog_file
214                 }
215         boc--
216         }
217
218         if (!/^%[a-z]+$/ || /changelog/)
219                 print > changelog_file
220         else
221                 print
222         next
223 }
224
225 # preambles:
226 preamble == 1 {
227         # There should not be a space after the name of field
228         # and before the colon.
229         sub(/[ \t]*:/, ":")
230         
231         field = tolower($1)
232
233         if (field ~ /packager:|distribution:|docdir:|prefix:/)
234                 next
235         
236         if (field ~ /buildroot:/)
237                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
238
239         if (field ~ /group:/) {
240                 format_preamble()
241                 print $0
242                 
243                 sub($1 "[ \t]*","")
244                 translate_group($0)
245                 close(groups_file)
246                 
247                 if ($0 ~ /^X11/ && x11 == 0)    # Is it X11 application?
248                        x11 = 1
249
250                 next    # Line is already formatted and printed
251         }
252                 
253         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
254         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
255                 $1 = "License:"
256         
257         if (field ~ /name:/)
258                 name = $2
259
260         if (field ~ /version:/)
261                 version = $2
262
263         # Use %{name} and %{version} in the filenames in "Source:"
264         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
265                 n = split($2, url, /\//)
266                 filename = url[n]
267                 sub(name, "%{name}", url[n])
268                 sub(version, "%{version}", url[n])
269                 sub(filename, url[n], $2)
270         }
271
272         if (field ~ /source:/)
273                 $1 = "Source0:" 
274
275         if (field ~ /patch:/)
276                 $1 = "Patch0:"
277         
278         format_preamble()
279         
280         if ($1 ~ /%define/) {
281                 # Do not add %define of _prefix if it already is.
282                 if ($2 ~ /_prefix/) {
283                         sub("^"prefix, $3, bindir)
284                         sub("^"prefix, $3, sbindir)
285                         sub("^"prefix, $3, libdir)
286                         sub("^"prefix, $3, datadir)
287                         sub("^"prefix, $3, includedir)
288                         prefix = $3
289                         x11 = 2
290                 }
291                 if ($2 ~ /_bindir/ && !/_sbindir/)
292                         bindir = $3
293                 if ($2 ~ /_sbindir/)
294                         sbindir = $3
295                 if ($2 ~ /_libdir/)
296                         libdir = $3
297                 if ($2 ~ /_sysconfdir/)
298                         sysconfdir = $3
299                 if ($2 ~ /_datadir/)
300                         datadir = $3
301                 if ($2 ~ /_includedir/)
302                         includedir = $3
303                 if ($2 ~ /_mandir/)
304                         mandir = $3
305                 if ($2 ~ /_infodir/)
306                         infodir = $3
307         }
308 }
309
310
311 # main()  ;-)
312 {
313         preamble = 1
314         
315         print
316 }
317
318
319 END {
320         close(changelog_file)
321         while ((getline < changelog_file) > 0)
322                 print
323         system("rm -f " changelog_file)
324
325         if (date == 0) {
326                 print ""
327                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
328         }
329
330         if (boc > 0) {
331                 print "%changelog"
332                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
333                 printf "All persons listed below can be reached at "
334                 print "<cvs_login>@pld.org.pl\n"
335                 print "$" "Log:$"
336         }
337 }
338
339 # This function uses grep to determine if there is line (in the current file)
340 # which matches regexp.
341 function is_there_line(line, l)
342 {
343         command = "grep \"" line "\" " ARGV[1]
344         command | getline l
345         close(command)
346
347         if (l != "")
348                 return 1
349         else
350                 return 0
351 }
352
353 # This function prints translated names of groups.
354 function translate_group(group)
355 {
356         for(;;) {
357                 result = getline line < groups_file
358                 
359                 if (result == -1) {
360                         print "######\t\t" groups_file ": no such file"
361                         return
362                 }
363
364                 if (result == 0) {
365                         print "######\t\t" "Unknown group!"
366                         return
367                 }
368                 
369                 if (line == group) {
370                         found = 1
371                         continue
372                 }
373
374                 if (found == 1)
375                         if (line ~ /\[[a-z][a-z]\]:/) {
376                                 split(line, g, /\[|\]|\:/)
377                                 if (!is_there_line("^Group(" g[2] "):"))
378                                                 printf("Group(%s):%s\n", g[2], g[4])
379                         } else {
380                                 found = 0
381                                 return
382                         }
383         }
384 }
385
386 # There should be one or two tabs after the colon.
387 function format_preamble()
388 {
389         sub(/:[ \t]*/, ":")
390         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
391                 if (RLENGTH < 8)
392                         sub(/:/, ":\t\t")
393                 else
394                         sub(/:/, ":\t")
395         }
396 }
397
398 # Replace directly specified directories with macros
399 function use_macros()
400 {
401         gsub(bindir, "%{_bindir}")
402         gsub("%{_prefix}/bin", "%{_bindir}")
403         gsub("%{prefix}/bin", "%{_bindir}")
404
405         for (c = 1; c <= NF; c++) {
406                 if ($c ~ sbindir "/fix-info-dir")
407                         continue;
408                 gsub(sbindir, "%{_sbindir}", $c)
409         }
410
411         gsub("%{prefix}/sbin", "%{_sbindir}")
412         gsub("%{_prefix}/sbib", "%{_sbindir}")
413
414         gsub(libdir, "%{_libdir}")
415         gsub("%{prefix}/lib", "%{_libdir}")
416         gsub("%{_prefix}/lib", "%{_libdir}")
417
418         for (c = 1; c <= NF; c++) {
419                 if ($c ~ sysconfdir "/cron.d")
420                         continue;
421                 if ($c ~ sysconfdir "/crontab.d")
422                         continue;
423                 if ($c ~ sysconfdir "/logrotate.d")
424                         continue;
425                 if ($c ~ sysconfdir "/pam.d")
426                         continue;
427                 if ($c ~ sysconfdir "/profile.d")
428                         continue;
429                 if ($c ~ sysconfdir "/rc.d")
430                         continue;
431                 if ($c ~ sysconfdir "/security")
432                         continue;
433                 if ($c ~ sysconfdir "/skel")
434                         continue;
435                 if ($c ~ sysconfdir "/sysconfig")
436                         continue;
437                 gsub(sysconfdir, "%{_sysconfdir}", $c)
438         }
439
440         gsub(datadir, "%{_datadir}")
441         gsub("%{prefix}/share", "%{_datadir}")
442         gsub("%{_prefix}/share", "%{_datadir}")
443
444         gsub(includedir, "%{_includedir}")
445         gsub("%{prefix}/include", "%{_includedir}")
446         gsub("%{_prefix}/include", "%{_includedir}")
447
448         gsub(mandir, "%{_mandir}")
449         gsub("%{_datadir}/man", "%{_mandir}")
450         gsub("%{_prefix}/share/man", "%{_mandir}")
451         gsub("%{prefix}/share/man", "%{_mandir}")
452         gsub("%{prefix}/man", "%{_mandir}")
453         gsub("%{_prefix}/man", "%{_mandir}")
454
455         gsub(infodir, "%{_infodir}")
456         gsub("%{prefix}/info", "%{_infodir}")
457         gsub("%{_prefix}/info", "%{_infodir}")
458
459         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
460
461         if (prefix != "/") {
462                 for (c = 1; c <= NF; c++) {
463                         if ($c ~ prefix "/sbin/fix-info-dir")
464                                 continue;
465                         gsub(prefix, "%{_prefix}", $c)
466                 }
467                 gsub("%{prefix}", "%{_prefix}")
468         }
469
470         gsub("%{PACKAGE_VERSION}", "%{version}")
471         gsub("%{PACKAGE_NAME}", "%{name}")
472
473         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
474         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
475
476         gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
477         gsub("%{_datadir}/applnk", "%{_applnkdir}")
478 }
479
480 function fill(ch, n, i) {
481         for (i = 0; i < n; i++)
482                 printf("%c", ch)
483 }
484
485 function format_flush(line, indent, newline, word, first_word) {
486         first_word = 1
487         if (format_indent == -1) 
488                 newline = ""
489         else
490                 newline = fill(" ", format_indent) "- "
491
492         while (match(line, /[^\t ]+/)) {
493                 word = substr(line, RSTART, RLENGTH)
494                 if (length(newline) + length(word) + 1 > tw) {
495                         print newline
496                         
497                         if (format_indent == -1)
498                                 newline = ""
499                         else
500                                 newline = fill(" ", format_indent + 2)
501                         first_word = 1
502                 }
503
504                 if (first_word) {
505                         newline = newline word
506                         first_word = 0
507                 } else
508                         newline = newline " " word
509                         
510                 line = substr(line, RSTART + RLENGTH)
511         }
512         if (newline ~ /[^\t ]/) {
513                 print newline
514         }
515 }
516
This page took 0.118738 seconds and 4 git commands to generate.