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