]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- handle egg infos in py_sitescriptdir
[packages/rpm-build-tools.git] / adapter.awk
1 #!/usr/bin/gawk -f
2 #
3 # Adapter adapts .spec files for PLD Linux.
4 #
5 # Copyright (C) 1999-2007 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 #       Jakub Bogusz <qboosh@pld-linux.org>
13 #       Elan Ruusamäe <glen@pld-linux.org>
14 #
15 # See cvs log adapter{,.awk} for list of contributors
16 #
17 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
18
19 # TODO
20 # - really long sourceX make preamble sorting totally fcked up (try snake.spec r1.1)
21 # - parse ../PLD-doc/BuildRequires.txt and setup proper BR epoches?
22 # - add "-nc" option to skip CVS interaction
23 # - sort Summary(XX)
24 # - sort Requires, BuildRequires
25 # - check if %description (lang=C) contains 8bit
26 # - desc wrapping is totally fucked up on global.spec,1.25, dosemu.spec,1.115-
27
28 BEGIN {
29         RPM_SECTIONS = "package|build|changelog|clean|description|install|post|posttrans|postun|pre|prep|pretrans|preun|triggerin|triggerpostun|triggerun|verifyscript|check"
30         SECTIONS = "^%(" RPM_SECTIONS ")"
31
32         RCSID = "$Id$"
33         rev = RCSID # TODO: parse from RCSID
34         VERSION = "0.31/" rev
35
36         PREAMBLE_TAGS = "(R|BR|Summary|Name|Version|Release|Epoch|License|Group|URL|BuildArch|BuildRoot|Obsoletes|Conflicts|Provides|ExclusiveArch|ExcludeArch|Pre[Rr]eq|(Build)?Requires|Suggests)"
37
38         usedigest = 0   # Enable to switch to rpm 4.4.6+ md5 digests
39
40         preamble = 1    # Is it part of preamble? Default - yes
41         boc = 4                 # Beginning of %changelog
42         bod = 0                 # Beginning of %description
43         tw = 70                 # Descriptions width
44
45         b_idx = 0               # index of BR/R arrays
46         BR_count = 0    # number of additional BuildRequires
47
48         # If variable removed, then 1 (for removing it from export)
49         removed["LDFLAGS"] = 0
50         removed["CFLAGS"] = 0
51         removed["CXXFLAGS"] = 0
52
53         # get cvsaddress for changelog section
54         # using rpm macros as too lazy to add ~/.adapterrc parsing support.
55         "rpm --eval '%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'" | getline _cvsmaildomain
56         "rpm --eval '%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'" | getline _cvsmailfeedback
57
58         # If 1, we are inside of comment block (started with /^#%/)
59         comment_block = 0
60
61         # File with rpm groups
62         "rpm --eval %_sourcedir" | getline groups_file
63         groups_file = groups_file "/rpm.groups"
64         system("cd `rpm --eval %_sourcedir`; [ -f rpm.groups ] || cvs up rpm.groups >/dev/null")
65         system("[ -d ../PLD-doc ] && cd ../PLD-doc && ([ -f BuildRequires.txt ] || cvs up BuildRequires.txt >/dev/null)");
66
67         # Temporary file for changelog section
68         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
69
70         # Load rpm macros
71         "rpm --eval %_prefix"   | getline prefix
72         "rpm --eval %_bindir"   | getline bindir
73         "rpm --eval %_sbindir"  | getline sbindir
74         "rpm --eval %_libdir"   | getline libdir
75         "rpm --eval %_sysconfdir" | getline sysconfdir
76         "rpm --eval %_datadir"  | getline datadir
77         "rpm --eval %_includedir" | getline includedir
78         "rpm --eval %_mandir"   | getline mandir
79         "rpm --eval %_infodir"  | getline infodir
80         "rpm --eval %_examplesdir"      | getline examplesdir
81         "rpm --eval %_defaultdocdir"    | getline docdir
82         "rpm --eval %_kdedocdir"        | getline kdedocdir
83         "rpm --eval %_gtkdocdir"        | getline gtkdocdir
84         "rpm --eval %_desktopdir" | getline desktopdir
85         "rpm --eval %_pixmapsdir" | getline pixmapsdir
86         "rpm --eval %_javadir" | getline javadir
87
88         "rpm --eval %perl_sitearch" | getline perl_sitearch
89         "rpm --eval %perl_archlib" | getline perl_archlib
90         "rpm --eval %perl_privlib" | getline perl_privlib
91         "rpm --eval %perl_vendorlib" | getline perl_vendorlib
92         "rpm --eval %perl_vendorarch" | getline perl_vendorarch
93         "rpm --eval %perl_sitelib" | getline perl_sitelib
94
95         "rpm --eval %py_sitescriptdir 2>/dev/null" | getline py_sitescriptdir
96         "rpm --eval %py_sitedir 2>/dev/null" | getline py_sitedir
97         "rpm --eval %py_scriptdir 2>/dev/null" | getline py_scriptdir
98         "rpm --eval %py_ver 2>/dev/null" | getline py_ver
99
100         "rpm --eval %ruby_archdir" | getline ruby_archdir
101         "rpm --eval %ruby_ridir" | getline ruby_ridir
102         "rpm --eval %ruby_rubylibdir" | getline ruby_rubylibdir
103         "rpm --eval %ruby_sitearchdir" | getline ruby_sitearchdir
104         "rpm --eval %ruby_sitelibdir" | getline ruby_sitelibdir
105
106         "rpm --eval %php_pear_dir" | getline php_pear_dir
107         "rpm --eval %php_data_dir" | getline php_data_dir
108         "rpm --eval %tmpdir" | getline tmpdir
109 }
110
111 # There should be a comment with CVS keywords on the first line of file.
112 FNR == 1 {
113         if (!/# \$Revision:/)   # If this line is already OK?
114                 print "# $" "Revision:$, " "$" "Date:$" # No
115         else {
116                 print $0                                # Yes
117                 next            # It is enough for first line
118         }
119 }
120
121 # If the latest line matched /%files/
122 defattr == 1 {
123         if (ENVIRON["SKIP_DEFATTR"] != 1) {
124                 if ($0 !~ /defattr/) {  # If no %defattr
125                         print "%defattr(644,root,root,755)"     # Add it
126                 } else {
127                         $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
128                 }
129         }
130         defattr = 0
131 }
132
133 function b_makekey(a, b,        s) {
134         s = a "" b;
135         # kill bcond
136         gsub(/[#%]+{[!?]+[_a-zA-Z0-9]+:/, "", s);
137
138         # kill commented out items
139         gsub(/^#[ \t]*/, "", s);
140
141         # force order
142         gsub(/^Summary\(/, "11Summary(", s);
143         gsub(/^Summary/, "10Summary", s);
144         gsub(/^Name/, "2Name", s);
145         gsub(/^Version/, "3Version", s);
146         gsub(/^Release/, "4Release", s);
147         gsub(/^Epoch/, "5Epoch", s);
148         gsub(/^License/, "5License", s);
149         gsub(/^Group/, "6Group", s);
150         gsub(/^URL/, "7URL", s);
151
152         gsub(/^BuildRequires/, "B1BuildRequires", s);
153         gsub(/^BuildConflicts/, "B2BuildConflicts", s);
154
155         gsub(/^Suggests/, "X1Suggests", s);
156         gsub(/^Provides/, "X2Provides", s);
157         gsub(/^Obsoletes/, "X3Obsoletes", s);
158         gsub(/^Conflicts/, "X4Conflicts", s);
159         gsub(/^BuildArch/, "X5BuildArch", s);
160         gsub(/^ExclusiveArch/, "X6ExclusiveArch", s);
161         gsub(/^ExcludeArch/, "X7ExcludeArch", s);
162         gsub(/^BuildRoot/, "X9BuildRoot", s);
163
164 #       printf("%s -> %s\n", a""b, s);
165         return s;
166 }
167
168 # Comments
169 /^#/ && (description == 0) {
170         if (/This file does not like to be adapterized!/) {
171                 print                   # print this message
172                 while (getline)         # print the rest of spec as it is
173                         print
174                 do_not_touch_anything = 1 # do not touch anything in END()
175                 exit 0
176         }
177
178         # Generally, comments are printed without touching
179         sub(/[ \t]+$/, "")
180
181         if (/#[ \t]*Source.*md5/) {
182                 if (usedigest == 1) {
183                         sub(/^#[ \t]*Source/, "BuildRequires:\tdigest(%SOURCE", $0)
184                         sub(/-md5[ \t]*:[ \t]*/, ") = ", $0)
185                 }
186                 print $0
187                 next
188         }
189 }
190
191 /^%define/ {
192         # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
193         if ($2 == "_applnkdir") {
194                 next
195         }
196         if ($2 == "date") {
197                 date = 1
198                 if (did_files == 0) {
199                         print "%files"
200                         print ""
201                         did_files = 1
202                 }
203         }
204
205         # Do not add %define of _prefix if it already is.
206         if ($2 ~ /^_prefix/) {
207                 sub("^"prefix, $3, bindir)
208                 sub("^"prefix, $3, sbindir)
209                 sub("^"prefix, $3, libdir)
210                 sub("^"prefix, $3, datadir)
211                 sub("^"prefix, $3, includedir)
212                 prefix = $3
213         }
214
215         if ($2 ~ /_bindir/ && !/_sbindir/)
216                 bindir = $3
217         if ($2 ~ /_sbindir/)
218                 sbindir = $3
219         if ($2 ~ /_libdir/) {
220                 if ($3 ~ /^%\(/) {
221                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
222                         libdir = "%%%%%%%%%%%%%%"
223                 } else {
224                         libdir = $3
225                 }
226         }
227         if ($2 ~ /_sysconfdir/) {
228                 if ($3 ~ /^%\(/) {
229                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
230                         sysconfdir = "%%%%%%%%%%%%%%"
231                 } else {
232                         sysconfdir = $3
233                 }
234         }
235         if ($2 ~ /_datadir/) {
236                 if ($3 ~ /^%\(/) {
237                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
238                         datadir = "%%%%%%%%%%%%%%"
239                 } else {
240                         datadir = $3
241                 }
242         }
243         if ($2 ~ /_includedir/)
244                 includedir = $3
245         if ($2 ~ /_mandir/)
246                 mandir = $3
247         if ($2 ~ /_infodir/)
248                 infodir = $3
249         if ($2 ~ /_docdir/)
250                 docdir = $3
251
252         # version related macros
253         if ($2 ~ /^_beta$/)
254                 _beta = $3
255         if ($2 ~ /^_rc$/)
256                 _rc = $3
257         if ($2 ~ /^_pre$/)
258                 _pre = $3
259         if ($2 ~ /^_snap$/)
260                 _snap = $3
261         if ($2 ~ /^subver$/)
262                 subver = $3
263
264         # these are used usually when adapterizing external spec
265         if ($2 ~ /^name$/)
266                 name = $3
267         if ($2 ~ /^version$/)
268                 version = $3
269         if ($2 ~ /^release$/)
270                 release = $3
271
272         if ($2 ~ /^mod_name$/)
273                 mod_name = $3
274
275         sub(/[ \t]+$/, "");
276         # do nothing further, otherwise adapter thinks we're at preamble
277         print
278         next
279 }
280
281 # Obsolete
282 /^%include.*\/usr\/lib\/rpm\/macros\.python$/ {
283         next
284 }
285
286 ################
287 # %description #
288 ################
289 /^%description/, (!/^%description/ && $0 ~ SECTIONS) {
290         preamble = 0
291
292         if (/^%description/) {
293                 bod++
294                 format_line = ""
295                 format_indent = -1
296         }
297
298         # Format description
299         if (ENVIRON["SKIP_DESC"] != 1 && description == 1 && !/^%[a-z]+/ && !/^%description/) {
300                 if (/^[ \t]*$/) {
301                         format_flush(format_line, format_indent)
302                         print ""
303                         format_line = ""
304                         format_indent = -1
305                 } else if (/^[ \t]*[-\*][ \t]*/) {
306                         format_flush(format_line, format_indent)
307                         match($0, /^[ \t]*/)
308                         format_indent = RLENGTH
309                         match($0, /^[ \t]*[-\*][ \t]/)
310                         format_line = substr($0, RLENGTH)
311                 } else
312                         format_line = format_line " " $0
313                 next
314         }
315
316         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
317                 if (NF > 3 && $2 == "-l") {
318                         ll = $1
319                         for (i = 4; i <= NF; i++)
320                                 ll = ll " " $i
321                         $0 = ll " -l " $3
322                 }
323                 format_flush(format_line, format_indent)
324                 if (bod == 2) {
325                         bod = 1
326                         description = 1
327                 } else {
328                         bod = 0
329                         description = 0
330                 }
331         } else
332                 description = 1
333 }
334
335 #########
336 # %prep #
337 #########
338 /^%prep/, (!/^%prep/ && $0 ~ SECTIONS) {
339         preamble = 0
340         did_prep = 1
341
342         use_macros()
343
344         # Add '-q' to %setup
345         if (/^%setup/ && !/-q/) {
346                 sub(/^%setup/, "%setup -q")
347         }
348
349         if (/^%setup/ && name != "setup") {
350                 $0 = fixedsub(name, "%{name}", $0);
351                 $0 = fixedsub(version, "%{version}", $0);
352                 if (_beta) {
353                         $0 = fixedsub(_beta, "%{_beta}", $0);
354                 }
355                 if (_rc) {
356                         $0 = fixedsub(_rc, "%{_rc}", $0);
357                 }
358                 if (_pre) {
359                         $0 = fixedsub(_pre, "%{_pre}", $0);
360                 }
361                 if (_snap) {
362                         $0 = fixedsub(_snap, "%{_snap}", $0);
363                 }
364                 if (subver) {
365                         $0 = fixedsub(subver, "%{subver}", $0);
366                 }
367         }
368
369         if (/^%setup/ && /-n %{name}-%{version}( |$)/) {
370                 $0 = fixedsub(" -n %{name}-%{version}", "", $0)
371         }
372         sub("^%patch ", "%patch0 ");
373
374         # invalid in %prep
375         sub("^rm -rf \$RPM_BUILD_ROOT.*", "");
376 }
377
378 ##########
379 # %build #
380 ##########
381 /^%build/, (!/^%build/ && $0 ~ SECTIONS) {
382         preamble = 0
383
384         if (did_prep == 0) {
385                 print "%prep"
386                 print ""
387                 did_prep = 1
388         }
389
390         use_macros()
391         use_tabs()
392
393         if (/^automake$/)
394                 sub(/$/, " -a -c")
395
396         if (/LDFLAGS/) {
397                 if (/LDFLAGS="-s"/) {
398                         removed["LDFLAGS"] = 1
399                         next
400                 } else {
401                         split($0, tmp, "LDFLAGS=")
402                         count = split(tmp[2], flags, "\"")
403                         if (flags[1] != "" && flags[1] !~ "!?debug") {
404                                 sub(/-s[" ]?/, "%{rpmldflags} ", flags[1])
405                                 $0 = tmp[1] line[1] "LDFLAGS=" flags[1] "\""
406                                 for (i = 2; i < count; i++)
407                                         $0 = $0 flags[i] "\""
408                         }
409                 }
410         }
411
412         if (/CFLAGS=/)
413                 if (cflags("CFLAGS") == 0)
414                         next
415
416         if (/CXXFLAGS=/)
417                 if (cflags("CXXFLAGS") == 0)
418                         next
419
420         if (/^export /) {
421                 if (removed["LDFLAGS"])
422                         sub(" LDFLAGS", "")
423                 if (removed["CFLAGS"])
424                         sub(" CFLAGS", "")
425                 if (removed["CXXFLAGS"])
426                         sub(" CXXFLAGS", "")
427                 # Is there still something?
428                 if (/^export[ ]*$/)
429                         next
430         }
431
432         # quote CC
433         if (/CC=%{__cc} /) {
434                 sub("CC=%{__cc}", "CC=\"%{__cc}\"")
435         }
436         
437         # use PLD Linux macros
438         $0 = fixedsub("glib-gettextize --copy --force","%{__glib_gettextize}", $0);
439         $0 = fixedsub("intltoolize --copy --force", "%{__intltoolize}", $0);
440         $0 = fixedsub("automake --add-missing --copy", "%{__automake}", $0);
441         $0 = fixedsub("automake -a --foreign --copy", "%{__automake}", $0);
442         $0 = fixedsub("automake -a -c --foreign", "%{__automake}", $0);
443         $0 = fixedsub("automake -a -c", "%{__automake}", $0);
444         $0 = fixedsub("libtoolize --force --automake --copy", "%{__libtoolize}", $0);
445         $0 = fixedsub("libtoolize -c -f --automake", "%{__libtoolize}", $0);
446
447         sub(/^aclocal$/, "%{__aclocal}");
448         sub(/^autoheader$/, "%{__autoheader}");
449         sub(/^autoconf$/, "%{__autoconf}");
450         sub(/^automake$/, "%{__automake}");
451         sub(/^libtoolize$/, "%{__libtoolize}");
452
453         # atrpms
454         $0 = fixedsub("%perl_configure", "%{__perl} Makefile.PL \\\n\tINSTALLDIRS=vendor", $0);
455         $0 = fixedsub("%perl_makecheck", "%{?with_tests:%{__make} test}", $0);
456 }
457
458 ##########
459 # %clean #
460 ##########
461 /^%clean/, (!/^%clean/ && $0 ~ SECTIONS) {
462         did_clean = 1
463
464         use_macros()
465 }
466
467 ############
468 # %install #
469 ############
470 /^%install/, (!/^%install/ && $0 ~ SECTIONS) {
471
472         preamble = 0
473
474         # foreign rpms
475         sub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
476         sub("%buildroot", "$RPM_BUILD_ROOT");
477         sub("%{buildroot}", "$RPM_BUILD_ROOT");
478
479         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+(\${?RPM_BUILD_ROOT}?|%{?buildroot}?)/ && did_rmroot==0) {
480                 did_rmroot=1
481                 print "rm -rf $RPM_BUILD_ROOT"
482                 next
483         }
484
485         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
486                 print "rm -rf $RPM_BUILD_ROOT"
487                 did_rmroot=1
488         }
489
490         if (tmpdir) {
491                 buildroot = tmpdir "/" name "-" version "-root-" ENVIRON["USER"]
492                 gsub(buildroot, "$RPM_BUILD_ROOT")
493         }
494
495         if (!/%{_lib}/) {
496                 sub("\$RPM_BUILD_ROOT/%", "$RPM_BUILD_ROOT%")
497         }
498
499         use_macros()
500
501         # 'install -d' instead 'mkdir -p'
502         if (/mkdir -p/)
503                 sub(/mkdir -p/, "install -d")
504
505         # cp -a already implies cp -r
506         sub(/^cp -ar/, "cp -a")
507
508         # No '-u root' or '-g root' for 'install'
509         if (/^install/ && /-[ug][ \t]*root/)
510                 gsub(/-[ug][ \t]*root /, "")
511
512         if (/^install/ && /-m[ \t]*[0-9]+/)
513                 gsub(/-m[ \t]*[0-9]+ /, "")
514
515         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
516         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
517                 next
518
519         # No lines contain 'chmod' if it sets the modes to '644'
520         if ($1 ~ /chmod/ && $2 ~ /644/)
521                 next
522
523         # atrpms
524         $0 = fixedsub("%perl_makeinstall", "%{__make} pure_install \\\n\tDESTDIR=$RPM_BUILD_ROOT", $0);
525 }
526
527 ##########
528 # %files #
529 ##########
530 /^%files/, (!/^%files/ && $0 ~ SECTIONS) {
531         preamble = 0
532         did_files = 1
533
534         if ($0 ~ /^%files/)
535                 defattr = 1
536
537         use_files_macros()
538 }
539
540 ##############
541 # %changelog #
542 ##############
543 /^%changelog/, (!/^%changelog/ && $0 ~ SECTIONS) {
544         preamble = 0
545         has_changelog = 1
546         skip = 0
547         # There should be some CVS keywords on the first line of %changelog.
548         if (boc == 3) {
549                 if ($0 !~ _cvsmailfeedback) {
550                         print "* %{date} " _cvsmailfeedback > changelog_file
551                 } else {
552                         skip = 1
553                 }
554                 boc = 2
555         }
556         if (boc == 2 && !skip) {
557                 if (!/All persons listed below/) {
558                         printf "All persons listed below can be reached at " > changelog_file
559                         print "<cvs_login>" _cvsmaildomain "\n" > changelog_file
560                 } else {
561                         skip = 1
562                 }
563                 boc = 1
564         }
565         if (boc == 1 && !skip) {
566                 if (!/^$/) {
567                         if (!/\$.*Log:.*\$/) {
568                                 print "$" "Log:$" > changelog_file
569                         }
570                         boc = 0
571                 }
572         }
573         # Define date macro.
574         if (boc == 4) {
575                 if (date == 0) {
576                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
577                         print " date +\"%a %b %d %Y\"`)" > changelog_file
578                         date = 1
579                 }
580                 boc = 3
581         }
582
583         sub(/[ \t]+$/, "");
584         if (!/^%[a-z]+$/ || /changelog/) {
585                 # stop changelog if "real" changelog starts
586                 if (boc == 0 && /^\* /) {
587                         boc = -1
588                 }
589                 if (boc == -1) {
590                         next;
591                 }
592                 print > changelog_file
593         } else {
594                 print
595         }
596         next
597 }
598
599 ###########
600 # SCRIPTS #
601 ###########
602 /^%pre/, (!/^%pre/ && $0 ~ SECTIONS) {
603         preamble = 0
604
605         if (gsub("/usr/sbin/useradd", "%useradd")) {
606                 sub(" 2> /dev/null \|\| :", "");
607                 sub(" >/dev/null 2>&1 \|\|:", "");
608         }
609
610         # %useradd and %groupadd may not be wrapped
611         if (/%(useradd|groupadd).*\\$/) {
612                 a = $0; getline;
613                 sub(/^[\s\t]*/, "");
614                 $0 = substr(a, 1, length(a) - 1) $0;
615         }
616         use_script_macros()
617 }
618
619 /^%post/, (!/^%post/ && $0 ~ SECTIONS) {
620         preamble = 0
621         use_macros()
622 }
623 /^%preun/, (!/^%preun/ && $0 ~ SECTIONS) {
624         preamble = 0
625         use_macros()
626 }
627 /^%postun/, (!/^%postun/ && $0 ~ SECTIONS) {
628         preamble = 0
629         use_script_macros()
630 }
631 /^%triggerin/, (!/^%triggerin/ && $0 ~ SECTIONS) {
632         preamble = 0
633         use_script_macros()
634 }
635 /^%triggerun/, (!/^%triggerun/ && $0 ~ SECTIONS) {
636         preamble = 0
637         use_script_macros()
638 }
639 /^%triggerpostun/, (!/^%triggerpostun/ && $0 ~ SECTIONS) {
640         preamble = 0
641         use_script_macros()
642 }
643 /^%pretrans/, (!/^%pretrans/ && $0 ~ SECTIONS) {
644         preamble = 0
645         use_script_macros()
646 }
647 /^%posttrans/, (!/^%posttrans/ && $0 ~ SECTIONS) {
648         preamble = 0
649         use_script_macros()
650 }
651 /^%verifyscript/, (!/^%verifyscript/ && $0 ~ SECTIONS) {
652         preamble = 0
653         use_script_macros()
654 }
655 /^%check/, (!/^%check/ && $0 ~ SECTIONS) {
656         preamble = 0
657         use_script_macros()
658 }
659
660 #############
661 # PREAMBLES #
662 #############
663 preamble == 1 {
664         # There should not be a space after the name of field
665         # and before the colon.
666         sub(/[ \t]*:/, ":")
667
668         if (/^%perl_module_wo_prefix/) {
669                 name = $2
670                 version = $3
671                 release = "0." fixedsub(".%{disttag}.at", "", $4)
672         }
673
674         field = tolower($1)
675         fieldnlower = $1
676         if (field ~ /summary:/ && !/etc\.$/ && !/Inc\.$/) {
677                 sub(/\.$/, "", $0);
678         }
679         if (field ~ /group(\([^)]+\)):/)
680                 next
681         if (field ~ /group:/) {
682                 format_preamble()
683                 group = $0;
684                 sub(/^[^ \t]*[ \t]*/, "", group);
685                 group = replace_groupnames(group);
686                 $0 = "Group:\t\t" group
687
688                 if (group ~ /^X11/ && x11 == 0) # Is it X11 application?
689                         x11 = 1
690
691                 byl_plik_z_groupmi = 0
692                 byl_opis_grupy = 0
693                 while ((getline linia_grup < groups_file) > 0) {
694                         byl_plik_z_groupmi = 1
695                         if (linia_grup == group) {
696                                 byl_opis_grupy = 1
697                                 break
698                         }
699                 }
700
701                 if (!byl_plik_z_groupmi)
702                         print "######\t\t" groups_file ": no such file"
703                 else if (!byl_opis_grupy)
704                         print "######\t\t" "Unknown group!"
705
706                 close(groups_file)
707                 did_groups = 1
708         }
709
710         if (field ~ /prereq:/) {
711                 sub(/Pre[Rr]eq:/, "Requires:", $1);
712         }
713
714         # split (build)requires, obsoletes on commas
715         if (field ~ /(obsoletes|requires|provides|conflicts):/ && NF > 2) {
716                 value = substr($0, index($0, $2));
717                 $0 = format_requires($1, value);
718         }
719
720         # BR: tar (and others) is to common (rpm-build requires it)
721         if (field ~ /^buildrequires:/) {
722                 l = substr($0, index($0, $2));
723                 if (l == "awk" ||
724                         l == "binutils" ||
725                         l == "bzip2" ||
726                         l == "cpio" ||
727                         l == "diffutils" ||
728                         l == "elfutils" ||
729                         l == "fileutils" ||
730                         l == "findutils" ||
731                         l == "glibc-devel" ||
732                         l == "grep" ||
733                         l == "gzip" ||
734                         l == "make" ||
735                         l == "patch" ||
736                         l == "sed" ||
737                         l == "sh-utils" ||
738                         l == "tar" ||
739                         l == "textutils") {
740                         next
741                 }
742
743                 replace_requires();
744         }
745
746         if (field ~ /^requires:/) {
747                 replace_requires();
748         }
749
750
751         # obsolete/unwanted tags
752         if (field ~ /vendor:|packager:|distribution:|docdir:|prefix:|icon:|author:|author-email:|metadata-version:/) {
753                 next
754         }
755
756         if (field ~ /buildroot:/) {
757                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
758                 did_build_root = 1
759         }
760
761         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
762         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/) {
763                 $1 = "License:"
764         }
765
766         if (field ~ /license:/) {
767                 l = substr($0, index($0, $2));
768                 if (l == "Python Software Foundation License") {
769                         l = "PSF"
770                 }
771                 if (l == "Apache License 2.0" || l == "Apache 2.0" || l == "Apache License Version 2.0" || l == "Apache License, Version 2.0" || l == "Apache Software License v2") {
772                         l = "Apache v2.0"
773                 }
774                 if (l == "Apache Group License" || l == "Apache Software License" || l == "Apache License") {
775                         l = "Apache"
776                 }
777                 if (l == "Apache-style License" || l == "Apache-style Software License") {
778                         l = "Apache-like"
779                 }
780                 if (l == "Apache Software License 1.1" || l == "Apache 1.1") {
781                         l = "Apache v1.1"
782                 }
783                 if (l == "GPLv2") {
784                         l = "GPL v2"
785                 }
786                 if (l == "GPLv2+") {
787                         l = "GPL v2+"
788                 }
789                 $0 = "License:\t" l;
790         }
791
792
793         if (field ~ /name:/) {
794                 if ($2 == "%{name}" && name) {
795                         $2 = name
796                 }
797                 name = $2
798                 name_seen = 1;
799         }
800
801         if (field ~ /version:/) {
802                 if ($2 == "%{version}" && version) {
803                         $2 = version
804                 }
805                 version = $2
806                 version_seen = 1;
807         }
808
809         if (field ~ /release:/) {
810                 if ($2 == "%{release}" && release) {
811                         $2 = release
812                 }
813                 sub(/%atrelease /, "0.", $0)
814                 release = $2
815                 release_seen = 1;
816         }
817
818
819         if (field ~ /serial:/)
820                 $1 = "Epoch:"
821
822         if (field ~ /home-page:/)
823                 $1 = "URL:"
824
825         # proper caps
826         if (field ~ /^url:$/)
827                 $1 = "URL:"
828
829         if (field ~ /^description:$/)
830                 $1 = "\n%description\n"
831
832         # Use %{name} and %{version} in the filenames in "Source:"
833         if (field ~ /^source/ || field ~ /patch/) {
834                 n = split($2, url, /\//)
835                 if (url[n] ~ /\.gz$/) {
836                         url[n+1] = ".gz" url[n+1]
837                         sub(/\.gz$/,"",url[n])
838                 }
839                 if (url[n] ~ /\.zip$/) {
840                         url[n+1] = ".zip" url[n+1]
841                         sub(/\.zip$/,"",url[n])
842                 }
843                 if (url[n] ~ /\.tar$/) {
844                         url[n+1] = ".tar" url[n+1]
845                         sub(/\.tar$/,"",url[n])
846                 }
847                 if (url[n] ~ /\.patch$/) {
848                         url[n+1] = ".patch" url[n+1]
849                         sub(/\.patch$/,"",url[n])
850                 }
851                 if (url[n] ~ /\.bz2$/) {
852                         url[n+1] = ".bz2" url[n+1]
853                         sub(/\.bz2$/,"",url[n])
854                 }
855                 if (url[n] ~ /\.logrotate$/) {
856                         url[n+1] = ".logrotate" url[n+1]
857                         sub(/\.logrotate$/,"",url[n])
858                 }
859                 if (url[n] ~ /\.pamd$/) {
860                         url[n+1] = ".pamd" url[n+1]
861                         sub(/\.pamd$/,"",url[n])
862                 }
863
864                 # allow %{name} only in last url component
865                 s = ""
866                 for (i = 1; i <= n; i++) {
867                         url[i] = fixedsub("%{name}", name, url[i])
868                         if (s) {
869                                 s = s "/" url[i]
870                         } else {
871                                 s = url[i]
872                         }
873                 }
874                 $2 = s url[n+1]
875
876                 filename = url[n]
877                 if (name) {
878                         url[n] = fixedsub(name, "%{name}", url[n])
879                 }
880                 if (field ~ /source/) {
881                         if (version) {
882                                 url[n] = fixedsub(version, "%{version}", url[n])
883                         }
884                         if (_beta) {
885                                 url[n] = fixedsub(_beta, "%{_beta}", url[n])
886                         }
887                         if (_rc) {
888                                 url[n] = fixedsub(_rc, "%{_rc}", url[n])
889                         }
890                         if (_pre) {
891                                 url[n] = fixedsub(_pre, "%{_pre}", url[n])
892                         }
893                         if (_snap) {
894                                 url[n] = fixedsub(_snap, "%{_snap}", url[n])
895                         }
896                         if (subver) {
897                                 url[n] = fixedsub(subver, "%{subver}", url[n])
898                         }
899                 }
900                 # assigning to $2 kills preamble formatting
901                 $2 = fixedsub(filename, url[n], $2)
902
903                 $2 = unify_url($2)
904         }
905
906
907         if (field ~ /^source:/)
908                 $1 = "Source0:"
909
910         if (field ~ /^patch:/)
911                 $1 = "Patch0:"
912
913         kill_preamble_macros();
914         format_preamble()
915
916         if (field ~ /requires/) {
917                 # atrpms
918                 $0 = fixedsub("%{eversion}", "%{epoch}:%{version}-%{release}", $0);
919         }
920 }
921
922 /^%bcond_/ {
923         # do nothing
924         print
925         next
926 }
927
928 # sort BR/R!
929 #
930 # NOTES:
931 # - mixing BR/R and anything else confuses this (all will be sorted together)
932 #       so don't do that.
933 # - comments leading the BR/R can not be associated,
934 #       so don't adapterize when the BR/R are mixed with comments
935 ENVIRON["SKIP_SORTBR"] != 1 && preamble == 1 && $0 ~ PREAMBLE_TAGS, $0 ~ PREAMBLE_TAGS {
936         if ($1 ~ /Pre[Rr]eq:/) {
937                 sub(/Pre[Rr]eq:/, "Requires:", $1);
938         }
939         if ($1 == "BR:" ) {
940                 $1 = "BuildRequires:"
941         }
942         if ($1 == "R:" ) {
943                 $1 = "Requires:"
944         }
945         format_preamble()
946 #       kill_preamble_macros(); # breaks tabbing
947
948         b_idx++;
949         l = substr($0, index($0, $2));
950         b_ktmp = b_makekey($1, l);
951         b_key[b_idx] = b_ktmp;
952         b_val[b_ktmp] = $0;
953
954         next;
955 }
956
957 preamble == 1 {
958         if (b_idx > 0) {
959                 isort(b_key, b_idx);
960                 for (i = 1; i <= b_idx; i++) {
961                         v = b_val[b_key[i]];
962                         sub(/[ \t]+$/, "", v);
963                         print "" v;
964                 }
965                 b_idx = 0
966         }
967 }
968
969 # main() ;-)
970 {
971         preamble = 1
972
973         sub(/[ \t]+$/, "")
974         print
975
976         if (name_seen == 0 && name) {
977                 print "Name:\t\t" name
978                 name_seen = 1
979         }
980
981         if (version_seen == 0 && version) {
982                 print "Version:\t" version
983                 version_seen = 1
984         }
985
986         if (release_seen == 0 && release) {
987                 print "Release:\t" release
988                 release_seen = 1
989         }
990
991         if (did_build_root == 0) {
992 #               print "BuildRoot:\t%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
993                 did_build_root = 1
994         }
995         if (did_groups == 0) {
996 #               print "Group:\t\tunknown"
997                 did_groups = 1
998         }
999 }
1000
1001
1002 END {
1003         if (do_not_touch_anything)
1004                 exit 0
1005
1006         # TODO: need to output these in proper place
1007         if (BR_count > 0) {
1008                 for (i = 0; i <= BR_count; i++) {
1009                         print BR[i];
1010                 }
1011         }
1012
1013         close(changelog_file)
1014         while ((getline < changelog_file) > 0)
1015                 print
1016         system("rm -f " changelog_file)
1017
1018         if (did_clean == 0) {
1019                 print ""
1020                 print "%clean"
1021                 print "rm -rf $RPM_BUILD_ROOT"
1022         }
1023
1024         if (date == 0) {
1025                 print ""
1026                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
1027         }
1028
1029         if (has_changelog == 0) {
1030                 print "%changelog"
1031         }
1032
1033         if (boc > 2) {
1034                 print "* %{date} PLD Team <feedback@pld-linux.org>"
1035         }
1036         if (boc > 1) {
1037                 printf "All persons listed below can be reached at "
1038                 print "<cvs_login>@pld-linux.org\n"
1039         }
1040         if (boc > 0) {
1041                 print "$" "Log:$"
1042         }
1043 }
1044
1045 function fixedsub(s1,s2,t, ind) {
1046 # substitutes fixed strings (not regexps)
1047         if (ind = index(t,s1))
1048                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
1049         return t
1050 }
1051
1052 # There should be one or two tabs after the colon.
1053 function format_preamble()
1054 {
1055         if (/^#/ || /^%bcond_with/) {
1056                 return;
1057         }
1058         sub(/:[ \t]*/, ":")
1059         if (match($0, /[A-Za-z0-9(),#_ \t.-]+[ \t]*:[ \t]*/) == 1) {
1060                 if (RLENGTH < 8) {
1061                         sub(/:/, ":\t\t")
1062                 } else {
1063                         sub(/:/, ":\t")
1064                 }
1065         }
1066 }
1067
1068 # Replace directly specified directories with macros
1069 function use_macros()
1070 {
1071         # -m, --skip-macros, --no-macros -- skip macros subst
1072         if (ENVIRON["SKIP_MACROS"]) {
1073                 return
1074         }
1075
1076         # leave inline sed lines alone
1077         if (/(%{__sed}|sed) -i -e/) {
1078                 return;
1079         }
1080
1081         sub("%{_defaultdocdir}", "%{_docdir}");
1082         sub("%{_bindir}/perl", "%{__perl}");
1083         sub("%{_bindir}/python", "%{__python}");
1084
1085         gsub(infodir, "%{_infodir}")
1086
1087         gsub(perl_sitearch, "%{perl_sitearch}")
1088         gsub(perl_archlib, "%{perl_archlib}")
1089         gsub(perl_privlib, "%{perl_privlib}")
1090         gsub(perl_vendorlib, "%{perl_vendorlib}")
1091         gsub(perl_vendorarch, "%{perl_vendorarch}")
1092         gsub(perl_sitelib, "%{perl_sitelib}")
1093         
1094         gsub(py_sitescriptdir, "%{py_sitescriptdir}")
1095         gsub(py_sitedir, "%{py_sitedir}")
1096         gsub(py_scriptdir, "%{py_scriptdir}")
1097         gsub("%{_libdir}/python2.4/site-packages", "%{py_sitedir}")
1098         gsub("%{python_sitelib}", "%{py_sitedir}")
1099
1100         gsub(ruby_archdir, "%{ruby_archdir}")
1101         gsub(ruby_ridir, "%{ruby_ridir}")
1102         gsub(ruby_rubylibdir, "%{ruby_rubylibdir}")
1103         gsub(ruby_sitearchdir, "%{ruby_sitearchdir}")
1104         gsub(ruby_sitelibdir, "%{ruby_sitelibdir}")
1105
1106         gsub("%{_datadir}/applications", "%{_desktopdir}")
1107         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}")
1108         gsub("%{_datadir}/java", "%{_javadir}")
1109
1110         gsub(libdir, "%{_libdir}")
1111         gsub(javadir, "%{_javadir}")
1112
1113         gsub(bindir, "%{_bindir}")
1114         gsub("%{prefix}/bin", "%{_bindir}")
1115         if (prefix"/bin" == bindir)
1116                 gsub("%{_prefix}/bin", "%{_bindir}")
1117
1118         for (c = 1; c <= NF; c++) {
1119                 if ($c ~ sbindir "/fix-info-dir")
1120                         continue;
1121                 if ($c ~ sbindir "/webapp")
1122                         continue;
1123                 if ($c ~ sbindir "/ldconfig")
1124                         continue;
1125                 if ($c ~ sbindir "/chsh")
1126                         continue;
1127                 if ($c ~ sbindir "/usermod")
1128                         continue;
1129                 if ($c ~ sbindir "/chkconfig")
1130                         continue;
1131                 if ($c ~ sbindir "/installzope(product|3package)")
1132                         continue;
1133                 gsub(sbindir, "%{_sbindir}", $c)
1134         }
1135
1136         gsub("%{prefix}/sbin", "%{_sbindir}")
1137         if (prefix"/sbin" == sbindir) {
1138                 gsub("%{_prefix}/sbin", "%{_sbindir}")
1139         }
1140
1141         for (c = 1; c <= NF; c++) {
1142                 if ($c ~ sysconfdir "/{?cron.")
1143                         continue;
1144                 if ($c ~ sysconfdir "/{?crontab.d")
1145                         continue;
1146                 if ($c ~ sysconfdir "/{?env.d")
1147                         continue;
1148                 if ($c ~ sysconfdir "/{?modprobe.(d|conf)")
1149                         continue;
1150                 if ($c ~ sysconfdir "/{?udev")
1151                         continue;
1152                 if ($c ~ sysconfdir "/{?hotplug")
1153                         continue;
1154                 if ($c ~ sysconfdir "/{?logrotate.d")
1155                         continue;
1156                 if ($c ~ sysconfdir "/{?pam.d")
1157                         continue;
1158                 if ($c ~ sysconfdir "/{?profile.d")
1159                         continue;
1160                 if ($c ~ sysconfdir "/{?rc.d")
1161                         continue;
1162                 if ($c ~ sysconfdir "/{?security")
1163                         continue;
1164                 if ($c ~ sysconfdir "/{?skel")
1165                         continue;
1166                 if ($c ~ sysconfdir "/{?sysconfig")
1167                         continue;
1168                 if ($c ~ sysconfdir "/{?shrc.d")
1169                         continue;
1170                 if ($c ~ sysconfdir "/{?certs")
1171                         continue;
1172                 if ($c ~ sysconfdir "/{?X11")
1173                         continue;
1174                 if ($c ~ sysconfdir "/{?ld.so.conf.d")
1175                         continue;
1176                 if ($c ~ sysconfdir "/{?rpm")
1177                         continue;
1178                 if ($c ~ sysconfdir "/{?bash_completion.d")
1179                         continue;
1180                 if ($c ~ sysconfdir "/{?samba")
1181                         continue;
1182                 if ($c ~ sysconfdir "/shells")
1183                         continue;
1184                 if ($c ~ sysconfdir "/ppp")
1185                         continue;
1186                 if ($c ~ sysconfdir "/dbus-1")
1187                         continue;
1188                 if ($c ~ sysconfdir "/tmpwatch")
1189                         continue;
1190                 gsub(sysconfdir, "%{_sysconfdir}", $c)
1191         }
1192
1193         gsub(docdir, "%{_docdir}")
1194
1195         gsub(kdedocdir, "%{_kdedocdir}")
1196
1197         gsub(gtkdocdir, "%{_gtkdocdir}")
1198         gsub("%{_docdir}/gtk-doc/html", "%{_gtkdocdir}")
1199
1200         gsub(php_pear_dir, "%{php_pear_dir}")
1201         gsub(php_data_dir, "%{php_data_dir}")
1202
1203         for (c = 1; c <= NF; c++) {
1204                 if ($c ~ datadir "/automake")
1205                         continue;
1206                 if ($c ~ datadir "/unsermake")
1207                         continue;
1208                 if ($c ~ datadir "/file/magic.mime")
1209                         continue;
1210                 gsub(datadir, "%{_datadir}", $c)
1211         }
1212
1213         gsub("%{prefix}/share", "%{_datadir}")
1214         if (prefix"/share" == datadir)
1215                 gsub("%{_prefix}/share", "%{_datadir}")
1216
1217         # CFLAGS="-I/usr/include/ncurses is usually correct.
1218         if (!/-I\/usr\/include/) {
1219                 gsub(includedir, "%{_includedir}")
1220         }
1221
1222         gsub("%{prefix}/include", "%{_includedir}")
1223         if (prefix"/include" == includedir) {
1224                 gsub("%{_prefix}/include", "%{_includedir}")
1225         }
1226
1227         gsub(mandir, "%{_mandir}")
1228         if ($0 !~ "%{_datadir}/manual") {
1229                 gsub("%{_datadir}/man", "%{_mandir}")
1230         }
1231         gsub("%{_prefix}/share/man", "%{_mandir}")
1232         gsub("%{prefix}/share/man", "%{_mandir}")
1233         gsub("%{prefix}/man", "%{_mandir}")
1234         gsub("%{_prefix}/man", "%{_mandir}")
1235
1236         gsub(infodir, "%{_infodir}")
1237         gsub("%{prefix}/info", "%{_infodir}")
1238         gsub("%{_prefix}/info", "%{_infodir}")
1239
1240         if (prefix !~ "/X11R6") {
1241                 gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
1242         }
1243
1244         gsub(examplesdir, "%{_examplesdir}")
1245
1246         if (prefix != "/") {
1247                 # leave --with-foo=/usr alone
1248                 if ($0 !~ "--with.*=.*" prefix) {
1249                         for (c = 1; c <= NF; c++) {
1250                                 if ($c ~ prefix "/sbin/fix-info-dir")
1251                                         continue;
1252                                 if ($c ~ prefix "/sbin/webapp")
1253                                         continue;
1254                                 if ($c ~ prefix "/sbin/chsh")
1255                                         continue;
1256                                 if ($c ~ prefix "/sbin/usermod")
1257                                         continue;
1258                                 if ($c ~ prefix "/sbin/installzope(product|3package)")
1259                                         continue;
1260                                 if ($c ~ prefix "/share/automake")
1261                                         continue;
1262                                 if ($c ~ prefix "/share/unsermake")
1263                                         continue;
1264                                 if ($c ~ prefix "/lib/sendmail")
1265                                         continue;
1266                                 if ($c ~ prefix "/lib/pkgconfig")
1267                                         continue;
1268
1269                                 # CFLAGS="-I/usr..." is usually correct.
1270                                 if (/-I\/usr/)
1271                                         continue;
1272                                 # same for LDFLAGS="-L/usr..."
1273                                 if (/-L\/usr/)
1274                                         continue;
1275
1276                                 gsub(prefix, "%{_prefix}", $c)
1277                         }
1278                 }
1279                 gsub("%{prefix}", "%{_prefix}")
1280         }
1281
1282         # replace back
1283         gsub("%{_includedir}/ncurses", "/usr/include/ncurses")
1284         gsub("%{_includedir}/freetype", "/usr/include/freetype")
1285
1286         gsub("%{PACKAGE_VERSION}", "%{version}")
1287         gsub("%{PACKAGE_NAME}", "%{name}")
1288
1289         gsub("^make$", "%{__make}")
1290         gsub("^make ", "%{__make} ")
1291         gsub("^gcc ", "%{__cc} ")
1292         gsub("^rm --interactive=never ", "%{__rm} ")
1293
1294         # mandrake specs
1295         gsub("^%make$", "%{__make}")
1296         gsub("^%make ", "%{__make} ")
1297         gsub("^%makeinstall_std", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1298         gsub("^%{makeinstall}", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1299         gsub("^%makeinstall", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1300         gsub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
1301         gsub("^%{__install}", "install")
1302         gsub("%optflags", "%{rpmcflags}")
1303         gsub("%{compat_perl_vendorarch}", "%{perl_vendorarch}")
1304
1305         gsub("^%{__make} install DESTDIR=\$RPM_BUILD_ROOT", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1306         gsub("^fix-info-dir$", "[ ! -x /usr/sbin/fix-info-dir ] || /usr/sbin/fix-info-dir -c %{_infodir} >/dev/null 2>\\&1")
1307         $0 = fixedsub("%buildroot", "$RPM_BUILD_ROOT", $0)
1308         $0 = fixedsub("%{buildroot}", "$RPM_BUILD_ROOT", $0)
1309         $0 = fixedsub("CXXFLAGS=%{rpmcflags} %configure", "CXXFLAGS=%{rpmcflags}\n%configure", $0);
1310         $0 = fixedsub("%__install", "install", $0);
1311
1312         # split configure line to multiple lines
1313         if (/%configure / && !/\\$/) {
1314                 $0 = format_configure($0);
1315         }
1316
1317         gsub("%_bindir", "%{_bindir}")
1318         gsub("%_datadir", "%{_datadir}")
1319         gsub("%_iconsdir", "%{_iconsdir}")
1320         gsub("%_sbindir", "%{_sbindir}")
1321         gsub("%_mandir", "%{_mandir}")
1322         gsub("%name", "%{name}")
1323         gsub(/%__rm/, "rm");
1324         gsub(/%__mkdir_p/, "install -d");
1325         gsub(/%__cp/, "cp");
1326         gsub(/%__ln_s/, "ln -s");
1327         gsub(/%__sed/, "%{__sed}");
1328         gsub(/%__cat/, "cat");
1329         gsub(/%__chmod/, "chmod");
1330
1331         gsub("/usr/src/linux", "%{_kernelsrcdir}")
1332         gsub("%{_prefix}/src/linux", "%{_kernelsrcdir}")
1333
1334         if (/^ant / || /^%{ant}/) {
1335                 sub(/^ant/, "%ant")
1336                 sub(/^%{ant}/, "%ant")
1337                 add_br("BuildRequires:  jpackage-utils");
1338                 add_br("BuildRequires:  rpmbuild(macros) >= 1.300");
1339         }
1340
1341         $0 = fixedsub("%(%{__cc} -dumpversion)", "%{cc_version}", $0);
1342         $0 = fixedsub("%(%{__cxx} -dumpversion)", "%{cxx_version}", $0);
1343 }
1344
1345 function format_configure(line,         n, a, s) {
1346         n = split(line, a, / /);
1347         s = a[1] " \\\n";
1348         for (i = 2; i <= n; i++) {
1349                 s = s "\t" a[i] " \\\n"
1350         }
1351         return s
1352 }
1353
1354
1355 # insertion sort of A[1..n]
1356 # copied from mawk manual
1357 function isort(A,n,             i,j,hold) {
1358         for (i = 2; i <= n; i++) {
1359                 hold = A[j = i]
1360                 while (A[j-1] > hold) {
1361                         j-- ; A[j+1] = A[j]
1362                 }
1363                 A[j] = hold
1364         }
1365         # sentinel A[0] = "" will be created if needed
1366 }
1367
1368
1369 function use_files_macros(      i, n, t, a, l)
1370 {
1371         use_macros()
1372
1373         # skip comments
1374         if (/^#/) {
1375                 return;
1376         }
1377
1378         sub("^%doc %{_mandir}", "%{_mandir}")
1379
1380         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
1381         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
1382
1383         # uid/gid nobody is not valid in %files
1384         if (/%attr([^)]*nobody[^)]*)/ && !/FIXME/) {
1385                 $0 = $0 " # FIXME nobody user/group can't own files! -adapter.awk"
1386         }
1387
1388         # s[gu]id programs with globs are evil
1389         if (/%attr\([246]...,.*\*/ && !/FIXME/) {
1390                 $0 = $0 " # FIXME no globs for suid/sgid files"
1391         }
1392
1393         # replace back
1394         gsub("%{_sysconfdir}/cron\.d", "/etc/cron.d")
1395         gsub("%{_sysconfdir}/crontab\.d", "/etc/crontab.d")
1396         gsub("%{_sysconfdir}/logrotate\.d", "/etc/logrotate.d")
1397         gsub("%{_sysconfdir}/pam\.d", "/etc/pam.d")
1398         gsub("%{_sysconfdir}/profile\.d", "/etc/profile.d")
1399         gsub("%{_sysconfdir}/rc\.d", "/etc/rc.d")
1400         gsub("%{_sysconfdir}/security", "/etc/security")
1401         gsub("%{_sysconfdir}/skel", "/etc/skel")
1402         gsub("%{_sysconfdir}/sysconfig", "/etc/sysconfig")
1403         gsub("%{_sysconfdir}/certs", "/etc/certs")
1404         gsub("%{_sysconfdir}/init.d", "/etc/init.d")
1405         gsub("%{_sysconfdir}/dbus-1", "/etc/dbus-1")
1406         gsub("%{_sysconfdir}/pki", "/etc/pki")
1407         gsub("%{_sysconfdir}/tmpwatch", "/etc/tmpwatch")
1408
1409         # /etc/init.d -> /etc/rc.d/init.d
1410         if (!/^\/etc\/init\.d$/) {
1411                  gsub("/etc/init.d", "/etc/rc.d/init.d")
1412         }
1413
1414         if (/\/etc\/rc\.d\/init\.d\// && !/functions/) {
1415                 if (!/%attr.*\/etc\/rc\.d\/init\.d/) {
1416                         $0 = "%attr(754,root,root) " $0
1417                 }
1418                 if (/^%attr.*\/etc\/rc\.d\/init\.d/ && !/^%attr\(754 *,/) {
1419                         gsub("^%attr\\(... *,", "%attr(754,");
1420                 }
1421         }
1422
1423         if (/lib.+\.so/ && !/\.so$/ && !/^%attr.*/ && !/%exclude/) {
1424                 $0 = "%attr(755,root,root) " $0
1425         }
1426
1427         if (/%{perl_vendorarch}.*\.so$/ && !/^%attr.*/) {
1428                 $0 = "%attr(755,root,root) " $0
1429         }
1430
1431         # /etc/sysconfig files
1432         # %attr(640,root,root) %config(noreplace) %verify(not size mtime md5) /etc/sysconfig/*
1433         # attr not required, allow default 644 attr
1434         if (!/network-scripts/ && !/%dir/ && !/\.d$/ && !/functions/ && !/\/etc\/sysconfig\/wmstyle/) {
1435                 if (/\/etc\/sysconfig\// && /%config/ && !/%config\(noreplace/) {
1436                         gsub("%config", "%config(noreplace)")
1437                 }
1438
1439                 if (/\/etc\/sysconfig\// && !/%config\(noreplace/) {
1440                         $NF = "%config(noreplace) " $NF
1441                 }
1442
1443                 if (/\/etc\/sysconfig\// && /%attr\(755/) {
1444                         gsub("^%attr\\(... *,", "%attr(640,");
1445                 }
1446
1447                 if (/\/etc\/sysconfig\// && !/%verify/) {
1448                         gsub("/etc/sysconfig", "%verify(not size mtime md5) /etc/sysconfig");
1449                 }
1450         }
1451
1452         # kill leading zeros
1453         if (/%attr\(0[1-9]/) {
1454                 gsub("%attr\\(0", "%attr(")
1455         }
1456
1457         # kill leading whitespace
1458         gsub(/^ +/, "");
1459
1460         # kill default attrs
1461         gsub(/%dir %attr\(755,root,root\)/, "%dir");
1462         gsub(/%attr\(755,root,root\) %dir/, "%dir");
1463         if (!/%dir/) {
1464                 gsub(/%attr\(644,root,root\) /, "");
1465         }
1466
1467         # sort %verify attrs
1468         if (match($0, /%verify\(not([^)]+)\)/)) {
1469                 t = substr($0, RSTART, RLENGTH)
1470                 # kill commas: %verify(not,md5,size,mtime)
1471                 gsub(/,/, " ", t);
1472
1473                 gsub(/^%verify\(not |\)$/, "", t)
1474                 n = split(t, a, / /)
1475                 isort(a, n)
1476
1477                 s = "%verify(not"
1478                 for (i = 1 ; i <= n; i++) {
1479                         s = s " " a[i]
1480                 }
1481                 s = s ")"
1482
1483                 gsub(/%verify\(not[^)]+\)/, s)
1484         }
1485
1486         if (/%{_mandir}/) {
1487                 gsub("\.gz$", "*")
1488         }
1489
1490         # locale dir and no %lang -> bad
1491         if (/%{_datadir}\/locale\/.*\// && !/%(dir|lang)/) {
1492                 $(NF + 1) = "# FIXME consider using %find_lang"
1493         }
1494
1495         # python egg-infos
1496         if (match($0, "^%{py_site(script)?dir}/.+-py"py_ver".egg-info$")) {
1497                 l = index($0, "/");
1498                 t = substr($0, 0, l - 1);
1499                 s = substr($0, l + 1, RLENGTH - l - length("-py"py_ver".egg-info"));
1500                 if (match(s, "[^-]+$")) {
1501                         s = substr(s, 0, RSTART - 2);
1502                         print "%if \"%{py_ver}\" > \"2.4\""
1503                         gsub(t "/.+.egg-info", t "/" s "-*.egg-info");
1504                         print
1505                         print "%endif"
1506                         next
1507                 }
1508         }
1509
1510         # atrpms
1511         $0 = fixedsub("%{perl_man1dir}", "%{_mandir}/man1", $0);
1512         $0 = fixedsub("%{perl_man3dir}", "%{_mandir}/man3", $0);
1513         $0 = fixedsub("%{perl_bin}", "%{_bindir}", $0);
1514
1515         gsub(libdir "/pkgconfig", "%{_pkgconfigdir}");
1516         gsub("%{_libdir}/pkgconfig", "%{_pkgconfigdir}");
1517         gsub("%{_prefix}/lib/pkgconfig", "%{_pkgconfigdir}");
1518
1519         gsub("%{_datadir}/applications", "%{_desktopdir}");
1520         gsub("%{_datadir}/icons", "%{_iconsdir}");
1521         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}");
1522         gsub("%{_datadir}/pear", "%{php_pear_dir}");
1523         gsub("%{_datadir}/php", "%{php_data_dir}");
1524 }
1525
1526 function use_script_macros()
1527 {
1528         if (gsub("/sbin/service", "%service")) {
1529                 sub(" >/dev/null 2>&1 \|\|:", "");
1530                 sub(" 2> /dev/null \|\| :", "");
1531         }
1532 }
1533
1534 function fill(ch, n, i) {
1535         for (i = 0; i < n; i++)
1536                 printf("%c", ch)
1537 }
1538
1539 function format_flush(line, indent, newline, word, first_word) {
1540         first_word = 1
1541         if (format_indent == -1)
1542                 newline = ""
1543         else
1544                 newline = fill(" ", format_indent) "- "
1545
1546         while (match(line, /[^\t ]+/)) {
1547                 word = substr(line, RSTART, RLENGTH)
1548                 if (length(newline) + length(word) + 1 > tw) {
1549                         print newline
1550
1551                         if (format_indent == -1)
1552                                 newline = ""
1553                         else
1554                                 newline = fill(" ", format_indent + 2)
1555                         first_word = 1
1556                 }
1557
1558                 if (first_word) {
1559                         newline = newline word
1560                         first_word = 0
1561                 } else
1562                         newline = newline " " word
1563
1564                 line = substr(line, RSTART + RLENGTH)
1565         }
1566         if (newline ~ /[^\t ]/) {
1567                 print newline
1568         }
1569 }
1570
1571 function cflags(var)
1572 {
1573         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
1574                 removed[var] = 1
1575                 return 0
1576         }
1577
1578         if (!/!\?debug/)
1579                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
1580         return 1
1581 }
1582
1583 function unify_url(url)
1584 {
1585
1586         # sourceforge urls
1587         sub("[?&]big_mirror=.*$", "", url);
1588         sub("[?&]modtime=.*$", "", url);
1589         sub("[?]use_mirror=.*$", "", url);
1590         sub("[?]download$", "", url);
1591
1592         sub("^http://prdownloads\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1593         sub("^http://download\.sf\.net/", "http://dl.sourceforge.net/", url)
1594         sub("^http://download\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1595         sub("^http://downloads\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1596
1597         sub("^http://.*\.dl\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1598         sub("^http://dl\.sourceforge\.net/sourceforge/", "http://dl.sourceforge.net/", url)
1599         sub("^http://dl\.sf\.net/", "http://dl.sourceforge.net/", url)
1600
1601         sub("^ftp://ftp\.gnome\.org/", "http://ftp.gnome.org/", url)
1602         sub("^http://ftp\.gnome\.org/pub/gnome/", "http://ftp.gnome.org/pub/GNOME/", url)
1603
1604         # apache urls
1605         sub("^http://apache.zone-h.org/", "http://www.apache.org/dist/", url)
1606
1607         # gnu.org
1608         sub("^ftp://ftp.gnu.org/", "http://ftp.gnu.org/", url)
1609         sub("^http://ftp.gnu.org/pub/gnu/", "http://ftp.gnu.org/gnu/", url)
1610
1611         return url
1612 }
1613
1614 function demacroize(str)
1615 {
1616         if (mod_name) {
1617                 sub("%{mod_name}", mod_name, str);
1618         }
1619         if (name) {
1620                 sub("%{name}", name, str);
1621         }
1622         if (version) {
1623                 sub("%{version}", version, str);
1624         }
1625         if (_beta) {
1626                 sub("%{_beta}", _beta, str);
1627         }
1628         if (_rc) {
1629                 sub("%{_rc}", _rc, str);
1630         }
1631         if (_pre) {
1632                 sub("%{_pre}", _pre, str);
1633         }
1634         if (_snap) {
1635                 sub("%{_snap}", _snap, str);
1636         }
1637         if (subver) {
1638                 sub("%{subver}", subver, str);
1639         }
1640         return str;
1641 }
1642
1643 function kill_preamble_macros()
1644 {
1645         if ($1 ~ /^Obsoletes:/) {
1646                 # NB! assigning $2 a value breaks tabbing
1647                 $2 = demacroize($2);
1648         }
1649         if ($1 ~ /^URL:/) {
1650                 # NB! assigning $2 a value breaks tabbing
1651                 $2 = demacroize($2);
1652                 $2 = unify_url($2)
1653         }
1654 }
1655
1656 function get_epoch(pkg, ver,    epoch)
1657 {
1658         return
1659 # should parse the BR lines more adequately:
1660 #       freetype = 2.0.0 -> correct
1661 #       freetype = 2.1.9 -> with epoch 1, as epoch 1 was added in 2.1.7
1662
1663         shell = "grep -o '^" pkg ":[^:]\+' ../PLD-doc/BuildRequires.txt | awk '{print $NF}'";
1664         shell | getline epoch;
1665         return epoch;
1666 }
1667
1668 function format_requires(tag, value,    n, p, i, deps, ndeps) {
1669         # skip any formatting for commented out items or some weird macros
1670         if (/^#/ || /%\(/) {
1671                 return tag "\t" value
1672         }
1673         n = split(value, p, / *,? */);
1674         for (i = 1; i <= n; i++) {
1675                 if (p[i+1] ~ /[<=>]/) {
1676                         # add epoch if the version doesn't have it but BuildRequires.txt has
1677                         if (p[i] ~ /^[a-z]/ && p[i+2] !~ /^[0-9]+:/) {
1678                                 epoch = get_epoch(p[i], p[i+2])
1679                                 if (epoch) {
1680                                         p[i+2] = epoch ":" p[i+2];
1681                                 }
1682                         }
1683                         deps[ndeps++] = p[i] " " p[i+1] " " p[i+2];
1684                         i += 2;
1685                 } else {
1686                         deps[ndeps++] = p[i];
1687                 }
1688         }
1689         s = ""
1690         for (i in deps) {
1691                 s = s sprintf("%s\t%s\n", tag, deps[i]);
1692         }
1693         return substr(s, 1, length(s)-1);
1694 }
1695
1696 function use_tabs()
1697 {
1698         # reverse vim: ts=4 sw=4 et
1699         gsub(/    /, "\t");
1700 }
1701
1702 function add_br(br)
1703 {
1704         BR[BR_count++] = br
1705 }
1706
1707 function replace_requires()
1708 {
1709
1710         # jpackages
1711         sub(/^java-devel$/, "jdk", $2);
1712         sub(/^log4j$/, "logging-log4j", $2);
1713         sub(/^jakarta-log4j$/, "logging-log4j", $2);
1714         sub(/^oro$/, "jakarta-oro", $2);
1715         sub(/^jakarta-ant$/, "ant", $2);
1716         sub(/^xerces-j2$/, "xerces-j", $2);
1717         sub(/^ldapjdk$/, "ldapsdk", $2);
1718         sub(/^saxon-scripts$/, "saxon", $2);
1719         sub(/^xalan-j2$/, "xalan-j", $2);
1720         sub(/^xerces-j2$/, "xerces-j", $2);
1721         sub(/^gnu-regexp$/, "gnu.regexp", $2);
1722
1723         # redhat virtual
1724         sub(/^tftp-server$/, "tftpdaemon", $2);
1725
1726         sub(/^gcc-c\+\+$/, "libstdc++-devel", $2);
1727         sub(/^chkconfig$/, "/sbin/chkconfig", $2);
1728
1729         # fedora
1730         sub(/^iscsi-initiator-utils$/, "open-iscsi", $2);
1731
1732         replace_php_virtual_deps()
1733 }
1734
1735 # php virtual deps as discussed in devel-en
1736 function replace_php_virtual_deps()
1737 {
1738         pkg = $2
1739 #       if (pkg == "php-program") {
1740 #               $0 = $1 "\t/usr/bin/php"
1741 #               return
1742 #       }
1743
1744 #       if (pkg ~ /^php-[a-z]/ && pkg !~ /^php-(pear|common|cli|devel|fcgi|cgi|dirs|program|pecl-)/) {
1745 #               sub(/^php-/, "php(", pkg);
1746 #               sub(/$/, ") # verify this correctness -- it may be wanted to use specific not virtual dep", pkg);
1747 #               $2 = pkg
1748 #       }
1749
1750         if (pkg ~/^php$/) {
1751                 $2 = "webserver(php)";
1752                 if ($4 ~ /^[0-9]:/) {
1753                         $4 = substr($4, 3);
1754                 }
1755         }
1756
1757         if (pkg ~/^php4$/) {
1758                 $2 = "webserver(php)";
1759                 if ($4 ~ /^[0-9]:/) {
1760                         $4 = substr($4, 3);
1761                 }
1762         }
1763 }
1764
1765 function replace_groupnames(group)
1766 {
1767         sub(/^Amusements\/Games\/Strategy\/Real Time/, "X11/Applications/Games/Strategy", group)
1768         sub(/^Application\/Multimedia$/, "Applications/Multimedia", group)
1769         sub(/^Applications\/Compilers$/, "Development/Languages", group)
1770         sub(/^Applications\/Daemons$/, "Daemons", group)
1771         sub(/^Applications\/Internet$/, "Applications/Networking", group)
1772         sub(/^Applications\/Internet\/Peer to Peer/, "Applications/Networking", group)
1773         sub(/^Applications\/Productivity$/, "X11/Applications", group)
1774         sub(/^Database$/, "Applications/Databases", group)
1775         sub(/^Development\/Code Generators$/, "Development", group)
1776         sub(/^Development\/Docs$/, "Documentation", group)
1777         sub(/^Development\/Documentation$/, "Documentation", group)
1778         sub(/^Development\/Java/, "Development/Languages/Java", group)
1779         sub(/^Development\/Libraries\/C and C\+\+$/, "Development/Libraries", group)
1780         sub(/^Development\/Libraries\/Java$/, "Development/Languages/Java", group)
1781         sub(/^Development\/Other/,"Development", group)
1782         sub(/^Development\/Testing$/, "Development", group)
1783         sub(/^Emulators$/, "Applications/Emulators", group)
1784         sub(/^Games/,"Applications/Games", group)
1785         sub(/^Library\/Development$/, "Development/Libraries", group)
1786         sub(/^Networking\/Deamons$/, "Networking/Daemons", group)
1787         sub(/^Shells/,"Applications/Shells", group)
1788         sub(/^System Environment\/Base$/, "Base", group)
1789         sub(/^System Environment\/Daemons$/, "Daemons", group)
1790         sub(/^System Environment\/Kernel$/, "Base/Kernel", group)
1791         sub(/^System Environment\/Libraries$/, "Libraries", group)
1792         sub(/^System$/, "Base", group)
1793         sub(/^System\/Base$/, "Base", group)
1794         sub(/^System\/Libraries$/, "Libraries", group)
1795         sub(/^System\/Servers$/, "Daemons", group)
1796         sub(/^Text Processing\/Markup\/HTML$/, "Applications/Text", group)
1797         sub(/^Text Processing\/Markup\/XML$/, "Applications/Text", group)
1798         sub(/^Utilities\//,"Applications/", group)
1799         sub(/^Web\/Database$/, "Applications/WWW", group)
1800         sub(/^X11\/GNOME/,"X11/Applications", group)
1801         sub(/^X11\/GNOME\/Applications/,"X11/Applications", group)
1802         sub(/^X11\/GNOME\/Development\/Libraries/,"X11/Development/Libraries", group)
1803         sub(/^X11\/Games/,"X11/Applications/Games", group)
1804         sub(/^X11\/Games\/Strategy/,"X11/Applications/Games/Strategy", group)
1805         sub(/^X11\/Library/,"X11/Libraries", group)
1806         sub(/^X11\/Utilities/,"X11/Applications", group)
1807         sub(/^X11\/XFree86/, "X11", group)
1808         sub(/^X11\/Xserver$/, "X11/Servers", group)
1809         sub(/^Development\/C$/, "Development/Libraries", group)
1810         sub(/^Development\/Python$/, "Development/Languages/Python", group)
1811         sub(/^System\/Kernel and hardware$/, "Base/Kernel", group)
1812         sub(/^Application\/System$/, "Applications/System", group)
1813
1814         return group;
1815 }
1816
1817 # vim:ts=4:sw=4
This page took 0.222895 seconds and 4 git commands to generate.