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