]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- rm -rf $RPM_BUILD_ROOT at very beginnning of %install
[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 = 75                 # 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}?/) {
139                 did_clean=1
140                 print "rm -rf $RPM_BUILD_ROOT"
141                 next
142         }
143
144         if (!/^$/ && !/^%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                 translate_group($2)
244                 close(groups_file)
245                 
246                 if ($2 ~ /^X11/ && x11 == 0)    # Is it X11 application?
247                        x11 = 1
248
249                 next    # Line is already formatted and printed
250         }
251                 
252         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
253         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
254                 $1 = "License:"
255         
256         if (field ~ /name:/)
257                 name = $2
258
259         if (field ~ /version:/)
260                 version = $2
261
262         # Use %{name} and %{version} in the filenames in "Source:"
263         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
264                 n = split($2, url, /\//)
265                 filename = url[n]
266                 sub(name, "%{name}", url[n])
267                 sub(version, "%{version}", url[n])
268                 sub(filename, url[n], $2)
269         }
270
271         if (field ~ /source:/)
272                 $1 = "Source0:" 
273
274         if (field ~ /patch:/)
275                 $1 = "Patch0:"
276         
277         format_preamble()
278         
279         if ($1 ~ /%define/) {
280                 # Do not add %define of _prefix if it already is.
281                 if ($2 ~ /_prefix/) {
282                         prefix = $3
283                         x11 = 2
284                 }
285                 if ($2 ~ /_bindir/ && !/_sbindir/)
286                         bindir = $3
287                 if ($2 ~ /_sbindir/)
288                         sbindir = $3
289                 if ($2 ~ /_libdir/)
290                         libdir = $3
291                 if ($2 ~ /_sysconfdir/)
292                         sysconfdir = $3
293                 if ($2 ~ /_datadir/)
294                         datadir = $3
295                 if ($2 ~ /_includedir/)
296                         includedir = $3
297                 if ($2 ~ /_mandir/)
298                         mandir = $3
299                 if ($2 ~ /_infodir/)
300                         infodir = $3
301         }
302 }
303
304
305 # main()  ;-)
306 {
307         preamble = 1
308         
309         print
310 }
311
312
313 END {
314         close(changelog_file)
315         while ((getline < changelog_file) > 0)
316                 print
317         system("rm -f " changelog_file)
318
319         if (date == 0) {
320                 print ""
321                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
322         }
323
324         if (boc > 0) {
325                 print ""
326                 print "%changelog"
327                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
328                 printf "All persons listed below can be reached at "
329                 print "<cvs_login>@pld.org.pl\n"
330                 print "$" "Log:$"
331         }
332 }
333
334 # This function uses grep to determine if there is line (in the current file)
335 # which matches regexp.
336 function is_there_line(line, l)
337 {
338         command = "grep \"" line "\" " ARGV[1]
339         command | getline l
340         close(command)
341
342         if (l != "")
343                 return 1
344         else
345                 return 0
346 }
347
348 # This function prints translated names of groups.
349 function translate_group(group)
350 {
351         for(;;) {
352                 result = getline line < groups_file
353                 
354                 if (result == -1) {
355                         print "######\t\t" groups_file ": no such file"
356                         return
357                 }
358
359                 if (result == 0) {
360                         print "######\t\t" "Unknown group!"
361                         return
362                 }
363                 
364                 if (line == group) {
365                         found = 1
366                         continue
367                 }
368
369                 if (found == 1)
370                         if (line ~ /\[[a-z][a-z]\]:/) {
371                                 split(line, g, /\[|\]|\:/)
372                                 if (!is_there_line("^Group(" g[2] "):"))
373                                                 printf("Group(%s):%s\n", g[2], g[4])
374                         } else {
375                                 found = 0
376                                 return
377                         }
378         }
379 }
380
381 # There should be one or two tabs after the colon.
382 function format_preamble()
383 {
384         sub(/:[ \t]*/, ":")
385         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
386                 if (RLENGTH < 8)
387                         sub(/:/, ":\t\t")
388                 else
389                         sub(/:/, ":\t")
390         }
391 }
392
393 # Replace directly specified directories with macros
394 function use_macros()
395 {
396         gsub(bindir, "%{_bindir}")
397         gsub("%{_prefix}/bin", "%{_bindir}")
398         gsub("%{prefix}/bin", "%{_bindir}")
399
400         gsub(sbindir, "%{_sbindir}")
401         gsub("%{prefix}/sbin", "%{_sbindir}")
402         gsub("%{_prefix}/sbib", "%{_sbindir}")
403
404         gsub(libdir, "%{_libdir}")
405         gsub("%{prefix}/lib", "%{_libdir}")
406         gsub("%{_prefix}/lib", "%{_libdir}")
407
408         for (c = 1; c <= NF; c++) {
409                 if ($c ~ sysconfdir "/sysconfig")
410                         continue;
411                 if ($c ~ sysconfdir "/rc.d")
412                         continue;
413                 if ($c ~ sysconfdir "/pam.d")
414                         continue;
415                 if ($c ~ sysconfdir "/security")
416                         continue;
417                 gsub(sysconfdir, "%{_sysconfdir}", $c)
418         }
419
420         gsub(datadir, "%{_datadir}")
421         gsub("%{prefix}/share", "%{_datadir}")
422         gsub("%{_prefix}/share", "%{_datadir}")
423
424         gsub(includedir, "%{_includedir}")
425         gsub("%{prefix}/include", "%{_includedir}")
426         gsub("%{_prefix}/include", "%{_includedir}")
427
428         gsub(mandir, "%{_mandir}")
429         gsub("%{_datadir}/man", "%{_mandir}")
430         gsub("%{_prefix}/share/man", "%{_mandir}")
431         gsub("%{prefix}/share/man", "%{_mandir}")
432         gsub("%{prefix}/man", "%{_mandir}")
433         gsub("%{_prefix}/man", "%{_mandir}")
434
435         gsub(infodir, "%{_infodir}")
436         gsub("%{prefix}/info", "%{_infodir}")
437         gsub("%{_prefix}/info", "%{_infodir}")
438
439         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
440
441         if (prefix != "/") {
442                 gsub(prefix, "%{_prefix}")
443                 gsub("%{prefix}", "%{_prefix}")
444         }
445
446         gsub("%{PACKAGE_VERSION}", "%{version}")
447         gsub("%{PACKAGE_NAME}", "%{name}")
448
449         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
450         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
451
452         gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
453         gsub("%{_datadir}/applnk", "%{_applnkdir}")
454 }
455
456 function fill(ch, n, i) {
457         for (i = 0; i < n; i++)
458                 printf("%c", ch)
459 }
460
461 function format_flush(line, indent, newline, word, first_word) {
462         first_word = 1
463         if (format_indent == -1) 
464                 newline = ""
465         else
466                 newline = fill(" ", format_indent) "- "
467
468         while (match(line, /[^\t ]+/)) {
469                 word = substr(line, RSTART, RLENGTH)
470                 if (length(newline) + length(word) + 1 > tw) {
471                         print newline
472                         
473                         if (format_indent == -1)
474                                 newline = ""
475                         else
476                                 newline = fill(" ", format_indent + 2)
477                         first_word = 1
478                 }
479
480                 if (first_word) {
481                         newline = newline word
482                         first_word = 0
483                 } else
484                         newline = newline " " word
485                         
486                 line = substr(line, RSTART + RLENGTH)
487         }
488         if (newline ~ /[^\t ]/) {
489                 print newline
490         }
491 }
492
This page took 0.091059 seconds and 4 git commands to generate.