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