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