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