]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- more java packages rename
[packages/rpm-build-tools.git] / adapter.awk
1 #!/usr/bin/gawk -f
2 #
3 # Adapter adapts .spec files for PLD Linux.
4 #
5 # Copyright (C) 1999-2007 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 #21:25:40  SamChi> glen: it should change: /%source([0-9]+)/i to %{SOURCE\1}
28
29 BEGIN {
30         RPM_SECTIONS = "package|build|changelog|clean|description|install|post|posttrans|postun|pre|prep|pretrans|preun|triggerin|triggerpostun|triggerun|verifyscript|check"
31         SECTIONS = "^%(" RPM_SECTIONS ")"
32
33         RCSID = "$Id$"
34         rev = RCSID # TODO: parse from RCSID
35         VERSION = "0.31/" rev
36
37         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))"
38
39         usedigest = 0   # Enable to switch to rpm 4.4.6+ md5 digests
40
41         preamble = 1    # Is it part of preamble? Default - yes
42         boc = 4                 # Beginning of %changelog
43         bod = 0                 # Beginning of %description
44         tw = 70                 # Descriptions width
45
46         b_idx = 0               # index of BR/R arrays
47         BR_count = 0    # number of additional BuildRequires
48
49         # If variable removed, then 1 (for removing it from export)
50         removed["LDFLAGS"] = 0
51         removed["CFLAGS"] = 0
52         removed["CXXFLAGS"] = 0
53
54         # If 1, we are inside of comment block (started with /^#%/)
55         comment_block = 0
56
57         import_rpm_macros()
58
59         packages_dir = topdir "/packages"
60         groups_file = packages_dir "/rpm.groups"
61
62         system("cd "packages_dir"; [ -f rpm.groups ] || cvs up rpm.groups > /dev/null")
63         system("[ -d ../PLD-doc ] && cd ../PLD-doc && ([ -f BuildRequires.txt ] || cvs up BuildRequires.txt >/dev/null)");
64
65         # Temporary file for changelog section
66         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
67 }
68
69 # There should be a comment with CVS keywords on the first line of file.
70 FNR == 1 {
71         if (!/# \$Revision:/)   # If this line is already OK?
72                 print "# $" "Revision:$, " "$" "Date:$" # No
73         else {
74                 print $0                                # Yes
75                 next            # It is enough for first line
76         }
77 }
78
79 # If the latest line matched /%files/
80 defattr == 1 {
81         if (ENVIRON["SKIP_DEFATTR"] != 1) {
82                 if ($0 !~ /defattr/) {  # If no %defattr
83                         print "%defattr(644,root,root,755)"     # Add it
84                 } else {
85                         $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
86                 }
87         }
88         defattr = 0
89 }
90
91 function b_makekey(a, b,        s) {
92         s = a "" b;
93         # kill bcond
94         gsub(/[#%]+{[!?]+[_a-zA-Z0-9]+:/, "", s);
95
96         # kill commented out items
97         gsub(/^#[ \t]*/, "", s);
98
99         # force order
100         gsub(/^Summary\(/, "11Summary(", s);
101         gsub(/^Summary/, "10Summary", s);
102
103         gsub(/^Name/, "2Name", s);
104         gsub(/^Version/, "3Version", s);
105         gsub(/^Release/, "4Release", s);
106         gsub(/^Epoch/, "5Epoch", s);
107         gsub(/^License/, "5License", s);
108         gsub(/^Group/, "6Group", s);
109         gsub(/^URL/, "7URL", s);
110
111         gsub(/^BuildRequires/, "B1BuildRequires", s);
112         gsub(/^BuildConflicts/, "B2BuildConflicts", s);
113
114         gsub(/^Suggests/, "X1Suggests", s);
115         gsub(/^Provides/, "X2Provides", s);
116         gsub(/^Obsoletes/, "X3Obsoletes", s);
117         gsub(/^Conflicts/, "X4Conflicts", s);
118         gsub(/^BuildArch/, "X5BuildArch", s);
119         gsub(/^ExclusiveArch/, "X6ExclusiveArch", s);
120         gsub(/^ExcludeArch/, "X7ExcludeArch", s);
121         gsub(/^BuildRoot/, "X9BuildRoot", s);
122
123         gsub(/^AutoProv/, "Xx1AutoProv", s);
124         gsub(/^AutoReq/, "Xx2AutoReq", s);
125
126 #       printf("%s -> %s\n", a""b, s);
127         return s;
128 }
129
130 # Comments
131 /^#/ && (description == 0) {
132         if (/This file does not like to be adapterized!/) {
133                 print                   # print this message
134                 while (getline)         # print the rest of spec as it is
135                         print
136                 do_not_touch_anything = 1 # do not touch anything in END()
137                 exit 0
138         }
139
140         # Generally, comments are printed without touching
141         sub(/[ \t]+$/, "")
142
143         if (/#[ \t]*Source.*md5/) {
144                 if (usedigest == 1) {
145                         sub(/^#[ \t]*Source/, "BuildRequires:\tdigest(%SOURCE", $0)
146                         sub(/-md5[ \t]*:[ \t]*/, ") = ", $0)
147                 }
148                 print $0
149                 next
150         }
151 }
152
153 /^%define/ {
154         # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
155         if ($2 == "_applnkdir") {
156                 next
157         }
158         if ($2 == "date") {
159                 date = 1
160                 if (did_files == 0) {
161                         print "%files"
162                         print ""
163                         did_files = 1
164                 }
165         }
166
167         # Do not add %define of _prefix if it already is.
168         if ($2 ~ /^_prefix/) {
169                 sub("^"prefix, $3, bindir)
170                 sub("^"prefix, $3, sbindir)
171                 sub("^"prefix, $3, libdir)
172                 sub("^"prefix, $3, datadir)
173                 sub("^"prefix, $3, includedir)
174                 prefix = $3
175         }
176
177         if ($2 ~ /_bindir/ && !/_sbindir/)
178                 bindir = $3
179         if ($2 ~ /_sbindir/)
180                 sbindir = $3
181         if ($2 ~ /_libdir/) {
182                 if ($3 ~ /^%\(/) {
183                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
184                         libdir = "%%%%%%%%%%%%%%"
185                 } else {
186                         libdir = $3
187                 }
188         }
189         if ($2 ~ /_sysconfdir/) {
190                 if ($3 ~ /^%\(/) {
191                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
192                         sysconfdir = "%%%%%%%%%%%%%%"
193                 } else {
194                         sysconfdir = $3
195                 }
196         }
197         if ($2 ~ /_datadir/) {
198                 if ($3 ~ /^%\(/) {
199                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
200                         datadir = "%%%%%%%%%%%%%%"
201                 } else {
202                         datadir = $3
203                 }
204         }
205         if ($2 ~ /_includedir/)
206                 includedir = $3
207         if ($2 ~ /_mandir/)
208                 mandir = $3
209         if ($2 ~ /_infodir/)
210                 infodir = $3
211         if ($2 ~ /_docdir/)
212                 docdir = $3
213
214         # version related macros
215         if ($2 ~ /^_beta$/)
216                 _beta = $3
217         if ($2 ~ /^_rc$/)
218                 _rc = $3
219         if ($2 ~ /^_pre$/)
220                 _pre = $3
221         if ($2 ~ /^_snap$/)
222                 _snap = $3
223         if ($2 ~ /^subver$/)
224                 subver = $3
225
226         # these are used usually when adapterizing external spec
227         if ($2 ~ /^name$/)
228                 name = $3
229         if ($2 ~ /^version$/)
230                 version = $3
231         if ($2 ~ /^release$/)
232                 release = $3
233
234         if ($2 ~ /^mod_name$/)
235                 mod_name = $3
236
237         sub(/[ \t]+$/, "");
238         # do nothing further, otherwise adapter thinks we're at preamble
239         print
240         next
241 }
242
243 # Obsolete
244 /^%include.*\/usr\/lib\/rpm\/macros\.python$/ {
245         next
246 }
247
248 ################
249 # %description #
250 ################
251 /^%description/, (!/^%description/ && $0 ~ SECTIONS) {
252         preamble = 0
253
254         if (/^%description/) {
255                 bod++
256                 format_line = ""
257                 format_indent = -1
258         }
259
260         # Format description
261         if (ENVIRON["SKIP_DESC"] != 1 && description == 1 && !/^%[a-z]+/ && !/^%description/) {
262                 if (/^[ \t]*$/) {
263                         format_flush(format_line, format_indent)
264                         print ""
265                         format_line = ""
266                         format_indent = -1
267                 } else if (/^[ \t]*[-\*][ \t]*/) {
268                         format_flush(format_line, format_indent)
269                         match($0, /^[ \t]*/)
270                         format_indent = RLENGTH
271                         match($0, /^[ \t]*[-\*][ \t]/)
272                         format_line = substr($0, RLENGTH)
273                 } else
274                         format_line = format_line " " $0
275                 next
276         }
277
278         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
279                 if (NF > 3 && $2 == "-l") {
280                         ll = $1
281                         for (i = 4; i <= NF; i++)
282                                 ll = ll " " $i
283                         $0 = ll " -l " $3
284                 }
285                 format_flush(format_line, format_indent)
286                 if (bod == 2) {
287                         bod = 1
288                         description = 1
289                 } else {
290                         bod = 0
291                         description = 0
292                 }
293         } else
294                 description = 1
295 }
296
297 #########
298 # %prep #
299 #########
300 /^%prep/, (!/^%prep/ && $0 ~ SECTIONS) {
301         preamble = 0
302         did_prep = 1
303
304         use_macros()
305
306         # Add '-q' to %setup
307         if (/^%setup/ && !/-q/) {
308                 sub(/^%setup/, "%setup -q")
309         }
310
311         if (/^%setup/ && name != "setup") {
312                 $0 = fixedsub(name, "%{name}", $0);
313                 $0 = fixedsub(version, "%{version}", $0);
314                 if (_beta) {
315                         $0 = fixedsub(_beta, "%{_beta}", $0);
316                 }
317                 if (_rc) {
318                         $0 = fixedsub(_rc, "%{_rc}", $0);
319                 }
320                 if (_pre) {
321                         $0 = fixedsub(_pre, "%{_pre}", $0);
322                 }
323                 if (_snap) {
324                         $0 = fixedsub(_snap, "%{_snap}", $0);
325                 }
326                 if (subver) {
327                         $0 = fixedsub(subver, "%{subver}", $0);
328                 }
329         }
330
331         if (/^%setup/ && /-n %{name}-%{version}( |$)/) {
332                 $0 = fixedsub(" -n %{name}-%{version}", "", $0)
333         }
334         sub("^%patch ", "%patch0 ");
335
336         # invalid in %prep
337         sub("^rm -rf \$RPM_BUILD_ROOT.*", "");
338 }
339
340 ##########
341 # %build #
342 ##########
343 /^%build/, (!/^%build/ && $0 ~ SECTIONS) {
344         preamble = 0
345
346         if (did_prep == 0) {
347                 print "%prep"
348                 print ""
349                 did_prep = 1
350         }
351
352         use_macros()
353         use_tabs()
354
355         if (/^automake$/)
356                 sub(/$/, " -a -c")
357
358         if (/LDFLAGS/) {
359                 if (/LDFLAGS="-s"/) {
360                         removed["LDFLAGS"] = 1
361                         next
362                 } else {
363                         split($0, tmp, "LDFLAGS=")
364                         count = split(tmp[2], flags, "\"")
365                         if (flags[1] != "" && flags[1] !~ "!?debug") {
366                                 sub(/-s[" ]?/, "%{rpmldflags} ", flags[1])
367                                 $0 = tmp[1] line[1] "LDFLAGS=" flags[1] "\""
368                                 for (i = 2; i < count; i++)
369                                         $0 = $0 flags[i] "\""
370                         }
371                 }
372         }
373
374         if (/CFLAGS=/)
375                 if (cflags("CFLAGS") == 0)
376                         next
377
378         if (/CXXFLAGS=/)
379                 if (cflags("CXXFLAGS") == 0)
380                         next
381
382         if (/^export /) {
383                 if (removed["LDFLAGS"])
384                         sub(" LDFLAGS", "")
385                 if (removed["CFLAGS"])
386                         sub(" CFLAGS", "")
387                 if (removed["CXXFLAGS"])
388                         sub(" CXXFLAGS", "")
389                 # Is there still something?
390                 if (/^export[ ]*$/)
391                         next
392         }
393
394         # quote CC
395         if (/CC=%{__cc} /) {
396                 sub("CC=%{__cc}", "CC=\"%{__cc}\"")
397         }
398         
399         # use PLD Linux macros
400         $0 = fixedsub("glib-gettextize --copy --force","%{__glib_gettextize}", $0);
401         $0 = fixedsub("intltoolize --copy --force", "%{__intltoolize}", $0);
402         $0 = fixedsub("automake --add-missing --copy", "%{__automake}", $0);
403         $0 = fixedsub("automake -a --foreign --copy", "%{__automake}", $0);
404         $0 = fixedsub("automake -a -c --foreign", "%{__automake}", $0);
405         $0 = fixedsub("automake -a -c", "%{__automake}", $0);
406         $0 = fixedsub("libtoolize --force --automake --copy", "%{__libtoolize}", $0);
407         $0 = fixedsub("libtoolize -c -f --automake", "%{__libtoolize}", $0);
408
409         sub(/^aclocal$/, "%{__aclocal}");
410         sub(/^autoheader$/, "%{__autoheader}");
411         sub(/^autoconf$/, "%{__autoconf}");
412         sub(/^automake$/, "%{__automake}");
413         sub(/^libtoolize$/, "%{__libtoolize}");
414
415         # atrpms
416         $0 = fixedsub("%perl_configure", "%{__perl} Makefile.PL \\\n\tINSTALLDIRS=vendor", $0);
417         $0 = fixedsub("%perl_makecheck", "%{?with_tests:%{__make} test}", $0);
418 }
419
420 ##########
421 # %clean #
422 ##########
423 /^%clean/, (!/^%clean/ && $0 ~ SECTIONS) {
424         did_clean = 1
425
426         use_macros()
427 }
428
429 ############
430 # %install #
431 ############
432 /^%install/, (!/^%install/ && $0 ~ SECTIONS) {
433
434         preamble = 0
435
436         # foreign rpms
437         sub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
438         sub("%buildroot", "$RPM_BUILD_ROOT");
439         sub("%{buildroot}", "$RPM_BUILD_ROOT");
440
441         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+(\${?RPM_BUILD_ROOT}?|%{?buildroot}?)/ && did_rmroot==0) {
442                 did_rmroot=1
443                 print "rm -rf $RPM_BUILD_ROOT"
444                 next
445         }
446
447         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
448                 print "rm -rf $RPM_BUILD_ROOT"
449                 did_rmroot=1
450         }
451
452         if (tmpdir) {
453                 buildroot = tmpdir "/" name "-" version "-root-" ENVIRON["USER"]
454                 gsub(buildroot, "$RPM_BUILD_ROOT")
455         }
456
457         if (!/%{_lib}/) {
458                 sub("\$RPM_BUILD_ROOT/%", "$RPM_BUILD_ROOT%")
459         }
460
461         use_macros()
462
463         # 'install -d' instead 'mkdir -p'
464         if (/mkdir -p/)
465                 sub(/mkdir -p/, "install -d")
466
467         # cp -a already implies cp -r
468         sub(/^cp -ar/, "cp -a")
469
470         # No '-u root' or '-g root' for 'install'
471         if (/^install/ && /-[ug][ \t]*root/)
472                 gsub(/-[ug][ \t]*root /, "")
473
474         if (/^install/ && /-m[ \t]*[0-9]+/)
475                 gsub(/-m[ \t]*[0-9]+ /, "")
476
477         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
478         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
479                 next
480
481         # No lines contain 'chmod' if it sets the modes to '644'
482         if ($1 ~ /chmod/ && $2 ~ /644/)
483                 next
484
485         # atrpms
486         $0 = fixedsub("%perl_makeinstall", "%{__make} pure_install \\\n\tDESTDIR=$RPM_BUILD_ROOT", $0);
487 }
488
489 ##########
490 # %files #
491 ##########
492 /^%files/, (!/^%files/ && $0 ~ SECTIONS) {
493         preamble = 0
494         did_files = 1
495
496         if ($0 ~ /^%files/)
497                 defattr = 1
498
499         if (!use_files_macros()) {
500                 next
501         }
502 }
503
504 ##############
505 # %changelog #
506 ##############
507 /^%changelog/, (!/^%changelog/ && $0 ~ SECTIONS) {
508         preamble = 0
509         has_changelog = 1
510         skip = 0
511         # There should be some CVS keywords on the first line of %changelog.
512         if (boc == 3) {
513                 if ($0 !~ _cvsmailfeedback) {
514                         print "* %{date} " _cvsmailfeedback > changelog_file
515                 } else {
516                         skip = 1
517                 }
518                 boc = 2
519         }
520         if (boc == 2 && !skip) {
521                 if (!/All persons listed below/) {
522                         printf "All persons listed below can be reached at " > changelog_file
523                         print "<cvs_login>" _cvsmaildomain "\n" > changelog_file
524                 } else {
525                         skip = 1
526                 }
527                 boc = 1
528         }
529         if (boc == 1 && !skip) {
530                 if (!/^$/) {
531                         if (!/\$.*Log:.*\$/) {
532                                 print "$" "Log:$" > changelog_file
533                         }
534                         boc = 0
535                 }
536         }
537         # Define date macro.
538         if (boc == 4) {
539                 if (date == 0) {
540                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
541                         print " date +\"%a %b %d %Y\"`)" > changelog_file
542                         date = 1
543                 }
544                 boc = 3
545         }
546
547         sub(/[ \t]+$/, "");
548         if (!/^%[a-z]+$/ || /changelog/) {
549                 # stop changelog if "real" changelog starts
550                 if (boc == 0 && /^\* /) {
551                         boc = -1
552                 }
553                 if (boc == -1) {
554                         next;
555                 }
556                 print > changelog_file
557         } else {
558                 print
559         }
560         next
561 }
562
563 ###########
564 # SCRIPTS #
565 ###########
566 /^%pre/, (!/^%pre/ && $0 ~ SECTIONS) {
567         preamble = 0
568
569         if (gsub("/usr/sbin/useradd", "%useradd")) {
570                 sub(" 2> /dev/null \|\| :", "");
571                 sub(" >/dev/null 2>&1 \|\|:", "");
572         }
573
574         # %useradd and %groupadd may not be wrapped
575         if (/%(useradd|groupadd).*\\$/) {
576                 a = $0; getline;
577                 sub(/^[\s\t]*/, "");
578                 $0 = substr(a, 1, length(a) - 1) $0;
579         }
580         use_script_macros()
581 }
582
583 /^%post/, (!/^%post/ && $0 ~ SECTIONS) {
584         preamble = 0
585         use_macros()
586 }
587 /^%preun/, (!/^%preun/ && $0 ~ SECTIONS) {
588         preamble = 0
589         use_macros()
590 }
591 /^%postun/, (!/^%postun/ && $0 ~ SECTIONS) {
592         preamble = 0
593         use_script_macros()
594 }
595 /^%triggerin/, (!/^%triggerin/ && $0 ~ SECTIONS) {
596         preamble = 0
597         use_script_macros()
598 }
599 /^%triggerun/, (!/^%triggerun/ && $0 ~ SECTIONS) {
600         preamble = 0
601         use_script_macros()
602 }
603 /^%triggerpostun/, (!/^%triggerpostun/ && $0 ~ SECTIONS) {
604         preamble = 0
605         use_script_macros()
606 }
607 /^%pretrans/, (!/^%pretrans/ && $0 ~ SECTIONS) {
608         preamble = 0
609         use_script_macros()
610 }
611 /^%posttrans/, (!/^%posttrans/ && $0 ~ SECTIONS) {
612         preamble = 0
613         use_script_macros()
614 }
615 /^%verifyscript/, (!/^%verifyscript/ && $0 ~ SECTIONS) {
616         preamble = 0
617         use_script_macros()
618 }
619 /^%check/, (!/^%check/ && $0 ~ SECTIONS) {
620         preamble = 0
621         use_script_macros()
622 }
623
624 #############
625 # PREAMBLES #
626 #############
627 preamble == 1 {
628         # There should not be a space after the name of field
629         # and before the colon.
630         sub(/[ \t]*:/, ":")
631
632         if (/^%perl_module_wo_prefix/) {
633                 name = $2
634                 version = $3
635                 release = "0." fixedsub(".%{disttag}.at", "", $4)
636         }
637
638         field = tolower($1)
639         fieldnlower = $1
640         if (field ~ /summary:/ && !/etc\.$/ && !/Inc\.$/) {
641                 sub(/\.$/, "", $0);
642         }
643         if (field ~ /group(\([^)]+\)):/)
644                 next
645         if (field ~ /group:/) {
646                 format_preamble()
647                 group = $0;
648                 sub(/^[^ \t]*[ \t]*/, "", group);
649                 group = replace_groupnames(group);
650                 $0 = "Group:\t\t" group
651
652                 if (group ~ /^X11/ && x11 == 0) # Is it X11 application?
653                         x11 = 1
654
655                 byl_plik_z_groupmi = 0
656                 byl_opis_grupy = 0
657                 while ((getline linia_grup < groups_file) > 0) {
658                         byl_plik_z_groupmi = 1
659                         if (linia_grup == group) {
660                                 byl_opis_grupy = 1
661                                 break
662                         }
663                 }
664
665                 if (!byl_plik_z_groupmi)
666                         print "######\t\t" groups_file ": no such file"
667                 else if (!byl_opis_grupy)
668                         print "######\t\t" "Unknown group!"
669
670                 close(groups_file)
671                 did_groups = 1
672         }
673
674         if (field ~ /prereq:/) {
675                 sub(/Pre[Rr]eq:/, "Requires:", $1);
676         }
677
678         # split (build)requires, obsoletes on commas
679         if (field ~ /(obsoletes|requires|provides|conflicts):/ && NF > 2) {
680                 value = substr($0, index($0, $2));
681                 $0 = format_requires($1, value);
682         }
683
684         # BR: tar (and others) is to common (rpm-build requires it)
685         if (field ~ /^buildrequires:/) {
686                 l = substr($0, index($0, $2));
687                 if (l == "awk" ||
688                         l == "binutils" ||
689                         l == "bzip2" ||
690                         l == "cpio" ||
691                         l == "diffutils" ||
692                         l == "elfutils" ||
693                         l == "fileutils" ||
694                         l == "findutils" ||
695                         l == "glibc-devel" ||
696                         l == "grep" ||
697                         l == "gzip" ||
698                         l == "make" ||
699                         l == "patch" ||
700                         l == "sed" ||
701                         l == "sh-utils" ||
702                         l == "tar" ||
703                         l == "textutils") {
704                         next
705                 }
706
707                 replace_requires();
708         }
709
710         if (field ~ /^requires:/) {
711                 replace_requires();
712         }
713
714
715         # obsolete/unwanted tags
716         if (field ~ /vendor:|packager:|distribution:|docdir:|prefix:|icon:|author:|author-email:|metadata-version:/) {
717                 next
718         }
719
720         if (field ~ /buildroot:/) {
721                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
722                 did_build_root = 1
723         }
724
725         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
726         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/) {
727                 $1 = "License:"
728         }
729
730         if (field ~ /license:/) {
731                 l = substr($0, index($0, $2));
732                 if (l == "Python Software Foundation License") {
733                         l = "PSF"
734                 }
735                 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") {
736                         l = "Apache v2.0"
737                 }
738                 if (l == "Apache Group License" || l == "Apache Software License" || l == "Apache License") {
739                         l = "Apache"
740                 }
741                 if (l == "Apache-style License" || l == "Apache-style Software License") {
742                         l = "Apache-like"
743                 }
744                 if (l == "Apache Software License 1.1" || l == "Apache 1.1") {
745                         l = "Apache v1.1"
746                 }
747                 if (l == "GPLv2") {
748                         l = "GPL v2"
749                 }
750                 if (l == "GPLv2+") {
751                         l = "GPL v2+"
752                 }
753                 $0 = "License:\t" l;
754         }
755
756
757         if (field ~ /name:/) {
758                 if ($2 == "%{name}" && name) {
759                         $2 = name
760                 }
761                 name = $2
762                 name_seen = 1;
763         }
764
765         if (field ~ /version:/) {
766                 if ($2 == "%{version}" && version) {
767                         $2 = version
768                 }
769                 version = $2
770                 version_seen = 1;
771         }
772
773         if (field ~ /release:/) {
774                 if ($2 == "%{release}" && release) {
775                         $2 = release
776                 }
777                 sub(/%atrelease /, "0.", $0)
778                 release = $2
779                 release_seen = 1;
780         }
781
782
783         if (field ~ /serial:/)
784                 $1 = "Epoch:"
785
786         if (field ~ /home-page:/)
787                 $1 = "URL:"
788
789         # proper caps
790         if (field ~ /^url:$/)
791                 $1 = "URL:"
792
793         if (field ~ /^description:$/)
794                 $1 = "\n%description\n"
795
796         # Use %{name} and %{version} in the filenames in "Source:"
797         if (field ~ /^source/ || field ~ /patch/) {
798                 n = split($2, url, /\//)
799                 if (url[n] ~ /\.gz$/) {
800                         url[n+1] = ".gz" url[n+1]
801                         sub(/\.gz$/,"",url[n])
802                 }
803                 if (url[n] ~ /\.zip$/) {
804                         url[n+1] = ".zip" url[n+1]
805                         sub(/\.zip$/,"",url[n])
806                 }
807                 if (url[n] ~ /\.tar$/) {
808                         url[n+1] = ".tar" url[n+1]
809                         sub(/\.tar$/,"",url[n])
810                 }
811                 if (url[n] ~ /\.patch$/) {
812                         url[n+1] = ".patch" url[n+1]
813                         sub(/\.patch$/,"",url[n])
814                 }
815                 if (url[n] ~ /\.bz2$/) {
816                         url[n+1] = ".bz2" url[n+1]
817                         sub(/\.bz2$/,"",url[n])
818                 }
819                 if (url[n] ~ /\.logrotate$/) {
820                         url[n+1] = ".logrotate" url[n+1]
821                         sub(/\.logrotate$/,"",url[n])
822                 }
823                 if (url[n] ~ /\.pamd$/) {
824                         url[n+1] = ".pamd" url[n+1]
825                         sub(/\.pamd$/,"",url[n])
826                 }
827
828                 # allow %{name} only in last url component
829                 s = ""
830                 for (i = 1; i <= n; i++) {
831                         url[i] = fixedsub("%{name}", name, url[i])
832                         if (s) {
833                                 s = s "/" url[i]
834                         } else {
835                                 s = url[i]
836                         }
837                 }
838                 $2 = s url[n+1]
839
840                 filename = url[n]
841                 if (name) {
842                         url[n] = fixedsub(name, "%{name}", url[n])
843                 }
844                 if (field ~ /source/) {
845                         if (version) {
846                                 url[n] = fixedsub(version, "%{version}", url[n])
847                         }
848                         if (_beta) {
849                                 url[n] = fixedsub(_beta, "%{_beta}", url[n])
850                         }
851                         if (_rc) {
852                                 url[n] = fixedsub(_rc, "%{_rc}", url[n])
853                         }
854                         if (_pre) {
855                                 url[n] = fixedsub(_pre, "%{_pre}", url[n])
856                         }
857                         if (_snap) {
858                                 url[n] = fixedsub(_snap, "%{_snap}", url[n])
859                         }
860                         if (subver) {
861                                 url[n] = fixedsub(subver, "%{subver}", url[n])
862                         }
863                 }
864                 # assigning to $2 kills preamble formatting
865                 $2 = fixedsub(filename, url[n], $2)
866
867                 $2 = unify_url($2)
868         }
869
870
871         if (field ~ /^source:/)
872                 $1 = "Source0:"
873
874         if (field ~ /^patch:/)
875                 $1 = "Patch0:"
876
877         kill_preamble_macros();
878         format_preamble()
879
880         if (field ~ /requires/) {
881                 # atrpms
882                 $0 = fixedsub("%{eversion}", "%{epoch}:%{version}-%{release}", $0);
883         }
884 }
885
886 /^%bcond_/ {
887         # do nothing
888         print
889         next
890 }
891
892 # sort BR/R!
893 #
894 # NOTES:
895 # - mixing BR/R and anything else confuses this (all will be sorted together)
896 #       so don't do that.
897 # - comments leading the BR/R can not be associated,
898 #       so don't adapterize when the BR/R are mixed with comments
899 ENVIRON["SKIP_SORTBR"] != 1 && preamble == 1 && $0 ~ PREAMBLE_TAGS, $0 ~ PREAMBLE_TAGS {
900         if ($1 ~ /Pre[Rr]eq:/) {
901                 sub(/Pre[Rr]eq:/, "Requires:", $1);
902         }
903         if ($1 == "BR:" ) {
904                 $1 = "BuildRequires:"
905         }
906         if ($1 == "R:" ) {
907                 $1 = "Requires:"
908         }
909         format_preamble()
910 #       kill_preamble_macros(); # breaks tabbing
911
912         b_idx++;
913         l = substr($0, index($0, $2));
914         b_ktmp = b_makekey($1, l);
915         b_key[b_idx] = b_ktmp;
916         b_val[b_ktmp] = $0;
917
918         next;
919 }
920
921 preamble == 1 {
922         if (b_idx > 0) {
923                 isort(b_key, b_idx);
924                 for (i = 1; i <= b_idx; i++) {
925                         v = b_val[b_key[i]];
926                         sub(/[ \t]+$/, "", v);
927                         print "" v;
928                 }
929                 b_idx = 0
930         }
931 }
932
933 # main() ;-)
934 {
935         preamble = 1
936
937         sub(/[ \t]+$/, "")
938         print
939
940         if (name_seen == 0 && name) {
941                 print "Name:\t\t" name
942                 name_seen = 1
943         }
944
945         if (version_seen == 0 && version) {
946                 print "Version:\t" version
947                 version_seen = 1
948         }
949
950         if (release_seen == 0 && release) {
951                 print "Release:\t" release
952                 release_seen = 1
953         }
954
955         if (did_build_root == 0) {
956 #               print "BuildRoot:\t%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
957                 did_build_root = 1
958         }
959         if (did_groups == 0) {
960 #               print "Group:\t\tunknown"
961                 did_groups = 1
962         }
963 }
964
965
966 END {
967         if (do_not_touch_anything)
968                 exit 0
969
970         # TODO: need to output these in proper place
971         if (BR_count > 0) {
972                 for (i = 0; i <= BR_count; i++) {
973                         print BR[i];
974                 }
975         }
976
977         close(changelog_file)
978         while ((getline < changelog_file) > 0)
979                 print
980         system("rm -f " changelog_file)
981
982         if (did_clean == 0) {
983                 print ""
984                 print "%clean"
985                 print "rm -rf $RPM_BUILD_ROOT"
986         }
987
988         if (date == 0) {
989                 print ""
990                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
991         }
992
993         if (has_changelog == 0) {
994                 print "%changelog"
995         }
996
997         if (boc > 2) {
998                 print "* %{date} PLD Team <feedback@pld-linux.org>"
999         }
1000         if (boc > 1) {
1001                 printf "All persons listed below can be reached at "
1002                 print "<cvs_login>@pld-linux.org\n"
1003         }
1004         if (boc > 0) {
1005                 print "$" "Log:$"
1006         }
1007 }
1008
1009 # substitutes fixed strings (not regexps)
1010 function fixedsub(s1,s2,t, ind) {
1011         if (ind = index(t,s1))
1012                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
1013         return t
1014 }
1015
1016 # replace s with s2 if it equals to s1
1017 function replace(s, s1, s2) {
1018         if (s == s1) {
1019                 return s2;
1020         } else {
1021                 return s;
1022         }
1023 }
1024
1025 # There should be one or two tabs after the colon.
1026 function format_preamble()
1027 {
1028         if (/^#/ || /^%bcond_with/) {
1029                 return;
1030         }
1031         sub(/:[ \t]*/, ":")
1032         if (match($0, /[A-Za-z0-9(),#_ \t.-]+[ \t]*:[ \t]*/) == 1) {
1033                 if (RLENGTH < 8) {
1034                         sub(/:/, ":\t\t")
1035                 } else {
1036                         sub(/:/, ":\t")
1037                 }
1038         }
1039 }
1040
1041 # Replace directly specified directories with macros
1042 function use_macros()
1043 {
1044         # -m, --skip-macros, --no-macros -- skip macros subst
1045         if (ENVIRON["SKIP_MACROS"]) {
1046                 return
1047         }
1048
1049         # leave inline sed lines alone
1050         if (/(%{__sed}|sed) -i -e/) {
1051                 return;
1052         }
1053
1054         sub("%{_defaultdocdir}", "%{_docdir}");
1055         sub("%{_bindir}/perl", "%{__perl}");
1056         sub("%{_bindir}/python", "%{__python}");
1057
1058         gsub(infodir, "%{_infodir}")
1059
1060         gsub(perl_sitearch, "%{perl_sitearch}")
1061         gsub(perl_archlib, "%{perl_archlib}")
1062         gsub(perl_privlib, "%{perl_privlib}")
1063         gsub(perl_vendorlib, "%{perl_vendorlib}")
1064         gsub(perl_vendorarch, "%{perl_vendorarch}")
1065         gsub(perl_sitelib, "%{perl_sitelib}")
1066         
1067         gsub(py_sitescriptdir, "%{py_sitescriptdir}")
1068         gsub(py_sitedir, "%{py_sitedir}")
1069         gsub(py_scriptdir, "%{py_scriptdir}")
1070         gsub("%{_libdir}/python2.4/site-packages", "%{py_sitedir}")
1071         gsub("%{python_sitelib}", "%{py_sitedir}")
1072
1073         gsub(ruby_archdir, "%{ruby_archdir}")
1074         gsub(ruby_ridir, "%{ruby_ridir}")
1075         gsub(ruby_rubylibdir, "%{ruby_rubylibdir}")
1076         gsub(ruby_sitearchdir, "%{ruby_sitearchdir}")
1077         gsub(ruby_sitelibdir, "%{ruby_sitelibdir}")
1078         gsub(ruby_rdocdir, "%{ruby_rdocdir}")
1079
1080         gsub("%{_datadir}/applications", "%{_desktopdir}")
1081         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}")
1082         gsub("%{_datadir}/java", "%{_javadir}")
1083
1084         gsub(libdir, "%{_libdir}")
1085         gsub(javadir, "%{_javadir}")
1086
1087         gsub(bindir, "%{_bindir}")
1088         gsub("%{prefix}/bin", "%{_bindir}")
1089         if (prefix"/bin" == bindir)
1090                 gsub("%{_prefix}/bin", "%{_bindir}")
1091
1092         for (c = 1; c <= NF; c++) {
1093                 if ($c ~ sbindir "/fix-info-dir")
1094                         continue;
1095                 if ($c ~ sbindir "/webapp")
1096                         continue;
1097                 if ($c ~ sbindir "/ldconfig")
1098                         continue;
1099                 if ($c ~ sbindir "/chsh")
1100                         continue;
1101                 if ($c ~ sbindir "/usermod")
1102                         continue;
1103                 if ($c ~ sbindir "/chkconfig")
1104                         continue;
1105                 if ($c ~ sbindir "/installzope(product|3package)")
1106                         continue;
1107                 gsub(sbindir, "%{_sbindir}", $c)
1108         }
1109
1110         gsub("%{prefix}/sbin", "%{_sbindir}")
1111         if (prefix"/sbin" == sbindir) {
1112                 gsub("%{_prefix}/sbin", "%{_sbindir}")
1113         }
1114
1115         for (c = 1; c <= NF; c++) {
1116                 if ($c ~ sysconfdir "/{?cron.")
1117                         continue;
1118                 if ($c ~ sysconfdir "/{?crontab.d")
1119                         continue;
1120                 if ($c ~ sysconfdir "/{?env.d")
1121                         continue;
1122                 if ($c ~ sysconfdir "/{?modprobe.(d|conf)")
1123                         continue;
1124                 if ($c ~ sysconfdir "/{?udev")
1125                         continue;
1126                 if ($c ~ sysconfdir "/{?hotplug")
1127                         continue;
1128                 if ($c ~ sysconfdir "/{?logrotate.d")
1129                         continue;
1130                 if ($c ~ sysconfdir "/{?pam.d")
1131                         continue;
1132                 if ($c ~ sysconfdir "/{?profile.d")
1133                         continue;
1134                 if ($c ~ sysconfdir "/{?rc.d")
1135                         continue;
1136                 if ($c ~ sysconfdir "/{?security")
1137                         continue;
1138                 if ($c ~ sysconfdir "/{?skel")
1139                         continue;
1140                 if ($c ~ sysconfdir "/{?sysconfig")
1141                         continue;
1142                 if ($c ~ sysconfdir "/{?shrc.d")
1143                         continue;
1144                 if ($c ~ sysconfdir "/{?certs")
1145                         continue;
1146                 if ($c ~ sysconfdir "/{?X11")
1147                         continue;
1148                 if ($c ~ sysconfdir "/{?ld.so.conf.d")
1149                         continue;
1150                 if ($c ~ sysconfdir "/{?rpm")
1151                         continue;
1152                 if ($c ~ sysconfdir "/{?bash_completion.d")
1153                         continue;
1154                 if ($c ~ sysconfdir "/{?samba")
1155                         continue;
1156                 if ($c ~ sysconfdir "/shells")
1157                         continue;
1158                 if ($c ~ sysconfdir "/ppp")
1159                         continue;
1160                 if ($c ~ sysconfdir "/dbus-1")
1161                         continue;
1162                 if ($c ~ sysconfdir "/tmpwatch")
1163                         continue;
1164                 gsub(sysconfdir, "%{_sysconfdir}", $c)
1165         }
1166
1167         gsub(docdir, "%{_docdir}")
1168
1169         gsub(kdedocdir, "%{_kdedocdir}")
1170
1171         gsub(gtkdocdir, "%{_gtkdocdir}")
1172         gsub("%{_docdir}/gtk-doc/html", "%{_gtkdocdir}")
1173
1174         gsub(php_pear_dir, "%{php_pear_dir}")
1175         gsub(php_data_dir, "%{php_data_dir}")
1176
1177         for (c = 1; c <= NF; c++) {
1178                 if ($c ~ datadir "/automake")
1179                         continue;
1180                 if ($c ~ datadir "/unsermake")
1181                         continue;
1182                 if ($c ~ datadir "/file/magic.mime")
1183                         continue;
1184                 gsub(datadir, "%{_datadir}", $c)
1185         }
1186
1187         gsub("%{prefix}/share", "%{_datadir}")
1188         if (prefix"/share" == datadir)
1189                 gsub("%{_prefix}/share", "%{_datadir}")
1190
1191         # CFLAGS="-I/usr/include/ncurses is usually correct.
1192         if (!/-I\/usr\/include/) {
1193                 gsub(includedir, "%{_includedir}")
1194         }
1195
1196         gsub("%{prefix}/include", "%{_includedir}")
1197         if (prefix"/include" == includedir) {
1198                 gsub("%{_prefix}/include", "%{_includedir}")
1199         }
1200
1201         gsub(mandir, "%{_mandir}")
1202         if ($0 !~ "%{_datadir}/manual") {
1203                 gsub("%{_datadir}/man", "%{_mandir}")
1204         }
1205         gsub("%{_prefix}/share/man", "%{_mandir}")
1206         gsub("%{prefix}/share/man", "%{_mandir}")
1207         gsub("%{prefix}/man", "%{_mandir}")
1208         gsub("%{_prefix}/man", "%{_mandir}")
1209
1210         gsub(infodir, "%{_infodir}")
1211         gsub("%{prefix}/info", "%{_infodir}")
1212         gsub("%{_prefix}/info", "%{_infodir}")
1213
1214         if (prefix !~ "/X11R6") {
1215                 gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
1216         }
1217
1218         gsub(examplesdir, "%{_examplesdir}")
1219
1220         if (prefix != "/") {
1221                 # leave --with-foo=/usr alone
1222                 if ($0 !~ "--with.*=.*" prefix) {
1223                         for (c = 1; c <= NF; c++) {
1224                                 if ($c ~ prefix "/sbin/fix-info-dir")
1225                                         continue;
1226                                 if ($c ~ prefix "/sbin/webapp")
1227                                         continue;
1228                                 if ($c ~ prefix "/sbin/chsh")
1229                                         continue;
1230                                 if ($c ~ prefix "/sbin/usermod")
1231                                         continue;
1232                                 if ($c ~ prefix "/sbin/installzope(product|3package)")
1233                                         continue;
1234                                 if ($c ~ prefix "/share/automake")
1235                                         continue;
1236                                 if ($c ~ prefix "/share/unsermake")
1237                                         continue;
1238                                 if ($c ~ prefix "/lib/sendmail")
1239                                         continue;
1240                                 if ($c ~ prefix "/lib/pkgconfig")
1241                                         continue;
1242
1243                                 # CFLAGS="-I/usr..." is usually correct.
1244                                 if (/-I\/usr/)
1245                                         continue;
1246                                 # same for LDFLAGS="-L/usr..."
1247                                 if (/-L\/usr/)
1248                                         continue;
1249
1250                                 gsub(prefix, "%{_prefix}", $c)
1251                         }
1252                 }
1253                 gsub("%{prefix}", "%{_prefix}")
1254         }
1255
1256         # replace back
1257         gsub("%{_includedir}/ncurses", "/usr/include/ncurses")
1258         gsub("%{_includedir}/freetype", "/usr/include/freetype")
1259
1260         gsub("%{PACKAGE_VERSION}", "%{version}")
1261         gsub("%{PACKAGE_NAME}", "%{name}")
1262
1263         gsub("^make$", "%{__make}")
1264         gsub("^make ", "%{__make} ")
1265         gsub("^gcc ", "%{__cc} ")
1266         gsub("^rm --interactive=never ", "%{__rm} ")
1267
1268         # mandrake specs
1269         gsub("^%make$", "%{__make}")
1270         gsub("^%make ", "%{__make} ")
1271         gsub("^%makeinstall_std", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1272         gsub("^%{makeinstall}", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1273         gsub("^%makeinstall", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1274         gsub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
1275         gsub("^%{__install}", "install")
1276         gsub("%optflags", "%{rpmcflags}")
1277         gsub("%{compat_perl_vendorarch}", "%{perl_vendorarch}")
1278
1279         gsub("^%{__make} install DESTDIR=\$RPM_BUILD_ROOT", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1280         gsub("^fix-info-dir$", "[ ! -x /usr/sbin/fix-info-dir ] || /usr/sbin/fix-info-dir -c %{_infodir} >/dev/null 2>\\&1")
1281         $0 = fixedsub("%buildroot", "$RPM_BUILD_ROOT", $0)
1282         $0 = fixedsub("%{buildroot}", "$RPM_BUILD_ROOT", $0)
1283         $0 = fixedsub("CXXFLAGS=%{rpmcflags} %configure", "CXXFLAGS=%{rpmcflags}\n%configure", $0);
1284         $0 = fixedsub("%__install", "install", $0);
1285
1286         # split configure line to multiple lines
1287         if (/%configure / && !/\\$/) {
1288                 $0 = format_configure($0);
1289         }
1290
1291         gsub("%_bindir", "%{_bindir}")
1292         gsub("%_datadir", "%{_datadir}")
1293         gsub("%_iconsdir", "%{_iconsdir}")
1294         gsub("%_sbindir", "%{_sbindir}")
1295         gsub("%_mandir", "%{_mandir}")
1296         gsub("%name", "%{name}")
1297         gsub(/%__rm/, "rm");
1298         gsub(/%__mkdir_p/, "install -d");
1299         gsub(/%__cp/, "cp");
1300         gsub(/%__ln_s/, "ln -s");
1301         gsub(/%__sed/, "%{__sed}");
1302         gsub(/%__cat/, "cat");
1303         gsub(/%__chmod/, "chmod");
1304
1305         gsub("/usr/src/linux", "%{_kernelsrcdir}")
1306         gsub("%{_prefix}/src/linux", "%{_kernelsrcdir}")
1307
1308         if (/^ant / || /^%{ant}/) {
1309                 sub(/^ant/, "%ant")
1310                 sub(/^%{ant}/, "%ant")
1311                 add_br("BuildRequires:  jpackage-utils");
1312                 add_br("BuildRequires:  rpmbuild(macros) >= 1.300");
1313         }
1314
1315         $0 = fixedsub("%(%{__cc} -dumpversion)", "%{cc_version}", $0);
1316         $0 = fixedsub("%(%{__cxx} -dumpversion)", "%{cxx_version}", $0);
1317 }
1318
1319 function format_configure(line,         n, a, s) {
1320         n = split(line, a, / /);
1321         s = a[1] " \\\n";
1322         for (i = 2; i <= n; i++) {
1323                 s = s "\t" a[i] " \\\n"
1324         }
1325         return s
1326 }
1327
1328
1329 # insertion sort of A[1..n]
1330 # copied from mawk manual
1331 function isort(A,n,             i,j,hold) {
1332         for (i = 2; i <= n; i++) {
1333                 hold = A[j = i]
1334                 while (A[j-1] > hold) {
1335                         j-- ; A[j+1] = A[j]
1336                 }
1337                 A[j] = hold
1338         }
1339         # sentinel A[0] = "" will be created if needed
1340 }
1341
1342
1343 function use_files_macros(      i, n, t, a, l)
1344 {
1345         use_macros()
1346
1347         # skip comments
1348         if (/^#/) {
1349                 return 1;
1350         }
1351
1352         sub("^%doc %{_mandir}", "%{_mandir}")
1353
1354         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
1355         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
1356
1357         # uid/gid nobody is not valid in %files
1358         if (/%attr([^)]*nobody[^)]*)/ && !/FIXME/) {
1359                 $0 = $0 " # FIXME nobody user/group can't own files! -adapter.awk"
1360         }
1361
1362         # s[gu]id programs with globs are evil
1363         if (/%attr\([246]...,.*\*/ && !/FIXME/) {
1364                 $0 = $0 " # FIXME no globs for suid/sgid files"
1365         }
1366
1367         # replace back
1368         gsub("%{_sysconfdir}/cron\.d", "/etc/cron.d")
1369         gsub("%{_sysconfdir}/crontab\.d", "/etc/crontab.d")
1370         gsub("%{_sysconfdir}/logrotate\.d", "/etc/logrotate.d")
1371         gsub("%{_sysconfdir}/pam\.d", "/etc/pam.d")
1372         gsub("%{_sysconfdir}/profile\.d", "/etc/profile.d")
1373         gsub("%{_sysconfdir}/rc\.d", "/etc/rc.d")
1374         gsub("%{_sysconfdir}/security", "/etc/security")
1375         gsub("%{_sysconfdir}/skel", "/etc/skel")
1376         gsub("%{_sysconfdir}/sysconfig", "/etc/sysconfig")
1377         gsub("%{_sysconfdir}/certs", "/etc/certs")
1378         gsub("%{_sysconfdir}/init.d", "/etc/init.d")
1379         gsub("%{_sysconfdir}/dbus-1", "/etc/dbus-1")
1380         gsub("%{_sysconfdir}/pki", "/etc/pki")
1381         gsub("%{_sysconfdir}/tmpwatch", "/etc/tmpwatch")
1382
1383         # /etc/init.d -> /etc/rc.d/init.d
1384         if (!/^\/etc\/init\.d$/) {
1385                  gsub("/etc/init.d", "/etc/rc.d/init.d")
1386         }
1387
1388         if (/\/etc\/rc\.d\/init\.d\// && !/functions/) {
1389                 if (!/%attr.*\/etc\/rc\.d\/init\.d/) {
1390                         $0 = "%attr(754,root,root) " $0
1391                 }
1392                 if (/^%attr.*\/etc\/rc\.d\/init\.d/ && !/^%attr\(754 *,/) {
1393                         gsub("^%attr\\(... *,", "%attr(754,");
1394                 }
1395         }
1396
1397         if (/lib.+\.so/ && !/\.so$/ && !/^%attr.*/ && !/%exclude/) {
1398                 $0 = "%attr(755,root,root) " $0
1399         }
1400
1401         if (/%{perl_vendorarch}.*\.so$/ && !/^%attr.*/) {
1402                 $0 = "%attr(755,root,root) " $0
1403         }
1404
1405         # /etc/sysconfig files
1406         # %attr(640,root,root) %config(noreplace) %verify(not size mtime md5) /etc/sysconfig/*
1407         # attr not required, allow default 644 attr
1408         if (!/network-scripts/ && !/%dir/ && !/\.d$/ && !/functions/ && !/\/etc\/sysconfig\/wmstyle/) {
1409                 if (/\/etc\/sysconfig\// && /%config/ && !/%config\(noreplace/) {
1410                         gsub("%config", "%config(noreplace)")
1411                 }
1412
1413                 if (/\/etc\/sysconfig\// && !/%config\(noreplace/) {
1414                         $NF = "%config(noreplace) " $NF
1415                 }
1416
1417                 if (/\/etc\/sysconfig\// && /%attr\(755/) {
1418                         gsub("^%attr\\(... *,", "%attr(640,");
1419                 }
1420
1421                 if (/\/etc\/sysconfig\// && !/%verify/) {
1422                         gsub("/etc/sysconfig", "%verify(not size mtime md5) /etc/sysconfig");
1423                 }
1424         }
1425
1426         # kill leading zeros
1427         if (/%attr\(0[1-9]/) {
1428                 gsub("%attr\\(0", "%attr(")
1429         }
1430
1431         # kill leading whitespace
1432         gsub(/^ +/, "");
1433
1434         # kill default attrs
1435         gsub(/%dir %attr\(755,root,root\)/, "%dir");
1436         gsub(/%attr\(755,root,root\) %dir/, "%dir");
1437         if (!/%dir/) {
1438                 gsub(/%attr\(644,root,root\) /, "");
1439         }
1440
1441         # sort %verify attrs
1442         if (match($0, /%verify\(not([^)]+)\)/)) {
1443                 t = substr($0, RSTART, RLENGTH)
1444                 # kill commas: %verify(not,md5,size,mtime)
1445                 gsub(/,/, " ", t);
1446
1447                 gsub(/^%verify\(not |\)$/, "", t)
1448                 n = split(t, a, / /)
1449                 isort(a, n)
1450
1451                 s = "%verify(not"
1452                 for (i = 1 ; i <= n; i++) {
1453                         s = s " " a[i]
1454                 }
1455                 s = s ")"
1456
1457                 gsub(/%verify\(not[^)]+\)/, s)
1458         }
1459
1460         if (/%{_mandir}/) {
1461                 gsub("\.gz$", "*")
1462         }
1463
1464         # locale dir and no %lang -> bad
1465         if (/%{_datadir}\/locale\/.*\// && !/%(dir|lang)/) {
1466                 $(NF + 1) = "# FIXME consider using %find_lang"
1467         }
1468
1469         # python egg-infos
1470         if (match($0, "^%{py_site(script)?dir}/.+-py"py_ver".egg-info$")) {
1471                 # tests:
1472                 #%{py_sitedir}/*-py2.4.egg-info
1473                 #%{py_sitescriptdir}/GnuPGInterface-%{version}-py2.4.egg-info
1474                 #%{py_sitescriptdir}/python_mpd-%{version}-py2.4.egg-info
1475                 #%{py_sitescriptdir}/mechanize-0.1.6b-py2.4.egg-info
1476
1477                 l = index($0, "/");
1478                 t = substr($0, 0, l);
1479                 s = substr($0, l + 1, RLENGTH - l - length("-py"py_ver".egg-info"));
1480                 if (match(s, "[^-]+$")) {
1481 #printf("s[%s]; start[%d]; length[%d]\n", s, RSTART, RLENGTH);
1482                         if (RSTART > 1) {
1483                                 s = substr(s, 0, RSTART - 1);
1484                         }
1485 #printf("s2[%s]\n", s);
1486                         print "%if \"%{py_ver}\" > \"2.4\""
1487 #print t "/.+.egg-info"
1488                         gsub(t "/.+.egg-info", t "/" s "-*.egg-info");
1489                         print
1490                         print "%endif"
1491                         return 0;
1492                 }
1493         }
1494
1495         # atrpms
1496         $0 = fixedsub("%{perl_man1dir}", "%{_mandir}/man1", $0);
1497         $0 = fixedsub("%{perl_man3dir}", "%{_mandir}/man3", $0);
1498         $0 = fixedsub("%{perl_bin}", "%{_bindir}", $0);
1499
1500         gsub(libdir "/pkgconfig", "%{_pkgconfigdir}");
1501         gsub("%{_libdir}/pkgconfig", "%{_pkgconfigdir}");
1502         gsub("%{_prefix}/lib/pkgconfig", "%{_pkgconfigdir}");
1503
1504         gsub("%{_datadir}/applications", "%{_desktopdir}");
1505         gsub("%{_datadir}/icons", "%{_iconsdir}");
1506         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}");
1507         gsub("%{_datadir}/pear", "%{php_pear_dir}");
1508         gsub("%{_datadir}/php", "%{php_data_dir}");
1509
1510         return 1
1511 }
1512
1513 function use_script_macros()
1514 {
1515         if (gsub("/sbin/service", "%service")) {
1516                 sub(" >/dev/null 2>&1 \|\|:", "");
1517                 sub(" 2> /dev/null \|\| :", "");
1518         }
1519 }
1520
1521 function fill(ch, n, i) {
1522         for (i = 0; i < n; i++)
1523                 printf("%c", ch)
1524 }
1525
1526 function format_flush(line, indent, newline, word, first_word) {
1527         first_word = 1
1528         if (format_indent == -1)
1529                 newline = ""
1530         else
1531                 newline = fill(" ", format_indent) "- "
1532
1533         while (match(line, /[^\t ]+/)) {
1534                 word = substr(line, RSTART, RLENGTH)
1535                 if (length(newline) + length(word) + 1 > tw) {
1536                         print newline
1537
1538                         if (format_indent == -1)
1539                                 newline = ""
1540                         else
1541                                 newline = fill(" ", format_indent + 2)
1542                         first_word = 1
1543                 }
1544
1545                 if (first_word) {
1546                         newline = newline word
1547                         first_word = 0
1548                 } else
1549                         newline = newline " " word
1550
1551                 line = substr(line, RSTART + RLENGTH)
1552         }
1553         if (newline ~ /[^\t ]/) {
1554                 print newline
1555         }
1556 }
1557
1558 function cflags(var)
1559 {
1560         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
1561                 removed[var] = 1
1562                 return 0
1563         }
1564
1565         if (!/!\?debug/)
1566                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
1567         return 1
1568 }
1569
1570 function unify_url(url)
1571 {
1572
1573         # sourceforge urls
1574         # Docs about sourceforge mirror system: http://sourceforge.net/docs/B05/
1575         sub("[?&]big_mirror=.*$", "", url);
1576         sub("[?&]modtime=.*$", "", url);
1577         sub("[?]use_mirror=.*$", "", url);
1578         sub("[?]download$", "", url);
1579
1580         sub("^http://prdownloads\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1581         sub("^http://download\.sf\.net/", "http://dl.sourceforge.net/", url)
1582         sub("^http://download\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1583         sub("^http://downloads\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1584
1585         sub("^http://.*\.dl\.sourceforge\.net/", "http://dl.sourceforge.net/", url)
1586         sub("^http://dl\.sourceforge\.net/sourceforge/", "http://dl.sourceforge.net/", url)
1587         sub("^http://dl\.sf\.net/", "http://dl.sourceforge.net/", url)
1588
1589         sub("^ftp://ftp\.gnome\.org/", "http://ftp.gnome.org/", url)
1590         sub("^http://ftp\.gnome\.org/pub/gnome/", "http://ftp.gnome.org/pub/GNOME/", url)
1591
1592         # apache urls
1593         sub("^http://apache.zone-h.org/", "http://www.apache.org/dist/", url)
1594
1595         # gnu.org
1596         sub("^ftp://ftp.gnu.org/", "http://ftp.gnu.org/", url)
1597         sub("^http://ftp.gnu.org/pub/gnu/", "http://ftp.gnu.org/gnu/", url)
1598
1599         return url
1600 }
1601
1602 function demacroize(str)
1603 {
1604         if (mod_name) {
1605                 sub("%{mod_name}", mod_name, str);
1606         }
1607         if (name) {
1608                 sub("%{name}", name, str);
1609         }
1610         if (version) {
1611                 sub("%{version}", version, str);
1612         }
1613         if (_beta) {
1614                 sub("%{_beta}", _beta, str);
1615         }
1616         if (_rc) {
1617                 sub("%{_rc}", _rc, str);
1618         }
1619         if (_pre) {
1620                 sub("%{_pre}", _pre, str);
1621         }
1622         if (_snap) {
1623                 sub("%{_snap}", _snap, str);
1624         }
1625         if (subver) {
1626                 sub("%{subver}", subver, str);
1627         }
1628         return str;
1629 }
1630
1631 function kill_preamble_macros()
1632 {
1633         if ($1 ~ /^Obsoletes:/) {
1634                 # NB! assigning $2 a value breaks tabbing
1635                 $2 = demacroize($2);
1636         }
1637         if ($1 ~ /^URL:/) {
1638                 # NB! assigning $2 a value breaks tabbing
1639                 $2 = demacroize($2);
1640                 $2 = unify_url($2)
1641         }
1642 }
1643
1644 function get_epoch(pkg, ver,    epoch)
1645 {
1646         return
1647 # should parse the BR lines more adequately:
1648 #       freetype = 2.0.0 -> correct
1649 #       freetype = 2.1.9 -> with epoch 1, as epoch 1 was added in 2.1.7
1650
1651         shell = "grep -o '^" pkg ":[^:]\+' ../PLD-doc/BuildRequires.txt | awk '{print $NF}'";
1652         shell | getline epoch;
1653         return epoch;
1654 }
1655
1656 function format_requires(tag, value,    n, p, i, deps, ndeps) {
1657         # skip any formatting for commented out items or some weird macros
1658         if (/^#/ || /%\(/) {
1659                 return tag "\t" value
1660         }
1661         n = split(value, p, / *,? */);
1662         for (i = 1; i <= n; i++) {
1663                 if (p[i+1] ~ /[<=>]/) {
1664                         # add epoch if the version doesn't have it but BuildRequires.txt has
1665                         if (p[i] ~ /^[a-z]/ && p[i+2] !~ /^[0-9]+:/) {
1666                                 epoch = get_epoch(p[i], p[i+2])
1667                                 if (epoch) {
1668                                         p[i+2] = epoch ":" p[i+2];
1669                                 }
1670                         }
1671                         deps[ndeps++] = p[i] " " p[i+1] " " p[i+2];
1672                         i += 2;
1673                 } else {
1674                         deps[ndeps++] = p[i];
1675                 }
1676         }
1677         s = ""
1678         for (i in deps) {
1679                 s = s sprintf("%s\t%s\n", tag, deps[i]);
1680         }
1681         return substr(s, 1, length(s)-1);
1682 }
1683
1684 function use_tabs()
1685 {
1686         # reverse vim: ts=4 sw=4 et
1687         gsub(/    /, "\t");
1688 }
1689
1690 function add_br(br)
1691 {
1692         BR[BR_count++] = br
1693 }
1694
1695 function replace_requires()
1696 {
1697
1698         # jpackages
1699         sub(/^java-devel$/, "jdk", $2);
1700         sub(/^log4j$/, "java-log4j", $2);
1701         sub(/^logging-log4j$/, "java-log4j", $2);
1702         sub(/^jakarta-log4j$/, "java-log4j", $2);
1703         sub(/^oro$/, "java-oro", $2);
1704         sub(/^jakarta-oro$/, "java-oro", $2);
1705         sub(/^jakarta-ant$/, "ant", $2);
1706         sub(/^xerces-j2$/, "java-xerces", $2);
1707         sub(/^xerces-j$/, "java-xerces", $2);
1708         sub(/^ldapjdk$/, "ldapsdk", $2);
1709         sub(/^saxon-scripts$/, "saxon", $2);
1710         sub(/^xalan-j2$/, "java-xalan", $2);
1711         sub(/^xalan-j$/, "java-xalan", $2);
1712         sub(/^gnu-regexp$/, "java-gnu-regexp", $2);
1713         sub(/^gnu.regexp$/, "java-gnu-regexp", $2);
1714         sub(/^jakarta-commons-httpclient$/, "java-commons-httpclient", $2);
1715         sub(/^xml-commons-resolver$/, "java-xml-commons-resolver", $2);
1716         sub(/^axis$/, "java-axis", $2);
1717         sub(/^wsdl4j$/, "java-wsdl4j", $2);
1718         sub(/^uddi4j$/, "java-uddi4j", $2);
1719         sub(/^hamcrest$/, "java-hamcrest", $2);
1720
1721         # redhat virtual
1722         sub(/^tftp-server$/, "tftpdaemon", $2);
1723
1724         sub(/^gcc-c\+\+$/, "libstdc++-devel", $2);
1725         sub(/^chkconfig$/, "/sbin/chkconfig", $2);
1726
1727         # fedora
1728         sub(/^iscsi-initiator-utils$/, "open-iscsi", $2);
1729
1730         replace_php_virtual_deps()
1731 }
1732
1733 # php virtual deps as discussed in devel-en
1734 function replace_php_virtual_deps()
1735 {
1736         pkg = $2
1737 #       if (pkg == "php-program") {
1738 #               $0 = $1 "\t/usr/bin/php"
1739 #               return
1740 #       }
1741
1742 #       if (pkg ~ /^php-[a-z]/ && pkg !~ /^php-(pear|common|cli|devel|fcgi|cgi|dirs|program|pecl-)/) {
1743 #               sub(/^php-/, "php(", pkg);
1744 #               sub(/$/, ") # verify this correctness -- it may be wanted to use specific not virtual dep", pkg);
1745 #               $2 = pkg
1746 #       }
1747
1748         if (pkg ~/^php$/) {
1749                 $2 = "webserver(php)";
1750                 if ($4 ~ /^[0-9]:/) {
1751                         $4 = substr($4, 3);
1752                 }
1753         }
1754
1755         if (pkg ~/^php4$/) {
1756                 $2 = "webserver(php)";
1757                 if ($4 ~ /^[0-9]:/) {
1758                         $4 = substr($4, 3);
1759                 }
1760         }
1761 }
1762
1763 # Load rpm macros
1764 # you should update the list also in adapter when making changes here
1765 function import_rpm_macros() {
1766         # File with rpm groups
1767         topdir = ENVIRON["_topdir"]
1768
1769         if (!topdir) {
1770                 print "adapter.awk should not not be invoked directly, but via adapter script" > "/dev/stderr"
1771                 do_not_touch_anything = 1
1772                 exit(1);
1773         }
1774
1775         # get cvsaddress for changelog section
1776         # using rpm macros as too lazy to add ~/.adapterrc parsing support.
1777         _cvsmaildomain = ENVIRON["_cvsmaildomain"]
1778         _cvsmailfeedback = ENVIRON["_cvsmailfeedback"]
1779
1780         prefix = ENVIRON["_prefix"]
1781         bindir = ENVIRON["_bindir"]
1782         sbindir = ENVIRON["_sbindir"]
1783         libdir = ENVIRON["_libdir"]
1784         sysconfdir = ENVIRON["_sysconfdir"]
1785         datadir = ENVIRON["_datadir"]
1786         includedir = ENVIRON["_includedir"]
1787         mandir = ENVIRON["_mandir"]
1788         infodir = ENVIRON["_infodir"]
1789         examplesdir = ENVIRON["_examplesdir"]
1790         docdir = ENVIRON["_defaultdocdir"]
1791         kdedocdir = ENVIRON["_kdedocdir"]
1792         gtkdocdir = ENVIRON["_gtkdocdir"]
1793         desktopdir = ENVIRON["_desktopdir"]
1794         pixmapsdir = ENVIRON["_pixmapsdir"]
1795         javadir = ENVIRON["_javadir"]
1796
1797         perl_sitearch = ENVIRON["perl_sitearch"]
1798         perl_archlib = ENVIRON["perl_archlib"]
1799         perl_privlib = ENVIRON["perl_privlib"]
1800         perl_vendorlib = ENVIRON["perl_vendorlib"]
1801         perl_vendorarch = ENVIRON["perl_vendorarch"]
1802         perl_sitelib = ENVIRON["perl_sitelib"]
1803
1804         py_sitescriptdir = ENVIRON["py_sitescriptdir"]
1805         py_sitedir = ENVIRON["py_sitedir"]
1806         py_scriptdir = ENVIRON["py_scriptdir"]
1807         py_ver = ENVIRON["py_ver"]
1808
1809         ruby_archdir = ENVIRON["ruby_archdir"]
1810         ruby_ridir = ENVIRON["ruby_ridir"]
1811         ruby_rubylibdir = ENVIRON["ruby_rubylibdir"]
1812         ruby_sitearchdir = ENVIRON["ruby_sitearchdir"]
1813         ruby_sitelibdir = ENVIRON["ruby_sitelibdir"]
1814         ruby_rdocdir = ENVIRON["ruby_rdocdir"]
1815
1816         php_pear_dir = ENVIRON["php_pear_dir"]
1817         php_data_dir = ENVIRON["php_data_dir"]
1818         tmpdir = ENVIRON["tmpdir"]
1819 }
1820
1821 function replace_groupnames(group) {
1822         group = replace(group, "Amusements/Games/Strategy/Real Time", "X11/Applications/Games/Strategy");
1823         group = replace(group, "Application/Multimedia", "Applications/Multimedia");
1824         group = replace(group, "Application/System", "Applications/System");
1825         group = replace(group, "Applications/Compilers", "Development/Languages");
1826         group = replace(group, "Applications/Daemons", "Daemons");
1827         group = replace(group, "Applications/Internet", "Applications/Networking");
1828         group = replace(group, "Applications/Internet/Peer to Peer", "Applications/Networking");
1829         group = replace(group, "Applications/Productivity", "X11/Applications");
1830         group = replace(group, "Database", "Applications/Databases");
1831         group = replace(group, "Development/C", "Development/Libraries");
1832         group = replace(group, "Development/Code Generators", "Development");
1833         group = replace(group, "Development/Docs", "Documentation");
1834         group = replace(group, "Development/Documentation", "Documentation");
1835         group = replace(group, "Development/Java", "Development/Languages/Java");
1836         group = replace(group, "Development/Languages/Other", "Development/Languages");;
1837         group = replace(group, "Development/Languages/Ruby", "Development/Languages");
1838         group = replace(group, "Development/Libraries/C and C++", "Development/Libraries");
1839         group = replace(group, "Development/Libraries/Java", "Development/Languages/Java");
1840         group = replace(group, "Development/Libraries/Python", "Development/Languages/Python");
1841         group = replace(group, "Development/Libraries/TCL", "Development/Languages/Tcl");;
1842         group = replace(group, "Development/Other", "Development");
1843         group = replace(group, "Development/Python", "Development/Languages/Python");
1844         group = replace(group, "Development/Testing", "Development");
1845         group = replace(group, "Emulators", "Applications/Emulators");
1846         group = replace(group, "Games", "Applications/Games");
1847         group = replace(group, "Library/Development", "Development/Libraries");
1848         group = replace(group, "Networking/Deamons", "Networking/Daemons");
1849         group = replace(group, "Productivity/Databases/Servers", "Applications/Databases");
1850         group = replace(group, "Productivity/Networking/Web/Servers", "Networking/Daemons/HTTP");;
1851         group = replace(group, "Shells", "Applications/Shells");
1852         group = replace(group, "System Environment/Base", "Base");
1853         group = replace(group, "System Environment/Daemons", "Daemons");
1854         group = replace(group, "System Environment/Kernel", "Base/Kernel");
1855         group = replace(group, "System Environment/Libraries", "Libraries");
1856         group = replace(group, "System", "Base");
1857         group = replace(group, "System/Base", "Base");
1858         group = replace(group, "System/Kernel and hardware", "Base/Kernel");
1859         group = replace(group, "System/Libraries", "Libraries");
1860         group = replace(group, "System/Servers", "Daemons");
1861         group = replace(group, "Text Processing/Markup/HTML", "Applications/Text");
1862         group = replace(group, "Text Processing/Markup/XML", "Applications/Text");
1863         group = replace(group, "Web/Database", "Applications/WWW");
1864         group = replace(group, "X11/GNOME", "X11/Applications");
1865         group = replace(group, "X11/GNOME/Applications", "X11/Applications");
1866         group = replace(group, "X11/GNOME/Development/Libraries", "X11/Development/Libraries");
1867         group = replace(group, "X11/Games", "X11/Applications/Games");
1868         group = replace(group, "X11/Games/Strategy", "X11/Applications/Games/Strategy");
1869         group = replace(group, "X11/Library", "X11/Libraries");
1870         group = replace(group, "X11/Utilities", "X11/Applications");
1871         group = replace(group, "X11/XFree86", "X11");
1872         group = replace(group, "X11/Xserver", "X11/Servers");
1873
1874         return group;
1875 }
1876
1877 # vim:ts=4:sw=4
This page took 0.178089 seconds and 3 git commands to generate.