]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- change Epoch: to Serial:
[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                         date = 1
236                 }
237                 boc = 1
238         }
239
240         if (!/^%[a-z]+$/ || /changelog/)
241                 print > changelog_file
242         else
243                 print
244         next
245 }
246
247 # preambles:
248 preamble == 1 {
249         # There should not be a space after the name of field
250         # and before the colon.
251         sub(/[ \t]*:/, ":")
252         
253         field = tolower($1)
254
255         if (field ~ /packager:|distribution:|docdir:|prefix:/)
256                 next
257         
258         if (field ~ /buildroot:/)
259                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
260
261         if (field ~ /group:/) {
262                 format_preamble()
263                 print $0
264                 
265                 sub($1 "[ \t]*","")
266                 translate_group($0)
267                 close(groups_file)
268                 
269                 if ($0 ~ /^X11/ && x11 == 0)    # Is it X11 application?
270                        x11 = 1
271
272                 next    # Line is already formatted and printed
273         }
274                 
275         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
276         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
277                 $1 = "License:"
278         
279         if (field ~ /name:/)
280                 name = $2
281
282         if (field ~ /version:/)
283                 version = $2
284
285         if (field ~ /epoch:/)
286                 $1 = "Serial:"
287
288         # Use %{name} and %{version} in the filenames in "Source:"
289         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
290                 n = split($2, url, /\//)
291                 filename = url[n]
292                 sub(name, "%{name}", url[n])
293                 sub(version, "%{version}", url[n])
294                 sub(filename, url[n], $2)
295         }
296
297         if (field ~ /source:/)
298                 $1 = "Source0:" 
299
300         if (field ~ /patch:/)
301                 $1 = "Patch0:"
302         
303         format_preamble()
304         
305         if ($1 ~ /%define/) {
306                 # Do not add %define of _prefix if it already is.
307                 if ($2 ~ /_prefix/) {
308                         sub("^"prefix, $3, bindir)
309                         sub("^"prefix, $3, sbindir)
310                         sub("^"prefix, $3, libdir)
311                         sub("^"prefix, $3, datadir)
312                         sub("^"prefix, $3, includedir)
313                         prefix = $3
314                         x11 = 2
315                 }
316                 if ($2 ~ /_bindir/ && !/_sbindir/)
317                         bindir = $3
318                 if ($2 ~ /_sbindir/)
319                         sbindir = $3
320                 if ($2 ~ /_libdir/)
321                         libdir = $3
322                 if ($2 ~ /_sysconfdir/)
323                         sysconfdir = $3
324                 if ($2 ~ /_datadir/)
325                         datadir = $3
326                 if ($2 ~ /_includedir/)
327                         includedir = $3
328                 if ($2 ~ /_mandir/)
329                         mandir = $3
330                 if ($2 ~ /_infodir/)
331                         infodir = $3
332         }
333 }
334
335
336 # main()  ;-)
337 {
338         preamble = 1
339         
340         print
341 }
342
343
344 END {
345         close(changelog_file)
346         while ((getline < changelog_file) > 0)
347                 print
348         system("rm -f " changelog_file)
349
350         if (date == 0) {
351                 print ""
352                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
353         }
354         
355         if (has_changelog == 0)
356                 print "%changelog"
357
358         if (boc > 0) {
359                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
360                 printf "All persons listed below can be reached at "
361                 print "<cvs_login>@pld.org.pl\n"
362                 print "$" "Log:$"
363         }
364 }
365
366 # This function uses grep to determine if there is line (in the current file)
367 # which matches regexp.
368 function is_there_line(line, l)
369 {
370         command = "grep \"" line "\" " ARGV[1]
371         command | getline l
372         close(command)
373
374         if (l != "")
375                 return 1
376         else
377                 return 0
378 }
379
380 # This function prints translated names of groups.
381 function translate_group(group)
382 {
383         for(;;) {
384                 result = getline line < groups_file
385                 
386                 if (result == -1) {
387                         print "######\t\t" groups_file ": no such file"
388                         return
389                 }
390
391                 if (result == 0) {
392                         print "######\t\t" "Unknown group!"
393                         return
394                 }
395                 
396                 if (line == group) {
397                         found = 1
398                         continue
399                 }
400
401                 if (found == 1)
402                         if (line ~ /\[[a-z][a-z]\]:/) {
403                                 split(line, g, /\[|\]|\:/)
404                                 if (!is_there_line("^Group(" g[2] "):"))
405                                                 printf("Group(%s):%s\n", g[2], g[4])
406                         } else {
407                                 found = 0
408                                 return
409                         }
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.097868 seconds and 4 git commands to generate.