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