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