]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- adapter portions moved to adapter
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.27. Adapter adapts .spec files for PLD Linux.
4 #
5 # Copyright (C) 1999-2005 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-linux.org>
11 #       Michal Kochanowicz <mkochano@pld.org.pl>
12 #       Elan Ruusamä¤e <glen@pld-linux.org>
13 #
14 # See cvs log adapter{,.awk} for list of contributors
15 #
16 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
17
18 # TODO
19 # - parse ../PLD-doc/BuildRequires.txt and setup proper BR epoches?
20 # - add "-nc" option to skip CVS interaction
21 # - sort Summary(XX)
22 # - sort Requires, BuildRequires
23 # - check if %description (lang=C) contains 8bit
24 # - desc wrapping is totally fucked up on global.spec,1.25, dosemu.spec,1.115-
25
26 BEGIN {
27         RPM_SECTIONS = "package|build|changelog|clean|description|install|post|posttrans|postun|pre|prep|pretrans|preun|triggerin|triggerpostun|triggerun"
28         SECTIONS = "^%(" RPM_SECTIONS ")"
29
30         preamble = 1            # Is it part of preamble? Default - yes
31         boc = 4                 # Beginning of %changelog
32         bod = 0                 # Beginning of %description
33         tw = 70                 # Descriptions width
34
35         b_idx = 0               # index of BR/R arrays
36
37         # If variable removed, then 1 (for removing it from export)
38         removed["LDFLAGS"] = 0
39         removed["CFLAGS"] = 0
40         removed["CXXFLAGS"] = 0
41
42         # get cvsaddress for changelog section
43         # using rpm macros as too lazy to add ~/.adapterrc parsing support.
44         "rpm --eval '%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'" | getline _cvsmaildomain
45         "rpm --eval '%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'" | getline _cvsmailfeedback
46
47         # If 1, we are inside of comment block (started with /^#%/)
48         comment_block = 0
49
50         # File with rpm groups
51         "rpm --eval %_sourcedir" | getline groups_file
52         groups_file = groups_file "/rpm.groups"
53         system("cd `rpm --eval %_sourcedir`; [ -f rpm.groups ] || cvs up rpm.groups >/dev/null")
54
55         # Temporary file for changelog section
56         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
57
58         # Load rpm macros
59         "rpm --eval %_prefix"   | getline prefix
60         "rpm --eval %_bindir"   | getline bindir
61         "rpm --eval %_sbindir"  | getline sbindir
62         "rpm --eval %_libdir"   | getline libdir
63         "rpm --eval %_sysconfdir" | getline sysconfdir
64         "rpm --eval %_datadir"  | getline datadir
65         "rpm --eval %_includedir" | getline includedir
66         "rpm --eval %_mandir"   | getline mandir
67         "rpm --eval %_infodir"  | getline infodir
68         "rpm --eval %_examplesdir"      | getline examplesdir
69         "rpm --eval %_defaultdocdir"    | getline docdir
70
71         "rpm --eval %perl_sitearch" | getline perl_sitearch
72         "rpm --eval %perl_archlib" | getline perl_archlib
73         "rpm --eval %perl_privlib" | getline perl_privlib
74         "rpm --eval %perl_vendorlib" | getline perl_vendorlib
75         "rpm --eval %perl_vendorarch" | getline perl_vendorarch
76         "rpm --eval %perl_sitelib" | getline perl_sitelib
77
78         "rpm --eval %py_sitescriptdir" | getline py_sitescriptdir
79 }
80
81 # There should be a comment with CVS keywords on the first line of file.
82 FNR == 1 {
83         if (!/# \$Revision:/)   # If this line is already OK?
84                 print "# $" "Revision:$, " "$" "Date:$" # No
85         else {
86                 print $0                                # Yes
87                 next            # It is enough for first line
88         }
89 }
90
91 # If the latest line matched /%files/
92 defattr == 1 {
93         if (ENVIRON["SKIP_DEFATTR"] != 1) {
94                 if ($0 !~ /defattr/) {  # If no %defattr
95                         print "%defattr(644,root,root,755)"     # Add it
96                 } else {
97                         $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
98                 }
99         }
100         defattr = 0
101 }
102
103 function b_makekey(a, b,        s) {
104         s = a "" b;
105         # kill bcond
106     gsub(/[#%]+{[!?]+[_a-zA-Z0-9]+:/, "", s);
107         # kill commented out items
108     gsub(/[# \t]*/, "", s);
109         return s;
110 }
111
112 # sort BR/R!
113 #
114 # NOTES:
115 # - mixing BR/R and anything else confuses this (all will be sorted together)
116 #   so don't do that.
117 # - comments leading the BR/R can not be associated,
118 #   so don't adapterize when the BR/R are mixed with comments
119 ENVIRON["SKIP_SORTBR"] != 1 && preamble == 1 && /(PreReq|(Build)?Requires)/, /(PreReq|(Build)?Requires)/ {
120         if ($1 ~ /PreReq:/) {
121                 sub(/PreReq:/, "Requires:", $1);
122         }
123         format_preamble()
124
125         b_idx++;
126         l = substr($0, index($0, $2));
127         b_ktmp = b_makekey($1, l);
128         b_key[b_idx] = b_ktmp;
129         b_val[b_ktmp] = $0;
130
131         next;
132 }
133
134 /^%bcond_/ {
135         # do nothing
136         print
137         next
138 }
139
140 preamble == 1 {
141         if (b_idx > 0) {
142                 isort(b_key, b_idx);
143                 for (i = 1; i <= b_idx; i++) {
144                         print "" b_val[b_key[i]];
145                 }
146                 b_idx = 0
147         }
148 }
149
150 # Comments
151 /^#/ && (description == 0) {
152         if (/This file does not like to be adapterized!/) {
153                 print                   # print this message
154                 while (getline)         # print the rest of spec as it is
155                         print
156                 do_not_touch_anything = 1 # do not touch anything in END()
157                 exit 0
158         }
159
160         # Generally, comments are printed without touching
161         sub(/[ \t]+$/, "")
162         print $0
163         next
164 }
165
166 /^%define/ {
167         # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
168         if ($2 == "_applnkdir") {
169                 next
170         }
171         if ($2 == "date") {
172                 date = 1
173         }
174
175         # Do not add %define of _prefix if it already is.
176         if ($2 ~ /^_prefix/) {
177                 sub("^"prefix, $3, bindir)
178                 sub("^"prefix, $3, sbindir)
179                 sub("^"prefix, $3, libdir)
180                 sub("^"prefix, $3, datadir)
181                 sub("^"prefix, $3, includedir)
182                 prefix = $3
183         }
184
185         if ($2 ~ /_bindir/ && !/_sbindir/)
186                 bindir = $3
187         if ($2 ~ /_sbindir/)
188                 sbindir = $3
189         if ($2 ~ /_libdir/)
190                 libdir = $3
191         if ($2 ~ /_sysconfdir/) {
192                 if ($3 ~ /^%\(/) {
193                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
194                         sysconfdir = "%%%%%%%%%%%%%%"
195                 } else {
196                         sysconfdir = $3
197                 }
198         }
199         if ($2 ~ /_datadir/)
200                 datadir = $3
201         if ($2 ~ /_includedir/)
202                 includedir = $3
203         if ($2 ~ /_mandir/)
204                 mandir = $3
205         if ($2 ~ /_infodir/)
206                 infodir = $3
207         if ($2 ~ /_docdir/)
208                 docdir = $3
209
210         # version related macros
211         if ($2 ~ /^_beta$/)
212                 _beta = $3
213         if ($2 ~ /^_rc$/)
214                 _rc = $3
215         if ($2 ~ /^_snap$/)
216                 _snap = $3
217
218         # these are used usually when adapterizing external spec
219         if ($2 ~ /^name$/)
220                 name = $3
221         if ($2 ~ /^version$/)
222                 version = $3
223         if ($2 ~ /^release$/)
224                 release = $3
225
226         # do nothing further, otherwise adapter thinks we're at preamble
227         print
228         next
229 }
230
231 # Obsolete
232 /^%include.*\/usr\/lib\/rpm\/macros\.python$/ {
233         next
234 }
235
236 ################
237 # %description #
238 ################
239 /^%description/, (!/^%description/ && $0 ~ SECTIONS) {
240         preamble = 0
241
242         if (/^%description/) {
243                 bod++
244                 format_line = ""
245                 format_indent = -1
246         }
247
248         # Format description
249         if (ENVIRON["SKIP_DESC"] != 1 && description == 1 && !/^%[a-z]+/ && !/^%description/) {
250                 if (/^[ \t]*$/) {
251                         format_flush(format_line, format_indent)
252                         print ""
253                         format_line = ""
254                         format_indent = -1
255                 } else if (/^[ \t]*[-\*][ \t]*/) {
256                         format_flush(format_line, format_indent)
257                         match($0, /^[ \t]*/)
258                         format_indent = RLENGTH
259                         match($0, /^[ \t]*[-\*][ \t]/)
260                         format_line = substr($0, RLENGTH)
261                 } else
262                         format_line = format_line " " $0
263                 next
264         }
265
266         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
267                 if (NF > 3 && $2 == "-l") {
268                         ll = $1
269                         for (i = 4; i <= NF; i++)
270                                 ll = ll " " $i
271                         $0 = ll " -l " $3
272                 }
273                 format_flush(format_line, format_indent)
274                 if (bod == 2) {
275                         bod = 1
276                         description = 1
277                 } else {
278                         bod = 0
279                         description = 0
280                 }
281         } else
282                 description = 1
283 }
284
285 #########
286 # %prep #
287 #########
288 /^%prep/, (!/^%prep/ && $0 ~ SECTIONS) {
289         preamble = 0
290
291         use_macros()
292
293         # Add '-q' to %setup
294         if (/^%setup/ && !/-q/) {
295                 sub(/^%setup/, "%setup -q")
296         }
297
298         if (/^%setup/) {
299                 $0 = fixedsub(name, "%{name}", $0);
300                 $0 = fixedsub(version, "%{version}", $0);
301                 if (_beta) {
302                         $0 = fixedsub(_beta, "%{_beta}", $0);
303                 }
304                 if (_rc) {
305                         $0 = fixedsub(_rc, "%{_rc}", $0);
306                 }
307                 if (_snap) {
308                         $0 = fixedsub(_snap, "%{_snap}", $0);
309                 }
310         }
311
312         if (/^%setup/ && /-n %{name}-%{version}( |$)/) {
313                 $0 = fixedsub(" -n %{name}-%{version}", "", $0)
314         }
315
316         # invalid in %prep
317         sub("^rm -rf \$RPM_BUILD_ROOT.*", "");
318 }
319
320 ##########
321 # %build #
322 ##########
323 /^%build/, (!/^%build/ && $0 ~ SECTIONS) {
324         preamble = 0
325
326         use_macros()
327
328         if (/^automake$/)
329                 sub(/$/, " -a -c")
330
331         if (/LDFLAGS/) {
332                 if (/LDFLAGS="-s"/) {
333                         removed["LDFLAGS"] = 1
334                         next
335                 } else {
336                         split($0, tmp, "LDFLAGS=")
337                         count = split(tmp[2], flags, "\"")
338                         if (flags[1] != "" && flags[1] !~ "!?debug") {
339                                 sub(/-s[" ]?/, "%{rpmldflags} ", flags[1])
340                                 $0 = tmp[1] line[1] "LDFLAGS=" flags[1] "\""
341                                 for (i = 2; i < count; i++)
342                                         $0 = $0 flags[i] "\""
343                         }
344                 }
345         }
346
347         if (/CFLAGS=/)
348                 if (cflags("CFLAGS") == 0)
349                         next
350
351         if (/CXXFLAGS=/)
352                 if (cflags("CXXFLAGS") == 0)
353                         next
354
355         if (/^export /) {
356                 if (removed["LDFLAGS"])
357                         sub(" LDFLAGS", "")
358                 if (removed["CFLAGS"])
359                         sub(" CFLAGS", "")
360                 if (removed["CXXFLAGS"])
361                         sub(" CXXFLAGS", "")
362                 # Is there still something?
363                 if (/^export[ ]*$/)
364                         next
365         }
366
367         # quote CC
368         if (/CC=%{__cc} /) {
369                 sub("CC=%{__cc}", "CC=\"%{__cc}\"")
370         }
371         
372         # use PLD Linux macros
373         $0 = fixedsub("glib-gettextize --copy --force","%{__glib_gettextize}", $0);
374         $0 = fixedsub("intltoolize --copy --force", "%{__intltoolize}", $0);
375         $0 = fixedsub("automake --add-missing --copy", "%{__automake}", $0);
376
377         sub(/^aclocal$/, "%{__aclocal}");
378         sub(/^autoheader$/, "%{__autoheader}");
379         sub(/^autoconf$/, "%{__autoconf}");
380         sub(/^automake$/, "%{__automake}");
381
382         # atrpms
383         $0 = fixedsub("%perl_configure", "%{__perl} Makefile.PL \\\n\tINSTALLDIRS=vendor", $0);
384         $0 = fixedsub("%perl_makecheck", "%{?with_tests:%{__make} test}", $0);
385 }
386
387 ##########
388 # %clean #
389 ##########
390 /^%clean/, (!/^%clean/ && $0 ~ SECTIONS) {
391         did_clean = 1
392
393         # prevent next section header like "%post -p /sbin/ldconfig" being adapterized
394         if (!/^%/) {
395                 use_macros()
396         }
397 }
398
399 ############
400 # %install #
401 ############
402 /^%install/, (!/^%install/ && $0 ~ SECTIONS) {
403
404         preamble = 0
405
406         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+\${?RPM_BUILD_ROOT}?/ && did_rmroot==0) {
407                 did_rmroot=1
408                 print "rm -rf $RPM_BUILD_ROOT"
409                 next
410         }
411
412         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
413                 print "rm -rf $RPM_BUILD_ROOT"
414                 did_rmroot=1
415         }
416
417         use_macros()
418
419         # 'install -d' instead 'mkdir -p'
420         if (/mkdir -p/)
421                 sub(/mkdir -p/, "install -d")
422
423         # 'install' instead 'cp -p'
424         if (/cp -p\b/)
425                 sub(/cp -p/, "install")
426
427         # No '-u root' or '-g root' for 'install'
428         if (/^install/ && /-[ug][ \t]*root/)
429                 gsub(/-[ug][ \t]*root /, "")
430
431         if (/^install/ && /-m[ \t]*[0-9]+/)
432                 gsub(/-m[ \t]*[0-9]+ /, "")
433
434         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
435         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
436                 next
437
438         # No lines contain 'chmod' if it sets the modes to '644'
439         if ($1 ~ /chmod/ && $2 ~ /644/)
440                 next
441
442         # foreign rpms
443         $0 = fixedsub("%buildroot", "$RPM_BUILD_ROOT", $0)
444         $0 = fixedsub("%{buildroot}", "$RPM_BUILD_ROOT", $0)
445
446         # atrpms
447         $0 = fixedsub("%perl_makeinstall", "%{__make} pure_install \\\n\tDESTDIR=$RPM_BUILD_ROOT", $0);
448 }
449
450 ##########
451 # %files #
452 ##########
453 /^%files/, (!/^%files/ && $0 ~ SECTIONS) {
454         preamble = 0
455
456         if ($0 ~ /^%files/)
457                 defattr = 1
458
459         use_files_macros()
460 }
461
462 ##############
463 # %changelog #
464 ##############
465 /^%changelog/, (!/^%changelog/ && $0 ~ SECTIONS) {
466         preamble = 0
467         has_changelog = 1
468         skip = 0
469         # There should be some CVS keywords on the first line of %changelog.
470         if (boc == 3) {
471                 if ($0 !~ _cvsmailfeedback)
472                         print "* %{date} " _cvsmailfeedback > changelog_file
473                 else
474                         skip = 1
475                 boc = 2
476         }
477         if (boc == 2 && !skip) {
478                 if (!/All persons listed below/) {
479                         printf "All persons listed below can be reached at " > changelog_file
480                         print "<cvs_login>" _cvsmaildomain "\n" > changelog_file
481                 } else
482                         skip = 1
483                 boc = 1
484         }
485         if (boc == 1 && !skip) {
486                 if (!/^$/) {
487                         if (!/\$.*Log:.*\$/)
488                                 print "$" "Log:$" > changelog_file
489                         boc = 0
490                 }
491         }
492         # Define date macro.
493         if (boc == 4) {
494                 if (date == 0) {
495                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
496                         print " date +\"%a %b %d %Y\"`)" > changelog_file
497                         date = 1
498                 }
499                 boc = 3
500         }
501
502         sub(/[ \t]+$/, "")
503         if (!/^%[a-z]+$/ || /changelog/)
504                 print > changelog_file
505         else
506                 print
507         next
508 }
509
510 ###########
511 # SCRIPTS #
512 ###########
513 /^%pre/, (!/^%pre/ && $0 ~ SECTIONS) {
514         preamble = 0
515
516         # %useradd and %groupadd may not be wrapped
517         if (/%(useradd|groupadd).*\\$/) {
518                 a = $0; getline;
519                 sub(/^[\s\t]*/, "");
520                 $0 = substr(a, 1, length(a) - 1) $0;
521         }
522 }
523
524 /^%post/, (!/^%post/ && $0 ~ SECTIONS) {
525         preamble = 0
526         use_macros()
527 }
528 /^%preun/, (!/^%preun/ && $0 ~ SECTIONS) {
529         preamble = 0
530 }
531 /^%postun/, (!/^%postun/ && $0 ~ SECTIONS) {
532         preamble = 0
533 }
534 /^%triggerin/, (!/^%triggerin/ && $0 ~ SECTIONS) {
535         preamble = 0
536 }
537 /^%triggerun/, (!/^%triggerun/ && $0 ~ SECTIONS) {
538         preamble = 0
539 }
540 /^%triggerpostun/, (!/^%triggerpostun/ && $0 ~ SECTIONS) {
541         preamble = 0
542 }
543 /^%pretrans/, (!/^%pretrans/ && $0 ~ SECTIONS) {
544         preamble = 0
545 }
546 /^%posttrans/, (!/^%posttrans/ && $0 ~ SECTIONS) {
547         preamble = 0
548 }
549
550 #############
551 # PREAMBLES #
552 #############
553 preamble == 1 {
554         # There should not be a space after the name of field
555         # and before the colon.
556         sub(/[ \t]*:/, ":")
557
558         if (/^%perl_module_wo_prefix/) {
559                 name = $2
560                 version = $3
561                 release = "0." fixedsub(".%{disttag}.at", "", $4)
562         }
563
564         field = tolower($1)
565         fieldnlower = $1
566         if (field ~ /summary:/ && !/etc\.$/) {
567                 sub(/\.$/, "", $0);
568         }
569         if (field ~ /group(\([^)]+\)):/)
570                 next
571         if (field ~ /group:/) {
572                 format_preamble()
573                 sub(/^Utilities\//,"Applications/",$2)
574                 sub(/^Games/,"Applications/Games",$2)
575                 sub(/^X11\/Games/,"X11/Applications/Games",$2)
576                 sub(/^X11\/GNOME\/Development\/Libraries/,"X11/Development/Libraries",$2)
577                 sub(/^X11\/GNOME\/Applications/,"X11/Applications",$2)
578                 sub(/^X11\/GNOME/,"X11/Applications",$2)
579                 sub(/^X11\/Utilities/,"X11/Applications",$2)
580                 sub(/^X11\/Games\/Strategy/,"X11/Applications/Games/Strategy",$2)
581                 sub(/^Shells/,"Applications/Shells",$2)
582
583                 sub(/^[^ \t]*[ \t]*/,"")
584                 Grupa = $0
585
586                 sub(/^System Environment\/Libraries$/, "Libraries", Grupa)
587                 sub(/^System Environment\/Daemons$/, "Daemons", Grupa)
588                 sub(/^Applications\/Internet$/, "Applications/Networking", Grupa)
589                 sub(/^Applications\/Daemons$/, "Daemons", Grupa)
590                 sub(/^Application\/Multimedia$/, "Applications/Multimedia", Grupa)
591                 sub(/^System\/Servers$/, "Daemons", Grupa)
592                 sub(/^X11\/Xserver$/, "X11/Servers", Grupa)
593                 sub(/^X11\/XFree86/, "X11", Grupa)
594                 sub(/^Applications\/Compilers$/, "Development/Languages", Grupa)
595                 sub(/^Applications\/Internet\/Peer to Peer/, "Applications/Networking", Grupa)
596                 sub(/^Networking\/Deamons$/, "Networking/Daemons", Grupa)
597                 sub(/^Development\/Docs$/, "Documentation", Grupa)
598
599                 print "Group:\t\t" Grupa
600                 if (Grupa ~ /^X11/ && x11 == 0) # Is it X11 application?
601                         x11 = 1
602
603                 byl_plik_z_grupami = 0
604                 byl_opis_grupy = 0
605                 while ((getline linia_grup < groups_file) > 0) {
606                         byl_plik_z_grupami = 1
607                         if (linia_grup == Grupa) {
608                                 byl_opis_grupy = 1
609                                 break
610                         }
611                 }
612
613                 if (!byl_plik_z_grupami)
614                         print "######\t\t" groups_file ": no such file"
615                 else if (!byl_opis_grupy)
616                         print "######\t\t" "Unknown group!"
617
618                 close(groups_file)
619                 next
620         }
621
622         if (field ~ /prereq:/) {
623                 sub(/PreReq:/, "Requires:", $1);
624         }
625
626         # split (build)requires, obsoletes on commas
627         if (field ~ /(obsoletes|requires):/ && $0 ~ /,/) {
628                 l = substr($0, index($0, $2));
629                 n = split(l, p, / *, */);
630                 for (i in p) {
631                         printf("%s\t%s\n", $1, p[i]);
632                 }
633                 next;
634         }
635
636         if (field ~ /packager:|distribution:|docdir:|prefix:/)
637                 next
638
639         if (field ~ /buildroot:/)
640                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
641
642         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
643         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
644                 $1 = "License:"
645
646         if (field ~ /name:/) {
647                 if ($2 == "%{name}" && name) {
648                         $2 = name
649                 }
650                 name = $2
651                 name_seen = 1;
652         }
653
654         if (field ~ /version:/) {
655                 if ($2 == "%{version}" && version) {
656                         $2 = version
657                 }
658                 version = $2
659                 version_seen = 1;
660         }
661
662         if (field ~ /release:/) {
663                 if ($2 == "%{release}" && release) {
664                         $2 = release
665                 }
666                 release = $2
667                 release_seen = 1;
668         }
669
670         if (field ~ /serial:/)
671                 $1 = "Epoch:"
672
673         # proper caps
674         if (field ~ /^url:$/)
675                 $1 = "URL:"
676
677         # Use %{name} and %{version} in the filenames in "Source:"
678         if (field ~ /^source/ || field ~ /patch/) {
679                 n = split($2, url, /\//)
680                 if (url[n] ~ /\.gz$/) {
681                         url[n+1] = ".gz" url[n+1]
682                         sub(/\.gz$/,"",url[n])
683                 }
684                 if (url[n] ~ /\.zip$/) {
685                         url[n+1] = ".zip" url[n+1]
686                         sub(/\.zip$/,"",url[n])
687                 }
688                 if (url[n] ~ /\.tar$/) {
689                         url[n+1] = ".tar" url[n+1]
690                         sub(/\.tar$/,"",url[n])
691                 }
692                 if (url[n] ~ /\.patch$/) {
693                         url[n+1] = ".patch" url[n+1]
694                         sub(/\.patch$/,"",url[n])
695                 }
696                 if (url[n] ~ /\.bz2$/) {
697                         url[n+1] = ".bz2" url[n+1]
698                         sub(/\.bz2$/,"",url[n])
699                 }
700                 if (url[n] ~ /\.logrotate$/) {
701                         url[n+1] = ".logrotate" url[n+1]
702                         sub(/\.logrotate$/,"",url[n])
703                 }
704                 if (url[n] ~ /\.pamd$/) {
705                         url[n+1] = ".pamd" url[n+1]
706                         sub(/\.pamd$/,"",url[n])
707                 }
708
709                 # allow %{name} just in last url component
710                 s = ""
711                 for (i = 1; i <= n; i++) {
712                         url[i] = fixedsub("%{name}", name, url[i])
713                         if (s) {
714                                 s = s "/" url[i]
715                         } else {
716                                 s = url[i]
717                         }
718                 }
719                 $2 = s url[n+1]
720
721                 filename = url[n]
722                 if (name) {
723                         url[n] = fixedsub(name, "%{name}", url[n])
724                 }
725                 if (field ~ /source/) {
726                         if (version) {
727                                 url[n] = fixedsub(version, "%{version}", url[n])
728                         }
729                         if (_beta) {
730                                 url[n] = fixedsub(_beta, "%{_beta}", url[n])
731                         }
732                         if (_rc) {
733                                 url[n] = fixedsub(_rc, "%{_rc}", url[n])
734                         }
735                         if (_snap) {
736                                 url[n] = fixedsub(_snap, "%{_snap}", url[n])
737                         }
738                 }
739                 $2 = fixedsub(filename, url[n], $2)
740
741                 # sourceforge urls
742                 sub("[?]use_mirror=.*$", "", $2);
743                 sub("[?]download$", "", $2);
744                 sub("^http://prdownloads\.sourceforge\.net/", "http://dl.sourceforge.net/", $2)
745
746                 sub("^http://.*\.dl\.sourceforge\.net/", "http://dl.sourceforge.net/", $2)
747                 sub("^http://dl\.sourceforge\.net/sourceforge/", "http://dl.sourceforge.net/", $2)
748                 sub("^http://dl\.sf\.net/", "http://dl.sourceforge.net/", $2)
749         }
750
751
752         if (field ~ /^source:/)
753                 $1 = "Source0:"
754
755         if (field ~ /^patch:/)
756                 $1 = "Patch0:"
757
758         format_preamble()
759
760         if (field ~ /requires/) {
761                 # atrpms
762                 $0 = fixedsub("%{eversion}", "%{epoch}:%{version}-%{release}", $0);
763         }
764 }
765
766 # main() ;-)
767 {
768         preamble = 1
769
770         sub(/[ \t]+$/, "")
771         print
772
773         if (name_seen == 0 && name) {
774                 print "Name:\t" name
775                 name_seen = 1
776         }
777
778         if (version_seen == 0 && version) {
779                 print "Version:\t" version
780                 version_seen = 1
781         }
782
783         if (release_seen == 0 && release) {
784                 print "Release:\t" release
785                 release_seen = 1
786         }
787 }
788
789
790 END {
791         if (do_not_touch_anything)
792                 exit 0
793
794         close(changelog_file)
795         while ((getline < changelog_file) > 0)
796                 print
797         system("rm -f " changelog_file)
798
799         if (did_clean == 0) {
800                 print ""
801                 print "%clean"
802                 print "rm -rf $RPM_BUILD_ROOT"
803         }
804
805         if (date == 0) {
806                 print ""
807                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
808         }
809
810         if (has_changelog == 0)
811                 print "%changelog"
812
813         if (boc > 2)
814                 print "* %{date} PLD Team <feedback@pld-linux.org>"
815         if (boc > 1) {
816                 printf "All persons listed below can be reached at "
817                 print "<cvs_login>@pld-linux.org\n"
818         }
819         if (boc > 0)
820                 print "$" "Log:$"
821 }
822
823 function fixedsub(s1,s2,t, ind) {
824 # substitutes fixed strings (not regexps)
825         if (ind = index(t,s1))
826                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
827         return t
828 }
829
830 # There should be one or two tabs after the colon.
831 function format_preamble()
832 {
833         sub(/:[ \t]*/, ":")
834         if (match($0, /[A-Za-z0-9(),#_ \t]+[ \t]*:[ \t]*/) == 1) {
835                 if (RLENGTH < 8)
836                         sub(/:/, ":\t\t")
837                 else
838                         sub(/:/, ":\t")
839         }
840 }
841
842 # Replace directly specified directories with macros
843 function use_macros()
844 {
845         # -m, --skip-macros, --no-macros -- skip macros subst
846         if (ENVIRON["SKIP_MACROS"]) {
847                 return
848         }
849
850         gsub(perl_sitearch, "%{perl_sitearch}")
851         gsub(perl_archlib, "%{perl_archlib}")
852         gsub(perl_privlib, "%{perl_privlib}")
853         gsub(perl_vendorlib, "%{perl_vendorlib}")
854         gsub(perl_vendorarch, "%{perl_vendorarch}")
855         gsub(perl_sitelib, "%{perl_sitelib}")
856         
857         gsub(py_sitescriptdir, "%{py_sitescriptdir}")
858
859         gsub(bindir, "%{_bindir}")
860         gsub("%{prefix}/bin", "%{_bindir}")
861         if(prefix"/bin" == bindir)
862                 gsub("%{_prefix}/bin", "%{_bindir}")
863
864         for (c = 1; c <= NF; c++) {
865                 if ($c ~ sbindir "/fix-info-dir")
866                         continue;
867                 if ($c ~ sbindir "/webapp")
868                         continue;
869                 if ($c ~ sbindir "/chsh")
870                         continue;
871                 if ($c ~ sbindir "/usermod")
872                         continue;
873                 gsub(sbindir, "%{_sbindir}", $c)
874         }
875
876         gsub("%{prefix}/sbin", "%{_sbindir}")
877         if (prefix"/sbin" == sbindir)
878                 gsub("%{_prefix}/sbin", "%{_sbindir}")
879
880         for (c = 1; c <= NF; c++) {
881                 if ($c ~ sysconfdir "/{?cron.")
882                         continue;
883                 if ($c ~ sysconfdir "/{?crontab.d")
884                         continue;
885                 if ($c ~ sysconfdir "/{?env.d")
886                         continue;
887                 if ($c ~ sysconfdir "/{?logrotate.d")
888                         continue;
889                 if ($c ~ sysconfdir "/{?pam.d")
890                         continue;
891                 if ($c ~ sysconfdir "/{?profile.d")
892                         continue;
893                 if ($c ~ sysconfdir "/{?rc.d")
894                         continue;
895                 if ($c ~ sysconfdir "/{?security")
896                         continue;
897                 if ($c ~ sysconfdir "/{?skel")
898                         continue;
899                 if ($c ~ sysconfdir "/{?sysconfig")
900                         continue;
901                 if ($c ~ sysconfdir "/{?shrc.d")
902                         continue;
903                 if ($c ~ sysconfdir "/{?certs")
904                         continue;
905                 if ($c ~ sysconfdir "/{?X11")
906                         continue;
907                 if ($c ~ sysconfdir "/{?ld.so.conf.d")
908                         continue;
909                 if ($c ~ sysconfdir "/{?httpd") # temp
910                         continue;
911                 gsub(sysconfdir, "%{_sysconfdir}", $c)
912         }
913
914         gsub(docdir, "%{_docdir}")
915
916         for (c = 1; c <= NF; c++) {
917                 if ($c ~ datadir "/automake")
918                         continue;
919                 if ($c ~ datadir "/unsermake")
920                         continue;
921                 if ($c ~ datadir "/file/magic.mime")
922                         continue;
923                 gsub(datadir, "%{_datadir}", $c)
924         }
925
926         gsub("%{prefix}/share", "%{_datadir}")
927         if (prefix"/share" == datadir)
928                 gsub("%{_prefix}/share", "%{_datadir}")
929
930         # CFLAGS="-I/usr/include/ncurses is usually correct.
931         if (!/-I\/usr\/include/) {
932                 gsub(includedir, "%{_includedir}")
933         }
934
935         gsub("%{prefix}/include", "%{_includedir}")
936         if (prefix"/include" == includedir) {
937                 gsub("%{_prefix}/include", "%{_includedir}")
938         }
939
940         gsub(mandir, "%{_mandir}")
941         if ($0 !~ "%{_datadir}/manual") {
942                 gsub("%{_datadir}/man", "%{_mandir}")
943         }
944         gsub("%{_prefix}/share/man", "%{_mandir}")
945         gsub("%{prefix}/share/man", "%{_mandir}")
946         gsub("%{prefix}/man", "%{_mandir}")
947         gsub("%{_prefix}/man", "%{_mandir}")
948
949         gsub(infodir, "%{_infodir}")
950         gsub("%{prefix}/info", "%{_infodir}")
951         gsub("%{_prefix}/info", "%{_infodir}")
952
953         if (prefix !~ "/X11R6") {
954                 gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
955         }
956
957         gsub(examplesdir, "%{_examplesdir}")
958
959         if (prefix != "/") {
960                 # leave --with-foo=/usr alone
961                 if ($0 !~ "--with.*=.*" prefix) {
962                         for (c = 1; c <= NF; c++) {
963                                 if ($c ~ prefix "/sbin/fix-info-dir")
964                                         continue;
965                                 if ($c ~ prefix "/sbin/webapp")
966                                         continue;
967                                 if ($c ~ prefix "/sbin/chsh")
968                                         continue;
969                                 if ($c ~ prefix "/sbin/usermod")
970                                         continue;
971                                 if ($c ~ prefix "/share/automake")
972                                         continue;
973                                 if ($c ~ prefix "/share/unsermake")
974                                         continue;
975                                 if ($c ~ prefix "/lib/sendmail")
976                                         continue;
977                                 if ($c ~ prefix "/lib/pkgconfig")
978                                         continue;
979
980                                 # CFLAGS="-I/usr..." is usually correct.
981                                 if (/-I\/usr/)
982                                         continue;
983                                 # same for LDFLAGS="-L/usr..."
984                                 if (/-L\/usr/)
985                                         continue;
986
987                                 gsub(prefix, "%{_prefix}", $c)
988                         }
989                 }
990                 gsub("%{prefix}", "%{_prefix}")
991         }
992
993         # replace back
994         gsub("%{_includedir}/ncurses", "/usr/include/ncurses")
995         gsub("%{_includedir}/freetype", "/usr/include/freetype")
996
997         gsub("%{PACKAGE_VERSION}", "%{version}")
998         gsub("%{PACKAGE_NAME}", "%{name}")
999
1000         gsub("^make$", "%{__make}")
1001         gsub("^make ", "%{__make} ")
1002         gsub("^gcc ", "%{__cc} ")
1003
1004         # mandrake specs
1005         gsub("^%make$", "%{__make}")
1006         gsub("^%make ", "%{__make} ")
1007         gsub("^%makeinstall_std", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1008         gsub("^%{makeinstall}", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1009         gsub("^%makeinstall", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1010         gsub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
1011         gsub("^%{__install}", "install")
1012         gsub("^%{__rm}", "rm")
1013         gsub("%optflags", "%{rpmcflags}")
1014         gsub("%{compat_perl_vendorarch}", "%{perl_vendorarch}")
1015
1016         gsub("^%{__make} install DESTDIR=\$RPM_BUILD_ROOT", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1017         gsub("^fix-info-dir$", "[ ! -x /usr/sbin/fix-info-dir ] || /usr/sbin/fix-info-dir -c %{_infodir} >/dev/null 2>\&1")
1018         $0 = fixedsub("%buildroot", "$RPM_BUILD_ROOT", $0)
1019         $0 = fixedsub("%{buildroot}", "$RPM_BUILD_ROOT", $0)
1020         $0 = fixedsub("CXXFLAGS=%{rpmcflags} %configure", "CXXFLAGS=%{rpmcflags}\n%configure", $0);
1021
1022         gsub("%_bindir", "%{_bindir}")
1023         gsub("%_datadir", "%{_datadir}")
1024         gsub("%_iconsdir", "%{_iconsdir}")
1025         gsub("%_sbindir", "%{_sbindir}")
1026         gsub("%_mandir", "%{_mandir}")
1027         gsub("%name", "%{name}")
1028
1029         gsub("/usr/src/linux", "%{_kernelsrcdir}")
1030         gsub("%{_prefix}/src/linux", "%{_kernelsrcdir}")
1031 }
1032
1033
1034 # insertion sort of A[1..n]
1035 # copied from mawk manual
1036 function isort(A,n,             i,j,hold) {
1037         for (i = 2; i <= n; i++) {
1038                 hold = A[j = i]
1039                 while (A[j-1] > hold) {
1040                         j-- ; A[j+1] = A[j]
1041                 }
1042                 A[j] = hold
1043         }
1044         # sentinel A[0] = "" will be created if needed
1045 }
1046
1047
1048 function use_files_macros(      i, n, t, a)
1049 {
1050         use_macros()
1051
1052         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
1053         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
1054
1055         # uid/gid nobody is not valid in %files
1056         if (/%attr([^)]*nobody[^)]*)/ && !/FIXME/) {
1057                 $0 = $0 " # FIXME nobody user/group can't own files! -adapter.awk"
1058         }
1059
1060         # replace back
1061         gsub("%{_sysconfdir}/cron\.d", "/etc/cron.d")
1062         gsub("%{_sysconfdir}/crontab\.d", "/etc/crontab.d")
1063         gsub("%{_sysconfdir}/logrotate\.d", "/etc/logrotate.d")
1064         gsub("%{_sysconfdir}/pam\.d", "/etc/pam.d")
1065         gsub("%{_sysconfdir}/profile\.d", "/etc/profile.d")
1066         gsub("%{_sysconfdir}/rc\.d", "/etc/rc.d")
1067         gsub("%{_sysconfdir}/security", "/etc/security")
1068         gsub("%{_sysconfdir}/skel", "/etc/skel")
1069         gsub("%{_sysconfdir}/sysconfig", "/etc/sysconfig")
1070         gsub("%{_sysconfdir}/certs", "/etc/certs")
1071         gsub("%{_sysconfdir}/init.d", "/etc/init.d")
1072
1073         # /etc/init.d -> /etc/rc.d/init.d
1074         if (!/^\/etc\/init\.d$/) {
1075                  gsub("/etc/init.d", "/etc/rc.d/init.d")
1076         }
1077
1078         if (/\/etc\/rc\.d\/init\.d\// && !/functions/) {
1079                 if (!/%attr.*\/etc\/rc\.d\/init\.d/) {
1080                         $0 = "%attr(754,root,root) " $0
1081                 }
1082                 if (/^%attr.*\/etc\/rc\.d\/init\.d/ && !/^%attr\(754 *,/) {
1083                         gsub("^%attr\\(... *,", "%attr(754,");
1084                 }
1085         }
1086
1087         if (/lib.+\.so/ && !/\.so$/ && !/^%attr.*/ && !/%exclude/) {
1088                 $0 = "%attr(755,root,root) " $0
1089         }
1090
1091         # /etc/sysconfig files
1092         # %attr(640,root,root) %config(noreplace) %verify(not size mtime md5) /etc/sysconfig/*
1093         # attr not required, allow default 644 attr
1094         if (!/network-scripts/ && !/%dir/ && !/\.d$/ && !/functions/ && !/\/etc\/sysconfig\/wmstyle/) {
1095                 if (/\/etc\/sysconfig\// && /%config/ && !/%config\(noreplace\)/) {
1096                         gsub("%config", "%config(noreplace)")
1097                 }
1098
1099                 if (/\/etc\/sysconfig\// && !/%config\(noreplace\)/) {
1100                         $NF = "%config(noreplace) " $NF
1101                 }
1102
1103                 if (/\/etc\/sysconfig\// && /%attr\(755/) {
1104                         gsub("^%attr\\(... *,", "%attr(640,");
1105                 }
1106
1107                 if (/\/etc\/sysconfig\// && !/%verify/) {
1108                         gsub("/etc/sysconfig", "%verify(not size mtime md5) /etc/sysconfig");
1109                 }
1110         }
1111
1112         # kill leading zeros
1113         if (/%attr\(0[1-9]/) {
1114                 gsub("%attr\\(0", "%attr(")
1115         }
1116
1117         # sort %verify attrs
1118         if (match($0, /%verify\(not([^)]+)\)/)) {
1119                 t = substr($0, RSTART, RLENGTH)
1120                 # kill commas: %verify(not,md5,size,mtime)
1121                 gsub(/,/, " ", t);
1122
1123                 gsub(/^%verify\(not |\)$/, "", t)
1124                 n = split(t, a, / /)
1125                 isort(a, n)
1126
1127                 s = "%verify(not"
1128                 for (i = 1 ; i <= n; i++) {
1129                         s = s " " a[i]
1130                 }
1131                 s = s ")"
1132
1133                 gsub(/%verify\(not[^)]+\)/, s)
1134         }
1135
1136         if (/%{_mandir}/) {
1137                 gsub("\.gz$", "*")
1138         }
1139
1140         # locale dir and no %lang -> bad
1141         if (/%{_datadir}\/locale\/.*\// && !/%(dir|lang)/) {
1142                 $(NF + 1) = "# FIXME consider using %find_lang"
1143         }
1144
1145         # atrpms
1146         $0 = fixedsub("%{perl_man1dir}", "%{_mandir}/man1", $0);
1147         $0 = fixedsub("%{perl_man3dir}", "%{_mandir}/man3", $0);
1148         $0 = fixedsub("%{perl_bin}", "%{_bindir}", $0);
1149
1150         gsub(libdir "/pkgconfig", "%{_pkgconfigdir}");
1151         gsub("%{_libdir}/pkgconfig", "%{_pkgconfigdir}");
1152         gsub("%{_prefix}/lib/pkgconfig", "%{_pkgconfigdir}");
1153
1154         gsub("%{_datadir}/applications", "%{_desktopdir}");
1155         gsub("%{_datadir}/icons", "%{_iconsdir}");
1156         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}");
1157 }
1158
1159 function fill(ch, n, i) {
1160         for (i = 0; i < n; i++)
1161                 printf("%c", ch)
1162 }
1163
1164 function format_flush(line, indent, newline, word, first_word) {
1165         first_word = 1
1166         if (format_indent == -1)
1167                 newline = ""
1168         else
1169                 newline = fill(" ", format_indent) "- "
1170
1171         while (match(line, /[^\t ]+/)) {
1172                 word = substr(line, RSTART, RLENGTH)
1173                 if (length(newline) + length(word) + 1 > tw) {
1174                         print newline
1175
1176                         if (format_indent == -1)
1177                                 newline = ""
1178                         else
1179                                 newline = fill(" ", format_indent + 2)
1180                         first_word = 1
1181                 }
1182
1183                 if (first_word) {
1184                         newline = newline word
1185                         first_word = 0
1186                 } else
1187                         newline = newline " " word
1188
1189                 line = substr(line, RSTART + RLENGTH)
1190         }
1191         if (newline ~ /[^\t ]/) {
1192                 print newline
1193         }
1194 }
1195
1196 function cflags(var)
1197 {
1198         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
1199                 removed[var] = 1
1200                 return 0
1201         }
1202
1203         if (!/!\?debug/)
1204                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
1205         return 1
1206 }
This page took 0.160964 seconds and 4 git commands to generate.