]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
7685cf020d7a206f495faf8b256a408916ea074d
[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 script = 0
180 /^%pre/, (/^[a-z]+$/ && !/^%pre/) { script = 1 }
181 /^%preun/, (/^[a-z]+$/ && !/^%preun/) { script = 1 }
182 /^%post/, (/^[a-z]+$/ && !/^%post/) {   script = 1 }
183 /^%postun/, (/^[a-z]+$/ && !/^%postun/) { script = 1 }
184 script == 1 {
185         preamble = 0
186         if ($1 ~ /^mv$/) {
187                 if ($2 ~ /^-/)
188                         sub(/-[A-Za-z0-9]+ /, "", $0)
189                 sub($1, "mv -f")
190         }
191         if ($1 ~ /^rm$/) {
192                 recursive = 0
193                 if ($2 ~ /^-/) {
194                         if (match($2, "r"))
195                                 recursive = 1
196                         sub(/-[A-Za-z0-9]+ /, "", $0)
197                 }
198                 if (recursive)
199                         sub($1, "rm -rf")
200                 else
201                         sub($1, "rm -f")
202         }
203 }
204
205 # %files section:
206 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
207         preamble = 0
208         
209         if ($0 ~ /^%files/)
210                 defattr = 1
211         
212         use_macros()
213 }
214
215 # %changelog section:
216 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
217         preamble = 0
218         has_changelog = 1
219         # There should be some CVS keywords on the first line of %changelog.
220         if (boc == 1) {
221                 if (!/PLD Team/) {
222                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
223                         printf "All persons listed below can be reached at " > changelog_file
224                         print "<cvs_login>@pld.org.pl\n" > changelog_file
225                         print "$" "Log:$" > changelog_file
226                 }
227                 boc = 0
228         }
229         
230         # Define date macro.
231         if (boc == 2) {
232                 if (date == 0) {
233                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
234                         print " date +\"%a %b %d %Y\"`)" > changelog_file
235                 }
236         boc--
237         }
238
239         if (!/^%[a-z]+$/ || /changelog/)
240                 print > changelog_file
241         else
242                 print
243         next
244 }
245
246 # preambles:
247 preamble == 1 {
248         # There should not be a space after the name of field
249         # and before the colon.
250         sub(/[ \t]*:/, ":")
251         
252         field = tolower($1)
253
254         if (field ~ /packager:|distribution:|docdir:|prefix:/)
255                 next
256         
257         if (field ~ /buildroot:/)
258                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
259
260         if (field ~ /group:/) {
261                 format_preamble()
262                 print $0
263                 
264                 sub($1 "[ \t]*","")
265                 translate_group($0)
266                 close(groups_file)
267                 
268                 if ($0 ~ /^X11/ && x11 == 0)    # Is it X11 application?
269                        x11 = 1
270
271                 next    # Line is already formatted and printed
272         }
273                 
274         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
275         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
276                 $1 = "License:"
277         
278         if (field ~ /name:/)
279                 name = $2
280
281         if (field ~ /version:/)
282                 version = $2
283
284         # Use %{name} and %{version} in the filenames in "Source:"
285         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
286                 n = split($2, url, /\//)
287                 filename = url[n]
288                 sub(name, "%{name}", url[n])
289                 sub(version, "%{version}", url[n])
290                 sub(filename, url[n], $2)
291         }
292
293         if (field ~ /source:/)
294                 $1 = "Source0:" 
295
296         if (field ~ /patch:/)
297                 $1 = "Patch0:"
298         
299         format_preamble()
300         
301         if ($1 ~ /%define/) {
302                 # Do not add %define of _prefix if it already is.
303                 if ($2 ~ /_prefix/) {
304                         sub("^"prefix, $3, bindir)
305                         sub("^"prefix, $3, sbindir)
306                         sub("^"prefix, $3, libdir)
307                         sub("^"prefix, $3, datadir)
308                         sub("^"prefix, $3, includedir)
309                         prefix = $3
310                         x11 = 2
311                 }
312                 if ($2 ~ /_bindir/ && !/_sbindir/)
313                         bindir = $3
314                 if ($2 ~ /_sbindir/)
315                         sbindir = $3
316                 if ($2 ~ /_libdir/)
317                         libdir = $3
318                 if ($2 ~ /_sysconfdir/)
319                         sysconfdir = $3
320                 if ($2 ~ /_datadir/)
321                         datadir = $3
322                 if ($2 ~ /_includedir/)
323                         includedir = $3
324                 if ($2 ~ /_mandir/)
325                         mandir = $3
326                 if ($2 ~ /_infodir/)
327                         infodir = $3
328         }
329 }
330
331
332 # main()  ;-)
333 {
334         preamble = 1
335         
336         print
337 }
338
339
340 END {
341         close(changelog_file)
342         while ((getline < changelog_file) > 0)
343                 print
344         system("rm -f " changelog_file)
345
346         if (date == 0) {
347                 print ""
348                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
349         }
350
351         if (boc > 0) {
352                 print "%changelog"
353                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
354                 printf "All persons listed below can be reached at "
355                 print "<cvs_login>@pld.org.pl\n"
356                 print "$" "Log:$"
357         }
358 }
359
360 # This function uses grep to determine if there is line (in the current file)
361 # which matches regexp.
362 function is_there_line(line, l)
363 {
364         command = "grep \"" line "\" " ARGV[1]
365         command | getline l
366         close(command)
367
368         if (l != "")
369                 return 1
370         else
371                 return 0
372 }
373
374 # This function prints translated names of groups.
375 function translate_group(group)
376 {
377         for(;;) {
378                 result = getline line < groups_file
379                 
380                 if (result == -1) {
381                         print "######\t\t" groups_file ": no such file"
382                         return
383                 }
384
385                 if (result == 0) {
386                         print "######\t\t" "Unknown group!"
387                         return
388                 }
389                 
390                 if (line == group) {
391                         found = 1
392                         continue
393                 }
394
395                 if (found == 1)
396                         if (line ~ /\[[a-z][a-z]\]:/) {
397                                 split(line, g, /\[|\]|\:/)
398                                 if (!is_there_line("^Group(" g[2] "):"))
399                                                 printf("Group(%s):%s\n", g[2], g[4])
400                         } else {
401                                 found = 0
402                                 return
403                         }
404         }
405 }
406
407 # There should be one or two tabs after the colon.
408 function format_preamble()
409 {
410         sub(/:[ \t]*/, ":")
411         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
412                 if (RLENGTH < 8)
413                         sub(/:/, ":\t\t")
414                 else
415                         sub(/:/, ":\t")
416         }
417 }
418
419 # Replace directly specified directories with macros
420 function use_macros()
421 {
422         gsub(bindir, "%{_bindir}")
423         gsub("%{_prefix}/bin", "%{_bindir}")
424         gsub("%{prefix}/bin", "%{_bindir}")
425
426         for (c = 1; c <= NF; c++) {
427                 if ($c ~ sbindir "/fix-info-dir")
428                         continue;
429                 gsub(sbindir, "%{_sbindir}", $c)
430         }
431
432         gsub("%{prefix}/sbin", "%{_sbindir}")
433         gsub("%{_prefix}/sbib", "%{_sbindir}")
434
435         gsub(libdir, "%{_libdir}")
436         gsub("%{prefix}/lib", "%{_libdir}")
437         gsub("%{_prefix}/lib", "%{_libdir}")
438
439         for (c = 1; c <= NF; c++) {
440                 if ($c ~ sysconfdir "/cron.d")
441                         continue;
442                 if ($c ~ sysconfdir "/crontab.d")
443                         continue;
444                 if ($c ~ sysconfdir "/logrotate.d")
445                         continue;
446                 if ($c ~ sysconfdir "/pam.d")
447                         continue;
448                 if ($c ~ sysconfdir "/profile.d")
449                         continue;
450                 if ($c ~ sysconfdir "/rc.d")
451                         continue;
452                 if ($c ~ sysconfdir "/security")
453                         continue;
454                 if ($c ~ sysconfdir "/skel")
455                         continue;
456                 if ($c ~ sysconfdir "/sysconfig")
457                         continue;
458                 gsub(sysconfdir, "%{_sysconfdir}", $c)
459         }
460
461         gsub(datadir, "%{_datadir}")
462         gsub("%{prefix}/share", "%{_datadir}")
463         gsub("%{_prefix}/share", "%{_datadir}")
464
465         gsub(includedir, "%{_includedir}")
466         gsub("%{prefix}/include", "%{_includedir}")
467         gsub("%{_prefix}/include", "%{_includedir}")
468
469         gsub(mandir, "%{_mandir}")
470         gsub("%{_datadir}/man", "%{_mandir}")
471         gsub("%{_prefix}/share/man", "%{_mandir}")
472         gsub("%{prefix}/share/man", "%{_mandir}")
473         gsub("%{prefix}/man", "%{_mandir}")
474         gsub("%{_prefix}/man", "%{_mandir}")
475
476         gsub(infodir, "%{_infodir}")
477         gsub("%{prefix}/info", "%{_infodir}")
478         gsub("%{_prefix}/info", "%{_infodir}")
479
480         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
481
482         if (prefix != "/") {
483                 for (c = 1; c <= NF; c++) {
484                         if ($c ~ prefix "/sbin/fix-info-dir")
485                                 continue;
486                         gsub(prefix, "%{_prefix}", $c)
487                 }
488                 gsub("%{prefix}", "%{_prefix}")
489         }
490
491         gsub("%{PACKAGE_VERSION}", "%{version}")
492         gsub("%{PACKAGE_NAME}", "%{name}")
493
494         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
495         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
496
497         gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
498         gsub("%{_datadir}/applnk", "%{_applnkdir}")
499
500         gsub("^make$", "%{__make}")
501         gsub("^make ", "%{__make} ")
502 }
503
504 function fill(ch, n, i) {
505         for (i = 0; i < n; i++)
506                 printf("%c", ch)
507 }
508
509 function format_flush(line, indent, newline, word, first_word) {
510         first_word = 1
511         if (format_indent == -1) 
512                 newline = ""
513         else
514                 newline = fill(" ", format_indent) "- "
515
516         while (match(line, /[^\t ]+/)) {
517                 word = substr(line, RSTART, RLENGTH)
518                 if (length(newline) + length(word) + 1 > tw) {
519                         print newline
520                         
521                         if (format_indent == -1)
522                                 newline = ""
523                         else
524                                 newline = fill(" ", format_indent + 2)
525                         first_word = 1
526                 }
527
528                 if (first_word) {
529                         newline = newline word
530                         first_word = 0
531                 } else
532                         newline = newline " " word
533                         
534                 line = substr(line, RSTART + RLENGTH)
535         }
536         if (newline ~ /[^\t ]/) {
537                 print newline
538         }
539 }
540
This page took 0.069061 seconds and 3 git commands to generate.