]> git.pld-linux.org Git - packages/adapter.git/blob - adapter.awk
adapter: replace fedoraish %{_unitdir} with %{systemdunitdir}
[packages/adapter.git] / adapter.awk
1 #!/usr/bin/gawk -f
2 #
3 # Adapter adapts .spec files for PLD Linux.
4 #
5 # Copyright (C) 1999-2013 PLD-Team <feedback@pld-linux.org>
6 # Authors:
7 #       Michał Kuratczyk <kura@pld.org.pl>
8 #       Sebastian Zagrodzki <s.zagrodzki@mimuw.edu.pl>
9 #       Tomasz Kłoczko <kloczek@rudy.mif.pg.gda.pl>
10 #       Artur Frysiak <wiget@pld-linux.org>
11 #       Michal Kochanowicz <mkochano@pld.org.pl>
12 #       Jakub Bogusz <qboosh@pld-linux.org>
13 #       Elan Ruusamäe <glen@pld-linux.org>
14 #
15 # See cvs log adapter{,.awk} for list of contributors
16 #
17 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
18
19 # TODO
20 # - really long sourceX make preamble sorting totally fcked up (try snake.spec r1.1)
21 # - parse ../PLD-doc/BuildRequires.txt and setup proper BR epoches?
22 # - add "-nc" option to skip CVS interaction
23 # - sort Summary(XX)
24 # - sort Requires, BuildRequires
25 # - check if %description (lang=C) contains 8bit
26 # - desc wrapping is totally fucked up on global.spec,1.25, dosemu.spec,1.115-
27 # - it should change: /%source([0-9]+)/i to %{SOURCE\1}
28 # - extra quote on LDFLAGS line: https://bugs.launchpad.net/pld-linux/+bug/385836
29 # - %{with_foo:%attr()...} gets converted to %attr() %{with_foo:...} [vlc.spec]
30 # - 'R: foo ' (with traliling space) gets coverted to "R: foo\nR: " [vlc.spec @ 1.199 ]
31
32 BEGIN {
33         RPM_SECTIONS = "package|build|changelog|clean|description|install|post|posttrans|postun|pre|prep|pretrans|preun|triggerin|triggerpostun|triggerun|verifyscript|check"
34         SECTIONS = "^%(" RPM_SECTIONS ")"
35
36         rev = "1.514"
37         VERSION = "0.36/" rev
38
39         PREAMBLE_TAGS = "(R|BR|Summary|Name|Version|Release|Epoch|License|Group|URL|BuildArch|BuildRoot|Obsoletes|Conflicts|Provides|ExclusiveArch|ExcludeArch|Pre[Rr]eq|(Build)?Requires|Suggests|Auto(Req|Prov))"
40
41         usedigest = 0   # Enable to switch to rpm 4.4.6+ md5 digests
42
43         preamble = 1    # Is it part of preamble? Default - yes
44         bod = 0                 # Beginning of %description
45         tw = 70                 # Descriptions width
46
47         b_idx = 0               # index of BR/R arrays
48         BR_count = 0    # number of additional BuildRequires
49
50         # If variable removed, then 1 (for removing it from export)
51         removed["LDFLAGS"] = 0
52         removed["CFLAGS"] = 0
53         removed["CXXFLAGS"] = 0
54
55         # If 1, we are inside of comment block (started with /^#%/)
56         comment_block = 0
57
58         import_rpm_macros()
59
60         packages_dir = topdir
61         groups_file = packages_dir "/../rpm-build-tools/rpm.groups"
62
63         system("[ -d ../PLD-doc ] && cd ../PLD-doc && ([ -f BuildRequires.txt ] || cvs up BuildRequires.txt >/dev/null)");
64 }
65
66 # If the latest line matched /%files/
67 defattr == 1 {
68         if (ENVIRON["SKIP_DEFATTR"] != 1) {
69                 if ($0 !~ /defattr/) {  # If no %defattr
70                         print "%defattr(644,root,root,755)"     # Add it
71                 } else {
72                         $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
73                 }
74         }
75         defattr = 0
76 }
77
78 # call mktemp(1) and return the value
79 function mktemp(template, tmp) {
80    "mktemp " template | getline tmp
81    return tmp
82 }
83
84 function b_makekey(a, b,        s) {
85         s = a "" b;
86         # kill bcond
87         gsub(/[#%]+{[!?]+[_a-zA-Z0-9]+:/, "", s);
88
89         # kill commented out items
90         gsub(/^#[ \t]*/, "", s);
91
92         # force order
93         gsub(/^Summary\(/, "11Summary(", s);
94         gsub(/^Summary/, "10Summary", s);
95
96         gsub(/^Name/, "2Name", s);
97         gsub(/^Version/, "3Version", s);
98         gsub(/^Release/, "4Release", s);
99         gsub(/^Epoch/, "5Epoch", s);
100         gsub(/^License/, "5License", s);
101         gsub(/^Group/, "6Group", s);
102         gsub(/^URL/, "7URL", s);
103
104         gsub(/^BuildRequires/, "B1BuildRequires", s);
105         gsub(/^BuildConflicts/, "B2BuildConflicts", s);
106
107         gsub(/^Suggests/, "X1Suggests", s);
108         gsub(/^Provides/, "X2Provides", s);
109         gsub(/^Obsoletes/, "X3Obsoletes", s);
110         gsub(/^Conflicts/, "X4Conflicts", s);
111         gsub(/^BuildArch/, "X5BuildArch", s);
112         gsub(/^ExclusiveArch/, "X6ExclusiveArch", s);
113         gsub(/^ExcludeArch/, "X7ExcludeArch", s);
114         gsub(/^BuildRoot/, "X9BuildRoot", s);
115
116         gsub(/^AutoProv/, "Xx1AutoProv", s);
117         gsub(/^AutoReq/, "Xx2AutoReq", s);
118
119 #       printf("%s -> %s\n", a""b, s);
120         return s;
121 }
122
123 # Comments
124 /^#/ && (description == 0) {
125         if (/This file does not like to be adapterized!/) {
126                 print                   # print this message
127                 while (getline)         # print the rest of spec as it is
128                         print
129                 do_not_touch_anything = 1 # do not touch anything in END()
130                 exit(rc = 0)
131         }
132
133         # Generally, comments are printed without touching
134         sub(/[ \t]+$/, "")
135
136         if (/#[ \t]*Source.*md5/) {
137                 if (usedigest == 1) {
138                         sub(/^#[ \t]*Source/, "BuildRequires:\tdigest(%SOURCE", $0)
139                         sub(/-md5[ \t]*:[ \t]*/, ") = ", $0)
140                 }
141                 print $0
142                 next
143         }
144 }
145
146 /^%define/ {
147         # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
148         if ($2 == "_applnkdir") {
149                 next
150         }
151
152         # FIXME: this section will likely never match after cvs->git migration
153         if ($2 == "date") {
154                 if (did_files == 0) {
155                         print "%files"
156                         print ""
157                         did_files = 1
158                 }
159         }
160
161         # Do not add %define of _prefix if it already is.
162         if ($2 ~ /^_prefix/) {
163                 sub("^"prefix, $3, bindir)
164                 sub("^"prefix, $3, sbindir)
165                 sub("^"prefix, $3, libdir)
166                 sub("^"prefix, $3, datadir)
167                 sub("^"prefix, $3, includedir)
168                 prefix = $3
169         }
170
171         if ($2 ~ /_bindir/ && !/_sbindir/)
172                 bindir = $3
173         if ($2 ~ /_sbindir/)
174                 sbindir = $3
175         if ($2 ~ /_libdir/) {
176                 if ($3 ~ /^%\(/) {
177                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
178                         libdir = "%%%%%%%%%%%%%%"
179                 } else {
180                         libdir = $3
181                 }
182         }
183         if ($2 ~ /_sysconfdir/) {
184                 if ($3 ~ /^%\(/) {
185                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
186                         sysconfdir = "%%%%%%%%%%%%%%"
187                 } else {
188                         sysconfdir = $3
189                 }
190         }
191         if ($2 ~ /_datadir/) {
192                 if ($3 ~ /^%\(/) {
193                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
194                         datadir = "%%%%%%%%%%%%%%"
195                 } else {
196                         datadir = $3
197                 }
198         }
199         if ($2 ~ /_includedir/)
200                 includedir = $3
201         if ($2 ~ /_mandir/)
202                 mandir = $3
203         if ($2 ~ /_infodir/)
204                 infodir = $3
205         if ($2 ~ /_docdir/)
206                 docdir = $3
207
208         # version related macros
209         if ($2 ~ /^_beta$/)
210                 _beta = $3
211         if ($2 ~ /^_rc$/)
212                 _rc = $3
213         if ($2 ~ /^_pre$/)
214                 _pre = $3
215         if ($2 ~ /^_snap$/)
216                 _snap = $3
217         if ($2 ~ /^subver$/)
218                 subver = $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         if ($2 ~ /^_?pearname$/)
231                 pearname = $3
232         if ($2 ~ /^_class$/)
233                 pear_class = $3
234         if ($2 ~ /^_subclass$/)
235                 pear_subclass = $3
236
237         # kill the _class and _subclass pear macros
238         if ($2 == "_pearname" || $2 == "pearname") {
239                 if (pear_class) {
240                         gsub("%{_class}", pear_class, $3);
241                 }
242                 if (pear_subclass) {
243                         gsub("%{_subclass}", pear_subclass, $3);
244                 }
245         }
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                 if (subver) {
337                         $0 = fixedsub(subver, "%{subver}", $0);
338                 }
339         }
340
341         if (/^%setup/ && /-n %{name}-%{version}( |$)/) {
342                 $0 = fixedsub(" -n %{name}-%{version}", "", $0)
343         }
344         sub("^%patch ", "%patch0 ");
345
346         # fedora extras
347         if (/^%apply/) {
348                 sub("^%apply -n", "%patch");
349         }
350
351         # invalid in %prep
352         sub("^rm -rf \$RPM_BUILD_ROOT.*", "");
353 }
354
355 ##########
356 # %build #
357 ##########
358 /^%build/, (!/^%build/ && $0 ~ SECTIONS) {
359         preamble = 0
360
361         if (did_prep == 0) {
362                 print "%prep"
363                 print ""
364                 did_prep = 1
365         }
366
367         use_macros()
368         use_tabs()
369
370         if (/^automake$/)
371                 sub(/$/, " -a -c")
372
373         if (/LDFLAGS/) {
374                 if (/LDFLAGS="-s"/) {
375                         removed["LDFLAGS"] = 1
376                         next
377                 } else {
378                         split($0, tmp, "LDFLAGS=")
379                         count = split(tmp[2], flags, "\"")
380                         if (flags[1] != "" && flags[1] !~ "!?debug") {
381                                 sub(/-s[" ]?/, "%{rpmldflags} ", flags[1])
382                                 $0 = tmp[1] line[1] "LDFLAGS=" flags[1] "\""
383                                 for (i = 2; i < count; i++)
384                                         $0 = $0 flags[i] "\""
385                         }
386                 }
387         }
388
389         if (/CFLAGS=/)
390                 if (cflags("CFLAGS") == 0)
391                         next
392
393         if (/CXXFLAGS=/)
394                 if (cflags("CXXFLAGS") == 0)
395                         next
396
397         if (/^export /) {
398                 if (removed["LDFLAGS"])
399                         sub(" LDFLAGS", "")
400                 if (removed["CFLAGS"])
401                         sub(" CFLAGS", "")
402                 if (removed["CXXFLAGS"])
403                         sub(" CXXFLAGS", "")
404                 # Is there still something?
405                 if (/^export[ ]*$/)
406                         next
407         }
408
409         # quote CC
410         if (/CC=%{__cc} /) {
411                 sub("CC=%{__cc}", "CC=\"%{__cc}\"")
412         }
413
414         # use PLD Linux macros
415         $0 = fixedsub("glib-gettextize --copy --force","%{__glib_gettextize}", $0);
416         $0 = fixedsub("intltoolize --copy --force", "%{__intltoolize}", $0);
417         $0 = fixedsub("automake --add-missing --copy", "%{__automake}", $0);
418         $0 = fixedsub("automake -a --foreign --copy", "%{__automake}", $0);
419         $0 = fixedsub("automake -a -c --foreign", "%{__automake}", $0);
420         $0 = fixedsub("automake -a -c", "%{__automake}", $0);
421         $0 = fixedsub("libtoolize --force --automake --copy", "%{__libtoolize}", $0);
422         $0 = fixedsub("libtoolize -c -f --automake", "%{__libtoolize}", $0);
423
424         sub(/^aclocal$/, "%{__aclocal}");
425         sub(/^autoheader$/, "%{__autoheader}");
426         sub(/^autoconf$/, "%{__autoconf}");
427         sub(/^automake$/, "%{__automake}");
428         sub(/^libtoolize$/, "%{__libtoolize}");
429
430         # atrpms
431         $0 = fixedsub("%perl_configure", "%{__perl} Makefile.PL \\\n\tINSTALLDIRS=vendor", $0);
432         $0 = fixedsub("%perl_makecheck", "%{?with_tests:%{__make} test}", $0);
433
434         # alt linux
435         $0 = fixedsub("%make_build", "%{__make}", $0);
436 }
437
438 ##########
439 # %clean #
440 ##########
441 /^%clean/, (!/^%clean/ && $0 ~ SECTIONS) {
442         did_clean = 1
443
444         use_macros()
445 }
446
447 ############
448 # %install #
449 ############
450 /^%install/, (!/^%install/ && $0 ~ SECTIONS) {
451
452         preamble = 0
453
454         # foreign rpms
455         sub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
456         sub("%buildroot", "$RPM_BUILD_ROOT");
457         sub("%{buildroot}", "$RPM_BUILD_ROOT");
458
459         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+(\${?RPM_BUILD_ROOT}?|%{?buildroot}?)/ && did_rmroot==0) {
460                 did_rmroot=1
461                 print "rm -rf $RPM_BUILD_ROOT"
462                 next
463         }
464
465         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
466                 print "rm -rf $RPM_BUILD_ROOT"
467                 did_rmroot=1
468         }
469
470         if (tmpdir) {
471                 buildroot = tmpdir "/" name "-" version "-root-" ENVIRON["USER"]
472                 gsub(buildroot, "$RPM_BUILD_ROOT")
473         }
474
475         if (!/%{_lib}/) {
476                 sub("\$RPM_BUILD_ROOT/%", "$RPM_BUILD_ROOT%")
477         }
478
479         use_macros()
480
481         # 'install -d' instead 'mkdir -p'
482         if (/mkdir -p/)
483                 sub(/mkdir -p/, "install -d")
484
485         # cp -a already implies cp -r
486         sub(/^cp -ar/, "cp -a")
487
488         # No '-u root' or '-g root' for 'install'
489         if (/^install/ && /-[ug][ \t]*root/)
490                 gsub(/-[ug][ \t]*root /, "")
491
492         if (/^install/ && /-m[ \t]*[0-9]+/)
493                 gsub(/-m[ \t]*[0-9]+ /, "")
494
495         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
496         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
497                 next
498
499         # No lines contain 'chmod' if it sets the modes to '644'
500         if ($1 ~ /chmod/ && $2 ~ /644/)
501                 next
502
503         # atrpms
504         $0 = fixedsub("%perl_makeinstall", "%{__make} pure_install \\\n\tDESTDIR=$RPM_BUILD_ROOT", $0);
505
506         # alt linux
507         $0 = fixedsub("%make_install DESTDIR=$RPM_BUILD_ROOT install", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT", $0);
508 }
509
510 ##########
511 # %files #
512 ##########
513 /^%files/, (!/^%files/ && $0 ~ SECTIONS) {
514         preamble = 0
515         did_files = 1
516
517         if ($0 ~ /^%files/)
518                 defattr = 1
519
520         if (!use_files_macros()) {
521                 next
522         }
523 }
524
525 ###########
526 # SCRIPTS #
527 ###########
528 /^%pre/, (!/^%pre/ && $0 ~ SECTIONS) {
529         preamble = 0
530
531         if (gsub("/usr/sbin/useradd", "%useradd")) {
532                 sub(" 2> /dev/null \|\| :", "");
533                 sub(" >/dev/null 2>&1 \|\|:", "");
534         }
535
536         # fedora extras macros
537         if (/%__fe_useradd/) {
538                 sub("%__fe_useradd", "%useradd -u ");
539                 sub(" 2> /dev/null \|\| :", "");
540                 sub(" >/dev/null 2>&1 \|\|:", "");
541                 sub(" &>/dev/null \\|\\| :", "");
542         }
543
544         if (/%__fe_groupadd/) {
545                 sub("%__fe_groupadd", "%groupadd -g ");
546                 sub(" &>/dev/null \\|\\| :", "");
547         }
548
549         # %useradd and %groupadd may not be wrapped
550         if (/%(useradd|groupadd).*\\$/) {
551                 a = $0; getline;
552                 sub(/^[\s\t]*/, "");
553                 $0 = substr(a, 1, length(a) - 1) $0;
554         }
555         use_script_macros()
556 }
557
558 /^%post/, (!/^%post/ && $0 ~ SECTIONS) {
559         preamble = 0
560
561         # fedora extras macros
562         sub("%__chkconfig", "/sbin/chkconfig");
563
564         sub("update-desktop-database &> /dev/null \\|\\| :", "%update_desktop_database");
565         sub("touch --no-create %{_datadir}/icons/hicolor", "%update_icon_cache_post hicolor");
566         sub("if \\[ -x %{_bindir}/gtk-update-icon-cache \\]; then\n\t%{_bindir}/gtk-update-icon-cache -q %{_datadir}/icons/hicolor \|\| :\nfi", "");
567
568         sub("export GCONF_CONFIG_SOURCE=`gconftool-2 --get-default-source`", "")
569         if (/gconftool-2 --makefile-install-rule/) {
570                 sub("gconftool-2 --makefile-install-rule %{_sysconfdir}/gconf/schemas/", "%gconf_schema_install ")
571                 sub("> /dev/null", "");
572         }
573
574         use_macros()
575 }
576 /^%preun/, (!/^%preun/ && $0 ~ SECTIONS) {
577         preamble = 0
578         use_macros()
579 }
580 /^%postun/, (!/^%postun/ && $0 ~ SECTIONS) {
581         preamble = 0
582
583         # fedora extras macros
584         if (/%__fe_userdel|%__fe_groupdel/) {
585                 sub("%__fe_groupdel", "%groupremove");
586                 sub("%__fe_userdel", "%userremove");
587                 sub(" &>/dev/null \\|\\| :", "");
588         }
589
590         use_script_macros()
591 }
592 /^%triggerin/, (!/^%triggerin/ && $0 ~ SECTIONS) {
593         preamble = 0
594         use_script_macros()
595 }
596 /^%triggerun/, (!/^%triggerun/ && $0 ~ SECTIONS) {
597         preamble = 0
598         use_script_macros()
599 }
600 /^%triggerpostun/, (!/^%triggerpostun/ && $0 ~ SECTIONS) {
601         preamble = 0
602         use_script_macros()
603 }
604 /^%pretrans/, (!/^%pretrans/ && $0 ~ SECTIONS) {
605         preamble = 0
606         use_script_macros()
607 }
608 /^%posttrans/, (!/^%posttrans/ && $0 ~ SECTIONS) {
609         preamble = 0
610         use_script_macros()
611 }
612 /^%verifyscript/, (!/^%verifyscript/ && $0 ~ SECTIONS) {
613         preamble = 0
614         use_script_macros()
615 }
616 /^%check/, (!/^%check/ && $0 ~ SECTIONS) {
617         preamble = 0
618         use_script_macros()
619 }
620
621 #############
622 # PREAMBLES #
623 #############
624 preamble == 1 {
625         # There should not be a space after the name of field
626         # and before the colon.
627         sub(/[ \t]*:/, ":")
628
629         if (/^%perl_module_wo_prefix/) {
630                 name = $2
631                 version = $3
632                 release = "0." fixedsub(".%{disttag}.at", "", $4)
633         }
634
635         field = tolower($1)
636         if (field ~ /summary:/ && !/etc\.$/ && !/Inc\.$/) {
637                 sub(/\.$/, "", $0);
638         }
639         if (field ~ /group(\([^)]+\)):/) {
640                 next
641         }
642
643         if (field == "group:") {
644                 format_preamble()
645                 group = $0;
646                 sub(/^[^ \t]*[ \t]*/, "", group);
647                 group = replace_groupnames(group);
648                 $0 = "Group:\t\t" group
649
650                 byl_plik_z_groupmi = 0
651                 byl_opis_grupy = 0
652                 while ((getline linia_grup < groups_file) > 0) {
653                         byl_plik_z_groupmi = 1
654                         if (linia_grup == group) {
655                                 byl_opis_grupy = 1
656                                 break
657                         }
658                 }
659
660                 if (!byl_plik_z_groupmi)
661                         print "######\t\t" groups_file ": no such file"
662                 else if (!byl_opis_grupy)
663                         print "######\t\t" "Unknown group!"
664
665                 close(groups_file)
666                 did_groups = 1
667         }
668
669         if (field == "prereq:") {
670                 sub(/Pre[Rr]eq:/, "Requires:", $1);
671         }
672
673         # split (build)requires, obsoletes on commas
674         if (field ~ /(obsoletes|requires|provides|conflicts|suggests):/ && NF > 2) {
675                 value = substr($0, index($0, $2));
676                 $0 = format_requires($1, value);
677         }
678
679         # BR: tar (and others) is to common (rpm-build requires it)
680         if (field == "buildrequires:") {
681                 l = substr($0, index($0, $2));
682                 if (l == "awk" ||
683                         l == "binutils" ||
684                         l == "bzip2" ||
685                         l == "cpio" ||
686                         l == "diffutils" ||
687                         l == "elfutils" ||
688                         l == "fileutils" ||
689                         l == "findutils" ||
690                         l == "glibc-devel" ||
691                         l == "grep" ||
692                         l == "gzip" ||
693                         l == "make" ||
694                         l == "patch" ||
695                         l == "sed" ||
696                         l == "sh-utils" ||
697                         l == "tar" ||
698                         l == "textutils") {
699                         next
700                 }
701
702                 replace_requires(field);
703         }
704
705         if (field == "requires:" || field ~ /^requires\(/) {
706                 replace_requires(field);
707         }
708
709
710         # obsolete/unwanted tags
711         if (field ~ /vendor:|packager:|distribution:|docdir:|prefix:|icon:|author:|author-email:|metadata-version:/) {
712                 next
713         }
714
715         if (field ~ /buildroot:/) {
716                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
717                 did_build_root = 1
718         }
719
720         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
721         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/) {
722                 $1 = "License:"
723         }
724
725         # ease updating from debian .dsc
726         if (field ~ /homepage:/) {
727                 $1 = "URL:"
728         }
729
730         # suse
731         if (field ~ /recommends:/) {
732                 $1 = "Suggests:"
733         }
734
735         if ($3 == "==" && $1 !~ /%/) {
736                 $3 = "="
737         }
738
739         if (field ~ /license:/) {
740                 l = substr($0, index($0, $2));
741                 if (l == "Python Software Foundation License") {
742                         l = "PSF"
743                 }
744                 if (l == "Apache License 2.0" || l == "Apache 2.0" || l == "Apache License Version 2.0" || l == "Apache License, Version 2.0" || l == "Apache Software License v2") {
745                         l = "Apache v2.0"
746                 }
747                 if (l == "Apache Group License" || l == "Apache Software License" || l == "Apache License") {
748                         l = "Apache"
749                 }
750                 if (l == "Apache-style License" || l == "Apache-style Software License") {
751                         l = "Apache-like"
752                 }
753                 if (l == "Apache Software License 1.1" || l == "Apache 1.1") {
754                         l = "Apache v1.1"
755                 }
756                 if (l == "GPLv2") {
757                         l = "GPL v2"
758                 }
759                 if (l == "GPLv2+") {
760                         l = "GPL v2+"
761                 }
762                 if (l == "GPL v2 or later") {
763                         l = "GPL v2+"
764                 }
765                 if (l == "LGPL v2.0 only") {
766                         l = "LGPL v2"
767                 }
768                 if (l == "LGPLv2+") {
769                         l = "LGPL v2+"
770                 }
771                 if (l == "GPLv3") {
772                         l = "GPL v3"
773                 }
774                 if (l == "GPLv3+") {
775                         l = "GPL v3+"
776                 }
777                 if (l == "MPLv1.1") {
778                         l = "MPL v1.1"
779                 }
780                 $0 = "License:\t" l;
781         }
782
783
784         if (field ~ /name:/) {
785                 if ($2 == "%{name}" && name) {
786                         $2 = name
787                 }
788                 name = $2
789                 name_seen = 1;
790         }
791
792         if (field ~ /version:/) {
793                 if ($2 == "%{version}" && version) {
794                         $2 = version
795                 }
796                 version = $2
797                 version_seen = 1;
798         }
799
800         if (field ~ /release:/) {
801                 if ($2 == "%{release}" && release) {
802                         $2 = release
803                 }
804                 sub(/%atrelease /, "0.", $0)
805                 release = $2
806                 release_seen = 1;
807         }
808
809
810         if (field ~ /serial:/)
811                 $1 = "Epoch:"
812
813         if (field ~ /home-page:/)
814                 $1 = "URL:"
815
816         # proper caps
817         if (field ~ /^url:$/)
818                 $1 = "URL:"
819
820         if (field ~ /^patch/)
821                 $1 = "Patch" substr(field, 6);
822
823         if (field ~ /^description:$/)
824                 $1 = "\n%description\n"
825
826         # Use %{name} and %{version} in the filenames in "Source:"
827         if (field ~ /^source/ || field ~ /patch/) {
828                 n = split($2, url, /\//)
829                 if (url[n] ~ /\.gz$/) {
830                         url[n+1] = ".gz" url[n+1]
831                         sub(/\.gz$/,"",url[n])
832                 }
833                 if (url[n] ~ /\.zip$/) {
834                         url[n+1] = ".zip" url[n+1]
835                         sub(/\.zip$/,"",url[n])
836                 }
837                 if (url[n] ~ /\.tar$/) {
838                         url[n+1] = ".tar" url[n+1]
839                         sub(/\.tar$/,"",url[n])
840                 }
841                 if (url[n] ~ /\.patch$/) {
842                         url[n+1] = ".patch" url[n+1]
843                         sub(/\.patch$/,"",url[n])
844                 }
845                 if (url[n] ~ /\.bz2$/) {
846                         url[n+1] = ".bz2" url[n+1]
847                         sub(/\.bz2$/,"",url[n])
848                 }
849                 if (url[n] ~ /\.logrotate$/) {
850                         url[n+1] = ".logrotate" url[n+1]
851                         sub(/\.logrotate$/,"",url[n])
852                 }
853                 if (url[n] ~ /\.pamd$/) {
854                         url[n+1] = ".pamd" url[n+1]
855                         sub(/\.pamd$/,"",url[n])
856                 }
857
858                 # allow %{name} only in last url component
859                 s = ""
860                 for (i = 1; i <= n; i++) {
861                         url[i] = fixedsub("%{name}", name, url[i])
862                         if (s) {
863                                 s = s "/" url[i]
864                         } else {
865                                 s = url[i]
866                         }
867                 }
868                 $2 = s url[n+1]
869
870                 filename = url[n]
871                 if (name) {
872                         url[n] = fixedsub(name, "%{name}", url[n])
873                 }
874                 if (field ~ /source/) {
875                         if (version) {
876                                 url[n] = fixedsub(version, "%{version}", url[n])
877                         }
878                         if (_beta) {
879                                 url[n] = fixedsub(_beta, "%{_beta}", url[n])
880                         }
881                         if (_rc) {
882                                 url[n] = fixedsub(_rc, "%{_rc}", url[n])
883                         }
884                         if (_pre) {
885                                 url[n] = fixedsub(_pre, "%{_pre}", url[n])
886                         }
887                         if (_snap) {
888                                 url[n] = fixedsub(_snap, "%{_snap}", url[n])
889                         }
890                         if (subver) {
891                                 url[n] = fixedsub(subver, "%{subver}", url[n])
892                         }
893                 }
894                 # assigning to $2 kills preamble formatting
895                 $2 = fixedsub(filename, url[n], $2)
896
897                 $2 = unify_url($2)
898         }
899
900
901         if (field ~ /^source:/)
902                 $1 = "Source0:"
903
904         if (field ~ /^patch:/)
905                 $1 = "Patch0:"
906
907         kill_preamble_macros();
908         format_preamble()
909
910         if (field ~ /requires/) {
911                 # atrpms
912                 $0 = fixedsub("%{eversion}", "%{epoch}:%{version}-%{release}", $0);
913         }
914 }
915
916 /^%bcond_/ {
917         # do nothing
918         print
919         next
920 }
921
922 # sort BR/R!
923 #
924 # NOTES:
925 # - mixing BR/R and anything else confuses this (all will be sorted together)
926 #       so don't do that.
927 # - comments leading the BR/R can not be associated,
928 #       so don't adapterize when the BR/R are mixed with comments
929 ENVIRON["SKIP_SORTBR"] != 1 && preamble == 1 && $0 ~ PREAMBLE_TAGS ":", $0 ~ PREAMBLE_TAGS ":"{
930         if ($1 ~ /Pre[Rr]eq:/) {
931                 sub(/Pre[Rr]eq:/, "Requires:", $1);
932         }
933         if ($1 == "BR:" ) {
934                 $1 = "BuildRequires:"
935         }
936         if ($1 == "R:" ) {
937                 $1 = "Requires:"
938         }
939         format_preamble()
940 #       kill_preamble_macros(); # breaks tabbing
941
942         b_idx++;
943         l = substr($0, index($0, $2));
944         b_ktmp = b_makekey($1, l);
945         b_key[b_idx] = b_ktmp;
946         b_val[b_ktmp] = $0;
947
948         next;
949 }
950
951 preamble == 1 {
952         if (b_idx > 0) {
953                 isort(b_key, b_idx);
954                 for (i = 1; i <= b_idx; i++) {
955                         v = b_val[b_key[i]];
956                         sub(/[ \t]+$/, "", v);
957                         print "" v;
958                 }
959                 b_idx = 0
960         }
961 }
962
963 # main() ;-)
964 {
965         preamble = 1
966
967         sub(/[ \t]+$/, "")
968         print
969
970         if (name_seen == 0 && name) {
971                 print "Name:\t\t" name
972                 name_seen = 1
973         }
974
975         if (version_seen == 0 && version) {
976                 print "Version:\t" version
977                 version_seen = 1
978         }
979
980         if (release_seen == 0 && release) {
981                 print "Release:\t" release
982                 release_seen = 1
983         }
984
985         if (did_build_root == 0) {
986 #               print "BuildRoot:\t%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
987                 did_build_root = 1
988         }
989         if (did_groups == 0) {
990 #               print "Group:\t\tunknown"
991                 did_groups = 1
992         }
993 }
994
995
996 END {
997         if (do_not_touch_anything) {
998                 exit(rc)
999         }
1000
1001         # TODO: need to output these in proper place
1002         if (BR_count > 0) {
1003                 for (i = 0; i <= BR_count; i++) {
1004                         print BR[i];
1005                 }
1006         }
1007
1008         if (did_clean == 0) {
1009                 print ""
1010                 print "%clean"
1011                 print "rm -rf $RPM_BUILD_ROOT"
1012         }
1013 }
1014
1015 # substitutes fixed strings (not regexps)
1016 function fixedsub(s1,s2,t, ind) {
1017         if (ind = index(t,s1))
1018                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
1019         return t
1020 }
1021
1022 # replace s with s2 if it equals to s1
1023 function replace(s, s1, s2) {
1024         if (s == s1) {
1025                 return s2;
1026         } else {
1027                 return s;
1028         }
1029 }
1030
1031 # There should be one or two tabs after the colon.
1032 function format_preamble()
1033 {
1034         if (/^#/ || /^%bcond_with/) {
1035                 return;
1036         }
1037         sub(/:[ \t]*/, ":")
1038         if (match($0, /[A-Za-z0-9(),#_ \t.-]+[ \t]*:[ \t]*/) == 1) {
1039                 if (RLENGTH < 8) {
1040                         sub(/:/, ":\t\t")
1041                 } else {
1042                         sub(/:/, ":\t")
1043                 }
1044         }
1045 }
1046
1047 # Replace directly specified directories with macros
1048 function use_macros()
1049 {
1050         # -m, --skip-macros, --no-macros -- skip macros subst
1051         if (ENVIRON["SKIP_MACROS"]) {
1052                 return
1053         }
1054
1055         # leave inline sed lines alone
1056         if (/(%{__sed}|sed) -i -e/) {
1057                 return;
1058         }
1059
1060         sub("%{_defaultdocdir}", "%{_docdir}");
1061         sub("%{_bindir}/perl", "%{__perl}");
1062         sub("%{_bindir}/python", "%{__python}");
1063
1064         gsub(infodir, "%{_infodir}")
1065
1066         gsub(perl_sitearch, "%{perl_sitearch}")
1067         gsub(perl_archlib, "%{perl_archlib}")
1068         gsub(perl_privlib, "%{perl_privlib}")
1069         gsub(perl_vendorlib, "%{perl_vendorlib}")
1070         gsub(perl_vendorarch, "%{perl_vendorarch}")
1071         gsub(perl_sitelib, "%{perl_sitelib}")
1072
1073         gsub(py_sitescriptdir, "%{py_sitescriptdir}")
1074         gsub(py_sitedir, "%{py_sitedir}")
1075         gsub(py_scriptdir, "%{py_scriptdir}")
1076
1077         gsub(py3_sitescriptdir, "%{py3_sitescriptdir}")
1078         gsub(py3_sitedir, "%{py3_sitedir}")
1079         gsub(py3_scriptdir, "%{py3_scriptdir}")
1080
1081         gsub(ruby_archdir, "%{ruby_archdir}")
1082         gsub(ruby_ridir, "%{ruby_ridir}")
1083         gsub(ruby_rubylibdir, "%{ruby_rubylibdir}")
1084         gsub(ruby_sitearchdir, "%{ruby_sitearchdir}")
1085         gsub(ruby_sitelibdir, "%{ruby_sitelibdir}")
1086         gsub(ruby_rdocdir, "%{ruby_rdocdir}")
1087
1088         gsub("%{_unitdir}", "%{systemdunitdir}")
1089         gsub(systemdunitdir, "%{systemdunitdir}")
1090         gsub(systemdtmpfilesdir, "%{systemdtmpfilesdir}")
1091
1092         gsub("%{_datadir}/applications", "%{_desktopdir}")
1093         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}")
1094         gsub("%{_datadir}/java", "%{_javadir}")
1095
1096         gsub("%{_libdir}/pkgconfig", "%{_pkgconfigdir}")
1097         gsub(pkgconfigdir, "%{_pkgconfigdir}")
1098
1099         gsub("%{_datadir}/pkgconfig", "%{_npkgconfigdir}")
1100         gsub(npkgconfigdir, "%{_npkgconfigdir}")
1101
1102         gsub("%{_datadir}/locale", "%{_localedir}")
1103         gsub(localedir, "%{_localedir}")
1104
1105         gsub(libdir, "%{_libdir}")
1106         gsub(javadir, "%{_javadir}")
1107
1108         gsub(bindir, "%{_bindir}")
1109         gsub("%{prefix}/bin", "%{_bindir}")
1110         if (prefix"/bin" == bindir)
1111                 gsub("%{_prefix}/bin", "%{_bindir}")
1112
1113         for (c = 1; c <= NF; c++) {
1114                 if ($c ~ sbindir "/fix-info-dir")
1115                         continue;
1116                 if ($c ~ sbindir "/webapp")
1117                         continue;
1118                 if ($c ~ sbindir "/ldconfig")
1119                         continue;
1120                 if ($c ~ sbindir "/chsh")
1121                         continue;
1122                 if ($c ~ sbindir "/usermod")
1123                         continue;
1124                 if ($c ~ sbindir "/chkconfig")
1125                         continue;
1126                 if ($c ~ sbindir "/installzope(product|3package)")
1127                         continue;
1128                 gsub(sbindir, "%{_sbindir}", $c)
1129         }
1130
1131         gsub("%{prefix}/sbin", "%{_sbindir}")
1132         if (prefix"/sbin" == sbindir) {
1133                 gsub("%{_prefix}/sbin", "%{_sbindir}")
1134         }
1135
1136         for (c = 1; c <= NF; c++) {
1137                 if ($c ~ sysconfdir "/{?cron.")
1138                         continue;
1139                 if ($c ~ sysconfdir "/{?crontab.d")
1140                         continue;
1141                 if ($c ~ sysconfdir "/{?env.d")
1142                         continue;
1143                 if ($c ~ sysconfdir "/{?modprobe.(d|conf)")
1144                         continue;
1145                 if ($c ~ sysconfdir "/{?udev")
1146                         continue;
1147                 if ($c ~ sysconfdir "/{?hotplug")
1148                         continue;
1149                 if ($c ~ sysconfdir "/{?logrotate.d")
1150                         continue;
1151                 if ($c ~ sysconfdir "/{?pam.d")
1152                         continue;
1153                 if ($c ~ sysconfdir "/{?profile.d")
1154                         continue;
1155                 if ($c ~ sysconfdir "/{?rc.d")
1156                         continue;
1157                 if ($c ~ sysconfdir "/{?security")
1158                         continue;
1159                 if ($c ~ sysconfdir "/{?skel")
1160                         continue;
1161                 if ($c ~ sysconfdir "/{?sysconfig")
1162                         continue;
1163                 if ($c ~ sysconfdir "/{?shrc.d")
1164                         continue;
1165                 if ($c ~ sysconfdir "/{?certs")
1166                         continue;
1167                 if ($c ~ sysconfdir "/{?X11")
1168                         continue;
1169                 if ($c ~ sysconfdir "/{?ld.so.conf.d")
1170                         continue;
1171                 if ($c ~ sysconfdir "/{?rpm")
1172                         continue;
1173                 if ($c ~ sysconfdir "/{?bash_completion.d")
1174                         continue;
1175                 if ($c ~ sysconfdir "/{?samba")
1176                         continue;
1177                 if ($c ~ sysconfdir "/{?xdg")
1178                         continue;
1179                 if ($c ~ sysconfdir "/shells")
1180                         continue;
1181                 if ($c ~ sysconfdir "/inittab")
1182                         continue;
1183                 if ($c ~ sysconfdir "/init")
1184                         continue;
1185                 if ($c ~ sysconfdir "/ppp")
1186                         continue;
1187                 if ($c ~ sysconfdir "/dbus-1")
1188                         continue;
1189                 if ($c ~ sysconfdir "/tmpwatch")
1190                         continue;
1191                 if ($c ~ sysconfdir "/acpi")
1192                         continue;
1193                 if ($c ~ sysconfdir "/apm")
1194                         continue;
1195                 if ($c ~ sysconfdir "/modules-load\.d")
1196                         continue;
1197                 gsub(sysconfdir, "%{_sysconfdir}", $c)
1198         }
1199
1200         gsub(docdir, "%{_docdir}")
1201
1202         gsub(kdedocdir, "%{_kdedocdir}")
1203
1204         gsub(gtkdocdir, "%{_gtkdocdir}")
1205         gsub("%{_docdir}/gtk-doc/html", "%{_gtkdocdir}")
1206
1207         gsub(php_pear_dir, "%{php_pear_dir}")
1208         gsub(php_data_dir, "%{php_data_dir}")
1209
1210         for (c = 1; c <= NF; c++) {
1211                 if ($c ~ datadir "/automake")
1212                         continue;
1213                 if ($c ~ datadir "/unsermake")
1214                         continue;
1215                 if ($c ~ datadir "/file/magic.mime")
1216                         continue;
1217                 gsub(datadir, "%{_datadir}", $c)
1218         }
1219
1220         gsub("%{prefix}/share", "%{_datadir}")
1221         if (prefix"/share" == datadir)
1222                 gsub("%{_prefix}/share", "%{_datadir}")
1223
1224         # CFLAGS="-I/usr/include/ncurses is usually correct.
1225         if (!/-I\/usr\/include/) {
1226                 gsub(includedir, "%{_includedir}")
1227         }
1228
1229         gsub("%{prefix}/include", "%{_includedir}")
1230         if (prefix"/include" == includedir) {
1231                 gsub("%{_prefix}/include", "%{_includedir}")
1232         }
1233
1234         gsub(mandir, "%{_mandir}")
1235         if ($0 !~ "%{_datadir}/manual") {
1236                 gsub("%{_datadir}/man", "%{_mandir}")
1237         }
1238         gsub("%{_prefix}/share/man", "%{_mandir}")
1239         gsub("%{prefix}/share/man", "%{_mandir}")
1240         gsub("%{prefix}/man", "%{_mandir}")
1241         gsub("%{_prefix}/man", "%{_mandir}")
1242
1243         gsub(infodir, "%{_infodir}")
1244         gsub("%{prefix}/info", "%{_infodir}")
1245         gsub("%{_prefix}/info", "%{_infodir}")
1246
1247         if (prefix !~ "/X11R6") {
1248                 gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
1249         }
1250
1251         gsub(examplesdir, "%{_examplesdir}")
1252
1253         if (prefix != "/") {
1254                 # leave --with-foo=/usr alone
1255                 if ($0 !~ "--with.*=.*" prefix) {
1256                         for (c = 1; c <= NF; c++) {
1257                                 if ($c ~ prefix "/sbin/fix-info-dir")
1258                                         continue;
1259                                 if ($c ~ prefix "/sbin/webapp")
1260                                         continue;
1261                                 if ($c ~ prefix "/sbin/chsh")
1262                                         continue;
1263                                 if ($c ~ prefix "/sbin/usermod")
1264                                         continue;
1265                                 if ($c ~ prefix "/sbin/installzope(product|3package)")
1266                                         continue;
1267                                 if ($c ~ prefix "/share/automake")
1268                                         continue;
1269                                 if ($c ~ prefix "/share/unsermake")
1270                                         continue;
1271                                 if ($c ~ prefix "/lib/sendmail")
1272                                         continue;
1273                                 if ($c ~ prefix "/lib/pkgconfig")
1274                                         continue;
1275
1276                                 # CFLAGS="-I/usr..." is usually correct.
1277                                 if (/-I\/usr/)
1278                                         continue;
1279                                 # same for LDFLAGS="-L/usr..."
1280                                 if (/-L\/usr/)
1281                                         continue;
1282
1283                                 gsub(prefix, "%{_prefix}", $c)
1284                         }
1285                 }
1286                 gsub("%{prefix}", "%{_prefix}")
1287         }
1288
1289         # replace back
1290         gsub("%{_includedir}/ncurses", "/usr/include/ncurses")
1291         gsub("%{_includedir}/freetype", "/usr/include/freetype")
1292
1293         gsub("%{PACKAGE_VERSION}", "%{version}")
1294         gsub("%{PACKAGE_NAME}", "%{name}")
1295
1296         gsub("^make$", "%{__make}")
1297         gsub("^make ", "%{__make} ")
1298         gsub("^gcc ", "%{__cc} ")
1299         gsub("^rm --interactive=never ", "%{__rm} ")
1300
1301         # fedora
1302         gsub("%{ruby_sitearch}", "%{ruby_sitearchdir}")
1303         gsub("%{python_sitearch}", "%{py_sitedir}")
1304         gsub("%{python_sitelib}", "%{py_sitescriptdir}")
1305
1306         # alt linux
1307         gsub("%_man1dir", "%{_mandir}/man1")
1308
1309         # mandrake specs
1310         gsub("^%make$", "%{__make}")
1311         gsub("^%make ", "%{__make} ")
1312         gsub("^%makeinstall_std", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1313         gsub("^%{makeinstall}", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1314         gsub("^%makeinstall", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1315         gsub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
1316         gsub("^%{__install}", "install")
1317         gsub("%optflags", "%{rpmcflags}")
1318         gsub("%{compat_perl_vendorarch}", "%{perl_vendorarch}")
1319
1320         gsub("^%{__make} install DESTDIR=\$RPM_BUILD_ROOT", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1321         gsub("^fix-info-dir$", "[ ! -x /usr/sbin/fix-info-dir ] || /usr/sbin/fix-info-dir -c %{_infodir} >/dev/null 2>\\&1")
1322         $0 = fixedsub("%buildroot", "$RPM_BUILD_ROOT", $0)
1323         $0 = fixedsub("%{buildroot}", "$RPM_BUILD_ROOT", $0)
1324         $0 = fixedsub("CXXFLAGS=%{rpmcflags} %configure", "CXXFLAGS=%{rpmcflags}\n%configure", $0);
1325         $0 = fixedsub("%__install", "install", $0);
1326
1327         # split configure line to multiple lines
1328         if (/%configure +$/) {
1329                 sub( / +$/, "" );
1330         } else if (/%configure / && !/\\$/) {
1331                 $0 = format_configure($0);
1332         }
1333
1334         gsub("%_bindir", "%{_bindir}")
1335         gsub("%_datadir", "%{_datadir}")
1336         gsub("%_iconsdir", "%{_iconsdir}")
1337         gsub("%_sbindir", "%{_sbindir}")
1338         gsub("%_mandir", "%{_mandir}")
1339         gsub("%name", "%{name}")
1340         gsub(/%__rm/, "rm");
1341         gsub(/%__mkdir_p/, "install -d");
1342         gsub(/%__cp/, "cp");
1343         gsub(/%__ln_s/, "ln -s");
1344         gsub(/%__sed/, "%{__sed}");
1345         gsub(/%__cat/, "cat");
1346         gsub(/%__chmod/, "chmod");
1347
1348         gsub("/usr/src/linux", "%{_kernelsrcdir}")
1349         gsub("%{_prefix}/src/linux", "%{_kernelsrcdir}")
1350
1351         if (/^ant / || /^%{ant}/) {
1352                 sub(/^ant/, "%ant")
1353                 sub(/^%{ant}/, "%ant")
1354                 add_br("BuildRequires:  jpackage-utils");
1355                 add_br("BuildRequires:  rpmbuild(macros) >= 1.300");
1356         }
1357
1358         $0 = fixedsub("%(%{__cc} -dumpversion)", "%{cc_version}", $0);
1359         $0 = fixedsub("%(%{__cxx} -dumpversion)", "%{cxx_version}", $0);
1360
1361         # kill the _class and _subclass pear macros
1362         if (pear_class) {
1363                 gsub("%{_class}", pear_class);
1364         }
1365         if (pear_subclass) {
1366                 gsub("%{_subclass}", pear_subclass);
1367         }
1368 }
1369
1370 function format_configure(line,         n, a, s) {
1371         n = split(line, a, / /);
1372         s = a[1] " \\\n";
1373         for (i = 2; i <= n; i++) {
1374                 s = s "\t" a[i] " \\\n"
1375         }
1376         return s
1377 }
1378
1379
1380 # insertion sort of A[1..n]
1381 # copied from mawk manual
1382 function isort(A,n,             i,j,hold) {
1383         for (i = 2; i <= n; i++) {
1384                 hold = A[j = i]
1385                 while (A[j-1] > hold) {
1386                         j-- ; A[j+1] = A[j]
1387                 }
1388                 A[j] = hold
1389         }
1390         # sentinel A[0] = "" will be created if needed
1391 }
1392
1393
1394 function use_files_macros(      i, n, t, a, l)
1395 {
1396         use_macros()
1397
1398         # skip comments
1399         if (/^#/) {
1400                 return 1;
1401         }
1402
1403         sub("^%doc %{_mandir}", "%{_mandir}")
1404
1405         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
1406         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
1407
1408         # uid/gid nobody is not valid in %files
1409         if (/%attr([^)]*nobody[^)]*)/ && !/FIXME/) {
1410                 $0 = $0 " # FIXME nobody user/group can't own files! -adapter.awk"
1411         }
1412
1413         # s[gu]id programs with globs are evil
1414         if (/%attr\([246]...,.*\*/ && !/FIXME/) {
1415                 $0 = $0 " # FIXME no globs for suid/sgid files"
1416         }
1417
1418         # replace back
1419         gsub("%{_sysconfdir}/cron\.d", "/etc/cron.d")
1420         gsub("%{_sysconfdir}/crontab\.d", "/etc/crontab.d")
1421         gsub("%{_sysconfdir}/logrotate\.d", "/etc/logrotate.d")
1422         gsub("%{_sysconfdir}/pam\.d", "/etc/pam.d")
1423         gsub("%{_sysconfdir}/profile\.d", "/etc/profile.d")
1424         gsub("%{_sysconfdir}/rc\.d", "/etc/rc.d")
1425         gsub("%{_sysconfdir}/security", "/etc/security")
1426         gsub("%{_sysconfdir}/skel", "/etc/skel")
1427         gsub("%{_sysconfdir}/sysconfig", "/etc/sysconfig")
1428         gsub("%{_sysconfdir}/certs", "/etc/certs")
1429         gsub("%{_sysconfdir}/init.d", "/etc/init.d")
1430         gsub("%{_sysconfdir}/dbus-1", "/etc/dbus-1")
1431         gsub("%{_sysconfdir}/pki", "/etc/pki")
1432         gsub("%{_sysconfdir}/tmpwatch", "/etc/tmpwatch")
1433
1434         # /etc/init.d -> /etc/rc.d/init.d
1435         if (!/^\/etc\/init\.d$/) {
1436                  gsub("/etc/init.d", "/etc/rc.d/init.d")
1437         }
1438
1439         if (/\/etc\/rc\.d\/init\.d\// && !/functions/) {
1440                 if (!/%attr.*\/etc\/rc\.d\/init\.d/) {
1441                         $0 = "%attr(754,root,root) " $0
1442                 }
1443                 if (/^%attr.*\/etc\/rc\.d\/init\.d/ && !/^%attr\(754 *,/) {
1444                         gsub("^%attr\\(... *,", "%attr(754,");
1445                 }
1446         }
1447
1448         if (/lib.+\.so\b/ && !/\.so$/ && !/^%attr.*/ && !/%exclude/) {
1449                 $0 = "%attr(755,root,root) " $0
1450         }
1451
1452         if (/%{perl_vendorarch}.*\.so$/ && !/^%attr.*/) {
1453                 $0 = "%attr(755,root,root) " $0
1454         }
1455
1456         # remove attrs from man pages
1457         if (/%{_mandir}/ && /^%attr/) {
1458                 sub("^%attr\\(.*\\) *", "");
1459         }
1460
1461         # /etc/sysconfig files
1462         # %attr(640,root,root) %config(noreplace) %verify(not size mtime md5) /etc/sysconfig/*
1463         # attr not required, allow default 644 attr
1464         if (!/network-scripts/ && !/%dir/ && !/\.d$/ && !/functions/ && !/\/etc\/sysconfig\/wmstyle/) {
1465                 if (/\/etc\/sysconfig\// && /%config/ && !/%config\(noreplace/) {
1466                         gsub("%config", "%config(noreplace)")
1467                 }
1468
1469                 if (/\/etc\/sysconfig\// && !/%config\(noreplace/) {
1470                         $NF = "%config(noreplace) " $NF
1471                 }
1472
1473                 if (/\/etc\/sysconfig\// && /%attr\(755/) {
1474                         gsub("^%attr\\(... *,", "%attr(640,");
1475                 }
1476
1477                 if (/\/etc\/sysconfig\// && !/%verify/) {
1478                         gsub("/etc/sysconfig", "%verify(not size mtime md5) /etc/sysconfig");
1479                 }
1480         }
1481
1482         # kill leading zeros
1483         if (/%attr\(0[1-9]/) {
1484                 gsub("%attr\\(0", "%attr(")
1485         }
1486
1487         # kill leading whitespace
1488         gsub(/^ +/, "");
1489
1490         # kill default attrs
1491         gsub(/%dir %attr\(755,root,root\)/, "%dir");
1492         gsub(/%attr\(755,root,root\) %dir/, "%dir");
1493         if (!/%dir/) {
1494                 gsub(/%attr\(644,root,root\) /, "");
1495         }
1496
1497         # sort %verify attrs
1498         if (match($0, /%verify\(not([^)]+)\)/)) {
1499                 t = substr($0, RSTART, RLENGTH)
1500                 # kill commas: %verify(not,md5,size,mtime)
1501                 gsub(/,/, " ", t);
1502
1503                 gsub(/^%verify\(not |\)$/, "", t)
1504                 n = split(t, a, / /)
1505                 isort(a, n)
1506
1507                 s = "%verify(not"
1508                 for (i = 1 ; i <= n; i++) {
1509                         s = s " " a[i]
1510                 }
1511                 s = s ")"
1512
1513                 gsub(/%verify\(not[^)]+\)/, s)
1514         }
1515
1516         if (/%{_mandir}/) {
1517                 gsub("\.gz$", "*")
1518                 gsub("%ext_man$", "*")
1519         }
1520
1521         # locale dir and no %lang -> bad
1522         if (/%{_datadir}\/locale\/.*\// && !/%(dir|lang)/) {
1523                 $(NF + 1) = "# FIXME consider using %find_lang"
1524         }
1525
1526         # python egg-infos
1527         if (match($0, "^%{py_site(script)?dir}/.+-py"py_ver".egg-info$")) {
1528                 # tests:
1529                 #%{py_sitedir}/*-py2.4.egg-info
1530                 #%{py_sitescriptdir}/GnuPGInterface-%{version}-py2.4.egg-info
1531                 #%{py_sitescriptdir}/python_mpd-%{version}-py2.4.egg-info
1532                 #%{py_sitescriptdir}/mechanize-0.1.6b-py2.4.egg-info
1533
1534                 l = index($0, "/");
1535                 t = substr($0, 0, l);
1536                 s = substr($0, l + 1, RLENGTH - l - length("-py"py_ver".egg-info"));
1537                 if (match(s, "[^-]+$")) {
1538                         if (RSTART > 1) {
1539                                 s = substr(s, 0, RSTART - 1);
1540                         }
1541                         print "%if \"%{py_ver}\" > \"2.4\""
1542                         gsub(t "/.+.egg-info", t "/" s "-*.egg-info");
1543                         print
1544                         print "%endif"
1545                         return 0;
1546                 }
1547         }
1548
1549         # atrpms
1550         $0 = fixedsub("%{perl_man1dir}", "%{_mandir}/man1", $0);
1551         $0 = fixedsub("%{perl_man3dir}", "%{_mandir}/man3", $0);
1552         $0 = fixedsub("%{perl_bin}", "%{_bindir}", $0);
1553
1554         gsub(libdir "/pkgconfig", "%{_pkgconfigdir}");
1555         gsub("%{_libdir}/pkgconfig", "%{_pkgconfigdir}");
1556         gsub("%{_prefix}/lib/pkgconfig", "%{_pkgconfigdir}");
1557
1558         gsub("%{_datadir}/applications", "%{_desktopdir}");
1559         gsub("%{_datadir}/icons", "%{_iconsdir}");
1560         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}");
1561         gsub("%{_datadir}/pear", "%{php_pear_dir}");
1562         gsub("%{_datadir}/php", "%{php_data_dir}");
1563
1564         return 1
1565 }
1566
1567 function use_script_macros()
1568 {
1569         if (gsub("/sbin/service", "%service")) {
1570                 sub(" >/dev/null 2>&1 \|\|:", "");
1571                 sub(" 2> /dev/null \|\| :", "");
1572         }
1573 }
1574
1575 function fill(ch, n, i) {
1576         for (i = 0; i < n; i++)
1577                 printf("%c", ch)
1578 }
1579
1580 function format_flush(line, indent, newline, word, first_word) {
1581         first_word = 1
1582         if (format_indent == -1)
1583                 newline = ""
1584         else
1585                 newline = fill(" ", format_indent) "- "
1586
1587         while (match(line, /[^\t ]+/)) {
1588                 word = substr(line, RSTART, RLENGTH)
1589                 if (length(newline) + length(word) + 1 > tw) {
1590                         print newline
1591
1592                         if (format_indent == -1)
1593                                 newline = ""
1594                         else
1595                                 newline = fill(" ", format_indent + 2)
1596                         first_word = 1
1597                 }
1598
1599                 if (first_word) {
1600                         newline = newline word
1601                         first_word = 0
1602                 } else
1603                         newline = newline " " word
1604
1605                 line = substr(line, RSTART + RLENGTH)
1606         }
1607         if (newline ~ /[^\t ]/) {
1608                 print newline
1609         }
1610 }
1611
1612 function cflags(var)
1613 {
1614         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
1615                 removed[var] = 1
1616                 return 0
1617         }
1618
1619         if (!/!\?debug/)
1620                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
1621         return 1
1622 }
1623
1624 # return whole matched pattern
1625 function matchstr(str, pat)
1626 {
1627         match(str, "[^/]+$");
1628         return substr(str, RSTART, RLENGTH);
1629 }
1630
1631 function unify_url(url)
1632 {
1633
1634         # sourceforge urls
1635         # Docs about sourceforge mirror system: http://sourceforge.net/apps/trac/sourceforge/wiki/Mirrors
1636
1637         # 1. unify domains
1638         sub("^http://prdownloads\.sourceforge\.net/", "http://downloads.sourceforge.net/", url)
1639         sub("^http://download\.sf\.net/", "http://downloads.sourceforge.net/", url)
1640         sub("^http://download\.sourceforge\.net/", "http://downloads.sourceforge.net/", url)
1641         sub("^http://dl\.sourceforge\.net/", "http://downloads.sourceforge.net/", url)
1642         sub("^http://.*\.dl\.sourceforge\.net/", "http://downloads.sourceforge.net/", url)
1643         sub("^http://dl\.sf\.net/", "http://downloads.sourceforge.net/", url)
1644         sub("^http://downloads\.sourceforge\.net/sourceforge/", "http://downloads.sourceforge.net/", url)
1645
1646         # 3. unify urls
1647         if (url ~ /sourceforge.net/) {
1648                 sub("[?&]big_mirror=.*$", "", url);
1649                 sub("[?&]modtime=.*$", "", url);
1650                 sub("[?]use_mirror=.*$", "", url);
1651                 sub("[?]download$", "", url);
1652                 sub("/download$", "", url);
1653         }
1654
1655         # SF: new style urls, strip "files/" between and prepend dl.
1656         if (match(url, "^http://sourceforge.net/projects/[^/]+/files/")) {
1657                 url = substr(url, 1, RLENGTH - length("files/")) substr(url, RSTART + RLENGTH);
1658                 sub("^http://sourceforge.net/projects/", "http://downloads.sourceforge.net/project/", url);
1659         }
1660
1661         # SF unify: http://downloads.sourceforge.net/PROJECT/TARBALL
1662         # http://downloads.sourceforge.net/project/PROJECT/FILE/VERSION/%{name}-%{version}.zip
1663         if (match(url, "^http://downloads.sourceforge.net/project/[^/]+")) {
1664                 url = sprintf("http://downloads.sourceforge.net/%s/%s", substr(url, 42, RLENGTH - 41), matchstr(url, "[^/]+$"));
1665         }
1666
1667         sub("^ftp://ftp\.gnome\.org/", "http://ftp.gnome.org/", url)
1668         sub("^http://ftp\.gnome\.org/pub/gnome/", "http://ftp.gnome.org/pub/GNOME/", url)
1669
1670         # apache urls
1671         sub("^http://apache.zone-h.org/", "http://www.apache.org/dist/", url)
1672
1673         # gnu.org
1674         sub("^ftp://ftp\.gnu\.org/", "http://ftp.gnu.org/", url)
1675         sub("^http://ftp\.gnu\.org/pub/gnu/", "http://ftp.gnu.org/gnu/", url)
1676
1677         # debian.org
1678         sub("^ftp://ftp\.[^.]+\.debian\.org/", "ftp://ftp.debian.org/", url)
1679         sub("^http://ftp\.[^.]+\.debian\.org/", "ftp://ftp.debian.org/", url)
1680         sub("^ftp://ftp\.debian\.org/pub/debian/", "ftp://ftp.debian.org/debian/", url)
1681
1682         return url
1683 }
1684
1685 function demacroize(str)
1686 {
1687         if (mod_name) {
1688                 sub("%{mod_name}", mod_name, str);
1689         }
1690         if (pearname) {
1691                 sub("%{_pearname}", pearname, str);
1692         }
1693         if (pearname) {
1694                 sub("%{pearname}", pearname, str);
1695         }
1696         if (name) {
1697                 sub("%{name}", name, str);
1698         }
1699         if (version) {
1700                 sub("%{version}", version, str);
1701         }
1702         if (_beta) {
1703                 sub("%{_beta}", _beta, str);
1704         }
1705         if (_rc) {
1706                 sub("%{_rc}", _rc, str);
1707         }
1708         if (_pre) {
1709                 sub("%{_pre}", _pre, str);
1710         }
1711         if (_snap) {
1712                 sub("%{_snap}", _snap, str);
1713         }
1714         if (subver) {
1715                 sub("%{subver}", subver, str);
1716         }
1717         return str;
1718 }
1719
1720 function kill_preamble_macros()
1721 {
1722         if ($1 ~ /^Obsoletes:/) {
1723                 # NB! assigning $2 a value breaks tabbing
1724                 $2 = demacroize($2);
1725         }
1726         if ($1 ~ /^URL:/) {
1727                 # NB! assigning $2 a value breaks tabbing
1728                 $2 = demacroize($2);
1729                 $2 = unify_url($2)
1730         }
1731
1732         # fedora extras
1733         if (/%{\?FE_USERADD_REQ}/) {
1734                 $0 = "";
1735                 print "BuildRequires:   rpmbuild(macros) >= 1.202"
1736                 print "Provides:        user(xxx)"
1737                 print "Requires(postun):        /usr/sbin/userdel"
1738                 print "Requires(pre):  /bin/id"
1739                 print "Requires(pre):  /usr/sbin/useradd"
1740         }
1741 }
1742
1743 function get_epoch(pkg, ver,    epoch)
1744 {
1745         return
1746 # should parse the BR lines more adequately:
1747 #       freetype = 2.0.0 -> correct
1748 #       freetype = 2.1.9 -> with epoch 1, as epoch 1 was added in 2.1.7
1749
1750         shell = "grep -o '^" pkg ":[^:]\+' ../PLD-doc/BuildRequires.txt | awk '{print $NF}'";
1751         shell | getline epoch;
1752         return epoch;
1753 }
1754
1755 function format_requires(tag, value,    n, p, i, deps, ndeps) {
1756         # skip any formatting for commented out items or some weird macros
1757         if (/^#/ || /%\(/) {
1758                 return tag "\t" value
1759         }
1760         n = split(value, p, / *,? */);
1761         for (i = 1; i <= n; i++) {
1762                 if (p[i+1] ~ /[<=>]/) {
1763                         # add epoch if the version doesn't have it but BuildRequires.txt has
1764                         if (p[i] ~ /^[a-z]/ && p[i+2] !~ /^[0-9]+:/) {
1765                                 epoch = get_epoch(p[i], p[i+2])
1766                                 if (epoch) {
1767                                         p[i+2] = epoch ":" p[i+2];
1768                                 }
1769                         }
1770                         deps[ndeps++] = p[i] " " p[i+1] " " p[i+2];
1771                         i += 2;
1772                 } else {
1773                         deps[ndeps++] = p[i];
1774                 }
1775         }
1776         s = ""
1777         for (i in deps) {
1778                 s = s sprintf("%s\t%s\n", tag, deps[i]);
1779         }
1780         return substr(s, 1, length(s)-1);
1781 }
1782
1783 function use_tabs()
1784 {
1785         # reverse vim: ts=4 sw=4 et
1786         gsub(/    /, "\t");
1787 }
1788
1789 function add_br(br)
1790 {
1791         BR[BR_count++] = br
1792 }
1793
1794 # Load rpm macros
1795 # you should update the list also in adapter when making changes here
1796 function import_rpm_macros(  v) {
1797         # File with rpm groups
1798         topdir = ENVIRON["_topdir"]
1799
1800         if (!topdir) {
1801                 print "adapter.awk should not not be invoked directly, but via adapter script" > "/dev/stderr"
1802                 do_not_touch_anything = 1
1803                 exit(rc = 1);
1804         }
1805
1806         # update this version dep each time some new macro export is added
1807         v = 1.49
1808         if (!ENVIRON["ADAPTER_REVISION"] || ENVIRON["ADAPTER_REVISION"] < v) {
1809                 printf("adapter shell script is outdated: Need %s, got %s. Please update it.\n", v, ENVIRON["ADAPTER_REVISION"]) > "/dev/stderr"
1810                 do_not_touch_anything = 1
1811                 exit(rc = 1);
1812         }
1813
1814         prefix = ENVIRON["_prefix"]
1815         bindir = ENVIRON["_bindir"]
1816         sbindir = ENVIRON["_sbindir"]
1817         libdir = ENVIRON["_libdir"]
1818         sysconfdir = ENVIRON["_sysconfdir"]
1819         datadir = ENVIRON["_datadir"]
1820         includedir = ENVIRON["_includedir"]
1821         mandir = ENVIRON["_mandir"]
1822         infodir = ENVIRON["_infodir"]
1823         examplesdir = ENVIRON["_examplesdir"]
1824         docdir = ENVIRON["_defaultdocdir"]
1825         kdedocdir = ENVIRON["_kdedocdir"]
1826         gtkdocdir = ENVIRON["_gtkdocdir"]
1827         desktopdir = ENVIRON["_desktopdir"]
1828         pixmapsdir = ENVIRON["_pixmapsdir"]
1829         javadir = ENVIRON["_javadir"]
1830         pkgconfigdir = ENVIRON["_pkgconfigdir"]
1831         npkgconfigdir = ENVIRON["_npkgconfigdir"]
1832         localedir = ENVIRON["_localedir"]
1833
1834         perl_sitearch = ENVIRON["perl_sitearch"]
1835         perl_archlib = ENVIRON["perl_archlib"]
1836         perl_privlib = ENVIRON["perl_privlib"]
1837         perl_vendorlib = ENVIRON["perl_vendorlib"]
1838         perl_vendorarch = ENVIRON["perl_vendorarch"]
1839         perl_sitelib = ENVIRON["perl_sitelib"]
1840
1841         py_sitescriptdir = ENVIRON["py_sitescriptdir"]
1842         py_sitedir = ENVIRON["py_sitedir"]
1843         py_scriptdir = ENVIRON["py_scriptdir"]
1844         py_ver = ENVIRON["py_ver"]
1845
1846         py3_sitescriptdir = ENVIRON["py3_sitescriptdir"]
1847         py3_sitedir = ENVIRON["py3_sitedir"]
1848         py3_scriptdir = ENVIRON["py3_scriptdir"]
1849         py3_ver = ENVIRON["py3_ver"]
1850
1851         ruby_archdir = ENVIRON["ruby_archdir"]
1852         ruby_ridir = ENVIRON["ruby_ridir"]
1853         ruby_rubylibdir = ENVIRON["ruby_rubylibdir"]
1854         ruby_sitearchdir = ENVIRON["ruby_sitearchdir"]
1855         ruby_sitelibdir = ENVIRON["ruby_sitelibdir"]
1856         ruby_rdocdir = ENVIRON["ruby_rdocdir"]
1857
1858         php_pear_dir = ENVIRON["php_pear_dir"]
1859         php_data_dir = ENVIRON["php_data_dir"]
1860         tmpdir = ENVIRON["tmpdir"]
1861
1862         systemdunitdir = ENVIRON["systemdunitdir"]
1863         systemdtmpfilesdir = ENVIRON["systemdtmpfilesdir"]
1864 }
1865
1866 # php virtual deps as discussed in devel-en
1867 function replace_php_virtual_deps(field) {
1868         pkg = $2
1869 #       if (pkg == "php-program") {
1870 #               $0 = $1 "\t/usr/bin/php"
1871 #               return
1872 #       }
1873
1874         if (field == "requires:") {
1875                 if (pkg ~ /^php-(bcmath|bz2|calendar|ctype|curl|dba|date|dom|enchant|exif|fileinfo|filter|fpm|ftp|gd|gettext|gmp|hash|iconv|imap|interbase|intl|json|ldap|mbstring|mcrypt|mssql|mysql|mysqli|odbc|openssl|pcntl|pcre|pdo|pdo-dblib|pdo-firebird|pdo-mysql|pdo-odbc|pdo-pgsql|pdo-sqlite|pgsql|phar|posix|pspell|readline|recode|session|shmop|simplexml|snmp|soap|sockets|spl|sqlite|sqlite3|sybase-ct|sysvmsg|sysvsem|sysvshm|tidy|tokenizer|wddx|xml|xmlreader|xmlrpc|xmlwriter|xsl|zip|zlib)/) {
1876                         sub(/^php-/, "php(", pkg);
1877                         sub(/$/, ")", pkg);
1878                         $2 = pkg
1879                 }
1880         }
1881
1882         if (pkg == "php" || pkg == "php-common") {
1883                 $2 = "php(core)";
1884                 if ($4 ~ /^[0-9]:/) {
1885                         $4 = substr($4, 3);
1886                 }
1887         }
1888
1889         if (pkg == "php4") {
1890                 $2 = "webserver(php)";
1891                 if ($4 ~ /^[0-9]:/) {
1892                         $4 = substr($4, 3);
1893                 }
1894         }
1895 }
1896
1897 function replace_groupnames(group) {
1898         group = replace(group, "Amusements/Games", "Applications/Games");
1899         group = replace(group, "Amusements/Games/Strategy/Real Time", "X11/Applications/Games/Strategy");
1900         group = replace(group, "Application/Multimedia", "Applications/Multimedia");
1901         group = replace(group, "Application/System", "Applications/System");
1902         group = replace(group, "Applications/Compilers", "Development/Languages");
1903         group = replace(group, "Applications/Daemons", "Daemons");
1904         group = replace(group, "Applications/Internet", "Applications/Networking");
1905         group = replace(group, "Applications/Internet/Peer to Peer", "Applications/Networking");
1906         group = replace(group, "Applications/Productivity", "X11/Applications");
1907         group = replace(group, "Applications/Security", "Applications/System");
1908         group = replace(group, "Applications/Web", "Applications/WWW");
1909         group = replace(group, "Database", "Applications/Databases");
1910         group = replace(group, "Development/C", "Development/Libraries");
1911         group = replace(group, "Development/Code Generators", "Development");
1912         group = replace(group, "Development/Docs", "Documentation");
1913         group = replace(group, "Development/Documentation", "Documentation");
1914         group = replace(group, "Development/Java", "Development/Languages/Java");
1915         group = replace(group, "Development/Languages/C and C++", "Libraries");
1916         group = replace(group, "Development/Languages/Other", "Development/Languages");;
1917         group = replace(group, "Development/Languages/Ruby", "Development/Languages");
1918         group = replace(group, "Development/Libraries/C and C++", "Development/Libraries");
1919         group = replace(group, "Development/Libraries/Java", "Development/Languages/Java");
1920         group = replace(group, "Development/Libraries/Python", "Development/Languages/Python");
1921         group = replace(group, "Development/Libraries/TCL", "Development/Languages/Tcl");;
1922         group = replace(group, "Development/Other", "Development");
1923         group = replace(group, "Development/Python", "Development/Languages/Python");
1924         group = replace(group, "Development/Testing", "Development");
1925         group = replace(group, "Editors", "Applications/Text");
1926         group = replace(group, "Emulators", "Applications/Emulators");
1927         group = replace(group, "File tools", "Applications/File");
1928         group = replace(group, "Games", "Applications/Games");
1929         group = replace(group, "Library/Development", "Development/Libraries");
1930         group = replace(group, "Networking/Deamons", "Networking/Daemons");
1931         group = replace(group, "Networking/Mail", "Applications/Mail");
1932         group = replace(group, "Networking/Other", "Networking");
1933         group = replace(group, "Productivity/Databases/Servers", "Applications/Databases");
1934         group = replace(group, "Productivity/Multimedia/Other", "X11/Applications/Multimedia");
1935         group = replace(group, "Productivity/Networking/Web/Servers", "Networking/Daemons/HTTP");;
1936         group = replace(group, "Shells", "Applications/Shells");
1937         group = replace(group, "System Environment/Base", "Base");
1938         group = replace(group, "System Environment/Daemons", "Daemons");
1939         group = replace(group, "System Environment/Kernel", "Base/Kernel");
1940         group = replace(group, "System Environment/Libraries", "Libraries");
1941         group = replace(group, "System Tools", "Applications/System");
1942         group = replace(group, "System", "Base");
1943         group = replace(group, "System/Base", "Base");
1944         group = replace(group, "System/Kernel and hardware", "Base/Kernel");
1945         group = replace(group, "System/Libraries", "Libraries");
1946         group = replace(group, "System/Servers", "Daemons");
1947         group = replace(group, "Text Processing/Markup/HTML", "Applications/Text");
1948         group = replace(group, "Text Processing/Markup/XML", "Applications/Text");
1949         group = replace(group, "Text tools", "Applications/Text");
1950         group = replace(group, "User Interface/Desktops", "X11/Applications");
1951         group = replace(group, "User Interface/X", "X11/Applications");
1952         group = replace(group, "Utilities/System", "Applications/System");
1953         group = replace(group, "Web/Database", "Applications/WWW");
1954         group = replace(group, "X11/GNOME", "X11/Applications");
1955         group = replace(group, "X11/GNOME/Applications", "X11/Applications");
1956         group = replace(group, "X11/GNOME/Development/Libraries", "X11/Development/Libraries");
1957         group = replace(group, "X11/Games", "X11/Applications/Games");
1958         group = replace(group, "X11/Games/Strategy", "X11/Applications/Games/Strategy");
1959         group = replace(group, "X11/Library", "X11/Libraries");
1960         group = replace(group, "X11/Utilities", "X11/Applications");
1961         group = replace(group, "X11/XFree86", "X11");
1962         group = replace(group, "X11/Xserver", "X11/Servers");
1963
1964         return group;
1965 }
1966
1967 function replace_pkgconfig(pkg,    cmd, path, n, i, line, r) {
1968         n = split("/usr/lib64/pkgconfig /usr/lib/pkgconfig /usr/share/pkgconfig", path, / /);
1969         for (i = 1; i <= n; i++) {
1970                 cmd = "rpm -qf --qf '%{N}' " path[i] "/" pkg ".pc";
1971                 # Getline returns 0 on end-of-file, -1 on error, otherwise 1.
1972                 if ((cmd | getline line) <= 0) {
1973                         continue;
1974                 }
1975
1976                 if (line !~ /No such file or directory/) {
1977                         # @modifies global $2
1978                         $2 = line;
1979                         return
1980                 }
1981         }
1982 }
1983
1984 function replace_requires(field,   pkg) {
1985         # pkg-config -> package names
1986         if (match($2, /pkgconfig\(([^)]+)\)/)) {
1987                 pkg = substr($2, RSTART + 10, RLENGTH - 11);
1988                 replace_pkgconfig(pkg);
1989         }
1990
1991         sub(/^python-setuptools-devel$/, "python-distribute", $2);
1992         sub(/^gcc-g77/, "gcc-fortran", $2);
1993
1994         # use virtual, not package name
1995         sub(/^rpm-build-macros$/, "rpmbuild(macros)", $2);
1996
1997         # bad package.xml, see http://pear.php.net/bugs/bug.php?id=17779
1998         sub(/^php-php-gtk/, "php-gtk2", $2);
1999
2000         # {{{ jpackages / fedora java packages
2001         sub(/^antlr3$/, "java-antlr3", $2);
2002         sub(/^aqute-bnd$/, "java-bnd", $2);
2003         sub(/^avalon-framework$/, "java-avalon-framework", $2);
2004         sub(/^avalon-logkit$/, "java-avalon-logkit", $2);
2005         sub(/^axis$/, "java-axis", $2);
2006         sub(/^bouncycastle$/, "java-bcprov", $2);
2007         sub(/^bouncycastle-mail$/, "java-bcmail", $2);
2008         sub(/^bouncycastle-pg$/, "java-bcpg", $2);
2009         sub(/^bouncycastle-tsp$/, "java-bctsp", $2);
2010         sub(/^bsf$/, "java-bsf", $2);
2011         sub(/^dom4j$/, "java-dom4j", $2);
2012         sub(/^flute$/, "java-flute", $2);
2013         sub(/^gnu-regexp$/, "java-gnu-regexp", $2);
2014         sub(/^gnu.regexp$/, "java-gnu-regexp", $2);
2015         sub(/^hamcrest$/, "java-hamcrest", $2);
2016         sub(/^itext$/, "java-itext", $2);
2017         sub(/^jaas$/, "java(jaas)", $2);
2018         sub(/^jaf$/, "java(jaf)", $2);
2019         sub(/^jakarta-ant$/, "ant", $2);
2020         sub(/^jakarta-commons-codec$/, "java-commons-codec", $2);
2021         sub(/^jakarta-commons-httpclient$/, "java-commons-httpclient", $2);
2022         sub(/^jakarta-commons-logging$/, "java-commons-logging", $2);
2023         sub(/^jakarta-log4j$/, "java-log4j", $2);
2024         sub(/^jakarta-oro$/, "java-oro", $2);
2025         sub(/^jakarta-servletapi$/, "java(servlet)", $2);
2026         sub(/^java-devel$/, "jdk", $2);
2027         sub(/^java\(JSP\)$/, "java(jsp)", $2);
2028         sub(/^java\(JavaServerFaces\)$/, "java(javaserverfaces)", $2);
2029         sub(/^java\(Portlet\)$/, "java(portlet)", $2);
2030         sub(/^java\(Servlet\)$/, "java(servlet)", $2);
2031         sub(/^javamail$/, "java(javamail)", $2);
2032         sub(/^jaxp$/, "java(jaxp)", $2);
2033         sub(/^jaxp_parser_impl$/, "java(jaxp_parser_impl)", $2);
2034         sub(/^jaxp_transform_impl$/, "java(jaxp_transform_impl)", $2);
2035         sub(/^jce$/, "java(jce)", $2);
2036         sub(/^jcommon$/, "java-jcommon", $2);
2037         sub(/^jdbc-stdext$/, "java(jdbc-stdext)", $2);
2038         sub(/^jdepend$/, "java-jdepend", $2);
2039         sub(/^jfreechart$/, "java-jfreechart", $2);
2040         sub(/^jmx$/, "java(jmx)", $2);
2041         sub(/^jndi$/, "java(jndi)", $2);
2042         sub(/^jsch$/, "java-jsch", $2);
2043         sub(/^jsse$/, "java(jsse)", $2);
2044         sub(/^jta$/, "java(jta)", $2);
2045         sub(/^junit$/, "java-junit", $2);
2046         sub(/^junit4$/, "java-junit", $2);
2047         sub(/^ldapjdk$/, "ldapsdk", $2);
2048         sub(/^libbase$/, "java-libbase", $2);
2049         sub(/^libfonts$/, "java-libfonts", $2);
2050         sub(/^libformula$/, "java-libformula", $2);
2051         sub(/^liblayout$/, "java-liblayout", $2);
2052         sub(/^libloader$/, "java-libloader", $2);
2053         sub(/^librepository$/, "java-librepository", $2);
2054         sub(/^libserializer$/, "java-libserializer", $2);
2055         sub(/^log4j$/, "java-log4j", $2);
2056         sub(/^logging-log4j$/, "java-log4j", $2);
2057         sub(/^oro$/, "java-oro", $2);
2058         sub(/^pdf-renderer$/, "java-pdf-renderer", $2);
2059         sub(/^pentaho-libxml$/, "java-libxml", $2);
2060         sub(/^rhino$/, "java-rhino", $2);
2061         sub(/^sac$/, "java-sac", $2);
2062         sub(/^saxon-scripts$/, "saxon", $2);
2063         sub(/^servlet$/, "java(servlet)", $2);
2064         sub(/^uddi4j$/, "java-uddi4j", $2);
2065         sub(/^ws-jaxme$/, "java-jaxme", $2);
2066         sub(/^wsdl4j$/, "java-wsdl4j", $2);
2067         sub(/^xalan-j$/, "java-xalan", $2);
2068         sub(/^xalan-j2$/, "java-xalan", $2);
2069         sub(/^xerces-j$/, "java-xerces", $2);
2070         sub(/^xerces-j2$/, "java-xerces", $2);
2071         sub(/^xml-commons-apis$/, "java-xml-commons", $2);
2072         sub(/^xml-commons-resolver$/, "java-xml-commons-resolver", $2);
2073         sub(/^xmldb-api$/, "java-xmldb", $2);
2074         sub(/^xmldb-api-sdk$/, "java-xmldb-sdk", $2);
2075         # }}}
2076
2077         # {{{ fedora / redhat
2078         sub(/^Django$/, "python-django", $2);
2079         sub(/^GitPython$/, "python-git", $2);
2080         sub(/^MySQL-python$/, "python-MySQLdb", $2);
2081         sub(/^PyQt4-devel$/, "python-PyQt4-devel", $2);
2082         sub(/^PyQwt-devel$/, "python-PyQwt-devel", $2);
2083         sub(/^ccid$/, "pcsc-driver-ccid", $2);
2084         sub(/^chkconfig$/, "/sbin/chkconfig", $2);
2085         sub(/^db4-devel$/, "db-devel", $2);
2086         sub(/^dbus-python$/, "python-dbus", $2);
2087         sub(/^desktop-notification-daemon$/, "dbus(org.freedesktop.Notifications)", $2);
2088         sub(/^elfutils-libelf-devel$/, "elfutils-devel", $2);
2089         sub(/^file-devel$/, "libmagic-devel", $2);
2090         sub(/^freetype2-devel$/, "freetype-devel", $2);
2091         sub(/^fuse-devel$/, "libfuse-devel", $2);
2092         sub(/^gamin-python$/, "python-gamin", $2);
2093         sub(/^gcc-c\+\+$/, "libstdc++-devel", $2);
2094         sub(/^gnome-desktop3-devel$/, "gnome-desktop-devel", $2);
2095         sub(/^gnome-python2-extras$/, "python-gnome-extras", $2);
2096         sub(/^gnome-python2-gconf$/, "python-gnome-gconf", $2);
2097         sub(/^gnome-python2-gnomekeyring$/, "python-gnome-desktop-keyring", $2);
2098         sub(/^gnome-python2-gtkspell$/, "python-gnome-extras-gtkspell", $2);
2099         sub(/^gtk-sharp2-devel$/, "dotnet-gtk-sharp2-devel", $2);
2100         sub(/^gtk2$/, "gtk+2", $2);
2101         sub(/^gtk2-devel$/, "gtk+2-devel", $2);
2102         sub(/^gtk3-devel$/, "gtk+3-devel", $2);
2103         sub(/^initscripts$/, "rc-scripts", $2);
2104         sub(/^iproute$/, "iproute2", $2);
2105         sub(/^iscsi-initiator-utils$/, "open-iscsi", $2);
2106         sub(/^keyutils-libs-devel$/, "keyutils-devel", $2);
2107         sub(/^libSM-devel$/, "xorg-lib-libSM-devel", $2);
2108         sub(/^libX11-devel$/, "xorg-lib-libX11-devel", $2);
2109         sub(/^libXScrnSaver-devel$/, "xorg-lib-libXScrnSaver-devel", $2);
2110         sub(/^libXau-devel$/, "xorg-lib-libXau-devel", $2);
2111         sub(/^libXcomposite-devel$/, "xorg-lib-libXcomposite-devel", $2);
2112         sub(/^libXcursor-devel$/, "xorg-lib-libXcursor-devel", $2);
2113         sub(/^libXdamage-devel$/, "xorg-lib-libXdamage-devel", $2);
2114         sub(/^libXext-devel$/, "xorg-lib-libXext-devel", $2);
2115         sub(/^libXft-devel$/, "xorg-lib-libXft-devel", $2);
2116         sub(/^libXinerama-devel$/, "xorg-lib-libXinerama-devel", $2);
2117         sub(/^libXrandr-devel$/, "xorg-lib-libXrandr-devel", $2);
2118         sub(/^libXrender-devel$/, "xorg-lib-libXrender-devel", $2);
2119         sub(/^libXtst-devel$/, "xorg-lib-libXtst-devel", $2);
2120         sub(/^libXv-devel$/, "xorg-lib-libXv-devel", $2);
2121         sub(/^libXxf86vm-devel$/, "xorg-lib-libXxf86vm-devel", $2);
2122         sub(/^libacl-devel$/, "acl-devel", $2);
2123         sub(/^libcurl-devel$/, "curl-devel", $2);
2124         sub(/^libgudev1-devel$/, "udev-glib-devel", $2);
2125         sub(/^libmx-devel$/, "mx-devel", $2);
2126         sub(/^libselinux-python$/, "python-selinux", $2);
2127         sub(/^libsrtp-devel$/, "srtp-devel", $2);
2128         sub(/^libtdb$/, "tdb", $2);
2129         sub(/^libtdb-devel$/, "tdb-devel", $2);
2130         sub(/^libtevent$/, "tevent", $2);
2131         sub(/^libtevent-devel$/, "tevent-devel", $2);
2132         sub(/^libusb1-devel$/, "libusb-devel", $2);
2133         sub(/^libxkbfile-devel$/, "xorg-lib-libxkbfile", $2);
2134         sub(/^mod_wsgi$/, "apache-mod_wsgi", $2);
2135         sub(/^newt-python$/, "python-snack", $2);
2136         sub(/^notify-python$/, "python-pynotify", $2);
2137         sub(/^pcsc-lite-ccid$/, "pcsc-driver-ccid", $2);
2138         sub(/^pulseaudio-libs-devel$/, "pulseaudio-devel", $2);
2139         sub(/^pyOpenSSL$/, "python-pyOpenSSL", $2);
2140         sub(/^pycairo$/, "python-pycairo", $2);
2141         sub(/^pyflakes$/, "python-pyflakes", $2);
2142         sub(/^pygobject2$/, "python-pygobject", $2);
2143         sub(/^pygobject3-devel$/, "python-pygobject3-common-devel", $2);
2144         sub(/^pygtk2$/, "python-pygtk", $2);
2145         sub(/^pygtk2-devel$/, "python-pygtk-devel", $2);
2146         sub(/^pygtk2-libglade$/, "python-pygtk-glade", $2);
2147         sub(/^pysvn$/, "python-pysvn", $2);
2148         sub(/^pytalloc$/, "python-talloc", $2);
2149         sub(/^pytalloc-devel$/, "python-talloc-devel", $2);
2150         sub(/^python-cups$/, "python-pycups", $2);
2151         sub(/^python-enchant$/, "python-pyenchant", $2);
2152         sub(/^python-imaging$/, "python-PIL", $2);
2153         sub(/^python-imaging-tk$/, "python-PIL-tk", $2);
2154         sub(/^python-newt$/, "python-snack", $2);
2155         sub(/^python-pygtk$/, "python-pygtk-gtk", $2);
2156         sub(/^python-recaptcha-client$/, "python-recaptcha", $2);
2157         sub(/^python-twisted-core$/, "python-TwistedCore", $2);
2158         sub(/^python-twisted-core$/, "python-TwistedCore", $2);
2159         sub(/^python-twisted-names$/, "python-TwistedNames", $2);
2160         sub(/^python2-devel$/, "python-devel", $2);
2161         sub(/^pytz$/, "python-pytz", $2);
2162         sub(/^qt4-devel$/, "qt4-build", $2);
2163         sub(/^qt4-webkit-devel$/, "QtWebKit-devel", $2);
2164         sub(/^qtlockedfile-devel$/, "QtLockedFile-devel", $2);
2165         sub(/^qtsingleapplication-devel$/, "QtSingleApplication-devel", $2);
2166         sub(/^rpm-python$/, "python-rpm", $2);
2167         sub(/^sip-devel$/, "python-sip-devel", $2);
2168         sub(/^tftp-server$/, "tftpdaemon", $2);
2169         sub(/^tkinter$/, "python-tkinter", $2);
2170         sub(/^urw-fonts$/, "fonts-Type1-urw", $2);
2171         sub(/^webkitgtk3-devel$/, "gtk-webkit3-devel", $2);
2172         sub(/^xapian-bindings-python$/, "python-xapian", $2);
2173         sub(/^xorg-x11-server-sdk$/, "xorg-xserver-server-devel", $2);
2174         # }}}
2175
2176         # {{{ mandriva
2177         sub(/^python-gobject-devel$/, "python-pygobject-devel", $2);
2178         sub(/^python-pyrex$/, "python-Pyrex", $2);
2179         sub(/^webkitgtk-devel$/, "gtk-webkit-devel", $2);
2180         sub(/^python-curl$/, "python-pycurl", $2);
2181         sub(/^python-webkitgtk$/, "python-pywebkitgtk", $2);
2182         sub(/^pygtk2.0$/, "python-pygtk-gtk", $2);
2183         sub(/^gnome-python-gconf$/, "python-gnome-gconf", $2);
2184         # }}}
2185
2186         # {{{ debian / ubuntu
2187         sub(/^blkid-dev$/, "libblkid-devel", $2);
2188         sub(/^ext2fs-dev$/, "e2fsprogs-devel", $2);
2189         sub(/^libao-dev$/, "libao-devel", $2);
2190         sub(/^libboost-filesystem[0-9.]+-dev$/, "boost-devel", $2);
2191         sub(/^libboost-program-options[0-9.]+-dev$/, "boost-devel", $2);
2192         sub(/^libboost-regex[0-9.]+-dev$/, "boost-devel", $2);
2193         sub(/^libboost-thread[0-9.]+-dev$/, "boost-devel", $2);
2194         sub(/^libcurl4-openssl-dev$/, "curl-devel", $2);
2195         sub(/^libdnet-dev$/, "libdnet-devel", $2);
2196         sub(/^libesd0-dev$/, "esound-devel", $2);
2197         sub(/^libfishsound1-dev$/, "libfishsound-devel", $2);
2198         sub(/^libgconf2-dev$/, "GConf2-devel", $2);
2199         sub(/^libgl1-mesa-dev$/, "OpenGL-devel", $2);
2200         sub(/^libgl1-mesa-dri$/, "OpenGL", $2);
2201         sub(/^libglib2.0-dev$/, "glib2-devel", $2);
2202         sub(/^libglu1-mesa-dev$/, "OpenGL-GLU-devel", $2);
2203         sub(/^libgtk2.0-dev$/, "gtk+2-devel", $2);
2204         sub(/^libhunspell-dev$/, "hunspell-devel", $2);
2205         sub(/^libmcrypt-dev$/, "libmcrypt-devel", $2);
2206         sub(/^libmhash-dev$/, "mhash-devel", $2);
2207         sub(/^liboggz1-dev$/, "libggz-devel", $2);
2208         sub(/^libpango1.0-dev$/, "pango-devel", $2);
2209         sub(/^libqt4-dev$/, "qt4-build", $2);
2210         sub(/^libshout3-dev$/, "libshout-devel", $2);
2211         sub(/^libslp-dev$/, "openslp-devel", $2);
2212         sub(/^libsndfile1-dev$/, "libsndfile-devel", $2);
2213         sub(/^libspeex-dev$/, "speex-devel", $2);
2214         sub(/^libssl-dev$/, "openssl-devel", $2);
2215         sub(/^libvorbis-dev$/, "libvorbis-devel", $2);
2216         sub(/^libxslt1-dev$/, "libxslt-devel", $2);
2217         sub(/^libxss-dev$/, "xorg-lib-libXScrnSaver-devel", $2);
2218         sub(/^mesa-common-dev$/, "OpenGL-devel", $2);
2219         sub(/^libudev$/, "udev-libs", $2);
2220         sub(/^tcp_wrappers-devel$/, "libwrap-devel", $2);
2221         sub(/^vala-tools$/, "vala", $2);
2222         sub(/^vala-devel$/, "vala", $2);
2223         # }}}
2224
2225         # {{{ altlinux
2226         sub(/^libatk-devel$/, "atk-devel", $2);
2227         sub(/^libgit-devel$/, "git-core-devel", $2);
2228         sub(/^libgtk\+2-devel$/, "gtk+2-devel", $2);
2229         sub(/^libncurses-devel$/, "ncurses-devel", $2);
2230         sub(/^libncursesxx-devel$/, "ncurses-c++-devel", $2);
2231         sub(/^libpango-devel$/, "pango-devel", $2);
2232         sub(/^libpcre-devel$/, "pcre-devel", $2);
2233         sub(/^libpopt-devel$/, "popt-devel", $2);
2234         sub(/^libssl-devel$/, "openssl-devel", $2);
2235         # }}}
2236
2237         # {{{ suse
2238         sub(/^alsa-devel$/, "alsa-lib-devel", $2);
2239         sub(/^gtk-sharp2$/, "dotnet-gtk-sharp2", $2);
2240         sub(/^gtkmm2-devel$/, "gtkmm-devel", $2);
2241         sub(/^libexpat-devel$/, "expat-devel", $2);
2242         sub(/^libffmpeg-devel$/, "ffmpeg-devel", $2);
2243         sub(/^libopenssl-devel$/, "openssl-devel", $2);
2244         sub(/^libpulse-devel$/, "pulseaudio-devel", $2);
2245         sub(/^monodoc-core$/, "mono-monodoc", $2);
2246         sub(/^python-gtk$/, "python-pygtk-gtk", $2);
2247         # }}}
2248
2249         replace_php_virtual_deps(field)
2250 }
2251
2252 # vim:ts=4:sw=4 fdm=marker
This page took 3.725533 seconds and 3 git commands to generate.