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