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