]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- precise match for sections with predefined section names (singleword macros confuse...
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.27. Adapter adapts .spec files for PLD.
4 #
5 # Copyright (C) 1999-2003 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 #       Elan Ruusamäe <glen@pld-linux.org>
13 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
14
15 # TODO
16 # - parse ../PLD-doc/BuildRequires.txt and setup proper BR epoches?
17 # - add "-nc" option to skip CVS interaction
18 # - sort Summary(XX)
19 # - sort Requires, BuildRequires
20 # - check if %description (lang=C) contains 8bit
21 # - desc wrapping is totally fucked up on global.spec,1.25, dosemu.spec,1.115-
22
23 BEGIN {
24         SECTIONS = "^%(build|changelog|clean|description|install|post|posttrans|postun|pre|prep|pretrans|preun|triggerin|triggerpostun|triggerun)"
25
26         preamble = 1            # Is it part of preamble? Default - yes
27         boc = 4                 # Beginning of %changelog
28         bod = 0                 # Beginning of %description
29         tw = 70                 # Descriptions width
30
31         b_idx = 0               # index of BR/R arrays
32
33         # If variable removed, then 1 (for removing it from export)
34         removed["LDFLAGS"] = 0
35         removed["CFLAGS"] = 0
36         removed["CXXFLAGS"] = 0
37
38         # get cvsaddress for changelog section
39         # using rpm macros as too lazy to add ~/.adapterrc parsing support.
40         "rpm --eval '%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'" | getline _cvsmaildomain
41         "rpm --eval '%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'" | getline _cvsmailfeedback
42
43         # If 1, we are inside of comment block (started with /^#%/)
44         comment_block = 0
45
46         # File with rpm groups
47         "rpm --eval %_sourcedir" | getline groups_file
48         groups_file = groups_file "/rpm.groups"
49         system("cd `rpm --eval %_sourcedir`; [ -f rpm.groups ] || cvs up rpm.groups >/dev/null")
50
51         # Temporary file for changelog section
52         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
53
54         # Load rpm macros
55         "rpm --eval %_prefix"   | getline prefix
56         "rpm --eval %_bindir"   | getline bindir
57         "rpm --eval %_sbindir"  | getline sbindir
58         "rpm --eval %_libdir"   | getline libdir
59         "rpm --eval %_sysconfdir" | getline sysconfdir
60         "rpm --eval %_datadir"  | getline datadir
61         "rpm --eval %_includedir" | getline includedir
62         "rpm --eval %_mandir"   | getline mandir
63         "rpm --eval %_infodir"  | getline infodir
64         "rpm --eval %_examplesdir"      | getline examplesdir
65
66         "rpm --eval %perl_sitearch" | getline perl_sitearch
67         "rpm --eval %perl_archlib" | getline perl_archlib
68         "rpm --eval %perl_privlib" | getline perl_privlib
69         "rpm --eval %perl_vendorlib" | getline perl_vendorlib
70         "rpm --eval %perl_vendorarch" | getline perl_vendorarch
71         "rpm --eval %perl_sitelib" | getline perl_sitelib
72
73         "rpm --eval %py_sitescriptdir" | getline py_sitescriptdir
74 }
75
76 # There should be a comment with CVS keywords on the first line of file.
77 FNR == 1 {
78         if (!/# \$Revision:/)   # If this line is already OK?
79                 print "# $" "Revision:$, " "$" "Date:$" # No
80         else {
81                 print $0                                # Yes
82                 next            # It is enough for first line
83         }
84 }
85
86 # If the latest line matched /%files/
87 defattr == 1 {
88         if (ENVIRON["SKIP_DEFATTR"] != 1) {
89                 if ($0 !~ /defattr/) {  # If no %defattr
90                         print "%defattr(644,root,root,755)"     # Add it
91                 } else {
92                         $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
93                 }
94         }
95         defattr = 0
96 }
97
98 function b_makekey(a, b,        s) {
99         s = a "" b;
100         # kill bcond
101     gsub(/[#%]+{[!?]+[_a-zA-Z0-9]+:/, "", s);
102         # kill commented out items
103     gsub(/[# \t]*/, "", s);
104         return s;
105 }
106
107 # sort BR/R!
108 #
109 # NOTES:
110 # - mixing BR/R and anything else confuses this (all will be sorted together)
111 #   so don't do that.
112 # - comments leading the BR/R can not be associated,
113 #   so don't adapterize when the BR/R are mixed with comments
114 ENVIRON["SKIP_SORTBR"] != 1 && preamble == 1 && /(PreReq|(Build)?Requires)/, /(PreReq|(Build)?Requires)/ {
115         if ($1 ~ /PreReq:/) {
116                 sub(/PreReq:/, "Requires:", $1);
117         }
118         format_preamble()
119
120         b_idx++;
121         l = substr($0, index($0, $2));
122         b_ktmp = b_makekey($1, l);
123         b_key[b_idx] = b_ktmp;
124         b_val[b_ktmp] = $0;
125
126         next;
127 }
128
129 /^%bcond_/ {
130         # do nothing
131         print
132         next
133 }
134
135 preamble == 1 {
136         if (b_idx > 0) {
137                 isort(b_key, b_idx);
138                 for (i = 1; i <= b_idx; i++) {
139                         print "" b_val[b_key[i]];
140                 }
141                 b_idx = 0
142         }
143 }
144
145 # Comments
146 /^#/ && (description == 0) {
147         if (/This file does not like to be adapterized!/) {
148                 print                   # print this message
149                 while (getline)         # print the rest of spec as it is
150                         print
151                 do_not_touch_anything = 1 # do not touch anything in END()
152                 exit 0
153         }
154
155         # Generally, comments are printed without touching
156         sub(/[ \t]+$/, "")
157         print $0
158         next
159 }
160
161 /^%define/ {
162         # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
163         if ($2 == "_applnkdir") {
164                 next
165         }
166         if ($2 == "date") {
167                 date = 1
168         }
169
170         # Do not add %define of _prefix if it already is.
171         if ($2 ~ /^_prefix/) {
172                 sub("^"prefix, $3, bindir)
173                 sub("^"prefix, $3, sbindir)
174                 sub("^"prefix, $3, libdir)
175                 sub("^"prefix, $3, datadir)
176                 sub("^"prefix, $3, includedir)
177                 prefix = $3
178         }
179
180         if ($2 ~ /_bindir/ && !/_sbindir/)
181                 bindir = $3
182         if ($2 ~ /_sbindir/)
183                 sbindir = $3
184         if ($2 ~ /_libdir/)
185                 libdir = $3
186         if ($2 ~ /_sysconfdir/) {
187                 if ($3 ~ /^%\(/) {
188                         # TODO: should escape for latter checks like: ($c ~ sysconfdir "/{?cron.")
189                         sysconfdir = "%%%%%%%%%%%%%%"
190                 } else {
191                         sysconfdir = $3
192                 }
193         }
194         if ($2 ~ /_datadir/)
195                 datadir = $3
196         if ($2 ~ /_includedir/)
197                 includedir = $3
198         if ($2 ~ /_mandir/)
199                 mandir = $3
200         if ($2 ~ /_infodir/)
201                 infodir = $3
202
203         # version related macros
204         if ($2 ~ /^_beta$/)
205                 _beta = $3
206         if ($2 ~ /^_rc$/)
207                 _rc = $3
208         if ($2 ~ /^_snap$/)
209                 _snap = $3
210
211         # these are used usually when adapterizing external spec
212         if ($2 ~ /^name$/)
213                 name = $3
214         if ($2 ~ /^version$/)
215                 version = $3
216         if ($2 ~ /^release$/)
217                 release = $3
218
219         # do nothing further, otherwise adapter thinks we're at preamble
220         print
221         next
222 }
223
224 # Obsolete
225 /^%include.*\/usr\/lib\/rpm\/macros\.python$/ {
226         next
227 }
228
229 ################
230 # %description #
231 ################
232 /^%description/, (!/^%description/ && $0 ~ SECTIONS) {
233         preamble = 0
234
235         if (/^%description/) {
236                 bod++
237                 format_line = ""
238                 format_indent = -1
239         }
240
241         # Format description
242         if (ENVIRON["SKIP_DESC"] != 1 && description == 1 && !/^%[a-z]+/ && !/^%description/) {
243                 if (/^[ \t]*$/) {
244                         format_flush(format_line, format_indent)
245                         print ""
246                         format_line = ""
247                         format_indent = -1
248                 } else if (/^[ \t]*[-\*][ \t]*/) {
249                         format_flush(format_line, format_indent)
250                         match($0, /^[ \t]*/)
251                         format_indent = RLENGTH
252                         match($0, /^[ \t]*[-\*][ \t]/)
253                         format_line = substr($0, RLENGTH)
254                 } else
255                         format_line = format_line " " $0
256                 next
257         }
258
259         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
260                 if (NF > 3 && $2 == "-l") {
261                         ll = $1
262                         for (i = 4; i <= NF; i++)
263                                 ll = ll " " $i
264                         $0 = ll " -l " $3
265                 }
266                 format_flush(format_line, format_indent)
267                 if (bod == 2) {
268                         bod = 1
269                         description = 1
270                 } else {
271                         bod = 0
272                         description = 0
273                 }
274         } else
275                 description = 1
276 }
277
278 #########
279 # %prep #
280 #########
281 /^%prep/, (!/^%prep/ && $0 ~ SECTIONS) {
282         preamble = 0
283
284         use_macros()
285
286         # Add '-q' to %setup
287         if (/^%setup/ && !/-q/) {
288                 sub(/^%setup/, "%setup -q")
289         }
290
291         if (/^%setup/) {
292                 $0 = fixedsub(name, "%{name}", $0);
293                 $0 = fixedsub(version, "%{version}", $0);
294                 if (_beta) {
295                         $0 = fixedsub(_beta, "%{_beta}", $0);
296                 }
297                 if (_rc) {
298                         $0 = fixedsub(_rc, "%{_rc}", $0);
299                 }
300                 if (_snap) {
301                         $0 = fixedsub(_snap, "%{_snap}", $0);
302                 }
303         }
304
305         if (/^%setup/ && /-n %{name}-%{version}( |$)/) {
306                 $0 = fixedsub(" -n %{name}-%{version}", "", $0)
307         }
308
309         # invalid in %prep
310         sub("^rm -rf \$RPM_BUILD_ROOT.*", "");
311 }
312
313 ##########
314 # %build #
315 ##########
316 /^%build/, (!/^%build/ && $0 ~ SECTIONS) {
317         preamble = 0
318
319         use_macros()
320
321         if (/^automake$/)
322                 sub(/$/, " -a -c")
323
324         if (/LDFLAGS/) {
325                 if (/LDFLAGS="-s"/) {
326                         removed["LDFLAGS"] = 1
327                         next
328                 } else {
329                         split($0, tmp, "LDFLAGS=")
330                         count = split(tmp[2], flags, "\"")
331                         if (flags[1] != "" && flags[1] !~ "!?debug") {
332                                 sub(/-s[" ]?/, "%{rpmldflags} ", flags[1])
333                                 $0 = tmp[1] line[1] "LDFLAGS=" flags[1] "\""
334                                 for (i = 2; i < count; i++)
335                                         $0 = $0 flags[i] "\""
336                         }
337                 }
338         }
339
340         if (/CFLAGS=/)
341                 if (cflags("CFLAGS") == 0)
342                         next
343
344         if (/CXXFLAGS=/)
345                 if (cflags("CXXFLAGS") == 0)
346                         next
347
348         if (/^export /) {
349                 if (removed["LDFLAGS"])
350                         sub(" LDFLAGS", "")
351                 if (removed["CFLAGS"])
352                         sub(" CFLAGS", "")
353                 if (removed["CXXFLAGS"])
354                         sub(" CXXFLAGS", "")
355                 # Is there still something?
356                 if (/^export[ ]*$/)
357                         next
358         }
359
360         # quote CC
361         if (/CC=%{__cc} /) {
362                 sub("CC=%{__cc}", "CC=\"%{__cc}\"")
363         }
364         
365         # use PLD Linux macros
366         $0 = fixedsub("glib-gettextize --copy --force","%{__glib_gettextize}", $0);
367         $0 = fixedsub("intltoolize --copy --force", "%{__intltoolize}", $0);
368         $0 = fixedsub("automake --add-missing --copy", "%{__automake}", $0);
369
370         sub(/^aclocal$/, "%{__aclocal}");
371         sub(/^autoheader$/, "%{__autoheader}");
372         sub(/^autoconf$/, "%{__autoconf}");
373         sub(/^automake$/, "%{__automake}");
374
375         # atrpms
376         $0 = fixedsub("%perl_configure", "%{__perl} Makefile.PL \\\n\tINSTALLDIRS=vendor", $0);
377         $0 = fixedsub("%perl_makecheck", "%{?with_tests:%{__make} test}", $0);
378 }
379
380 ##########
381 # %clean #
382 ##########
383 /^%clean/, (!/^%clean/ && $0 ~ SECTIONS) {
384         did_clean = 1
385
386         # prevent next section header like "%post -p /sbin/ldconfig" being adapterized
387         if (!/^%/) {
388                 use_macros()
389         }
390 }
391
392 ############
393 # %install #
394 ############
395 /^%install/, (!/^%install/ && $0 ~ SECTIONS) {
396
397         preamble = 0
398
399         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+\${?RPM_BUILD_ROOT}?/ && did_rmroot==0) {
400                 did_rmroot=1
401                 print "rm -rf $RPM_BUILD_ROOT"
402                 next
403         }
404
405         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
406                 print "rm -rf $RPM_BUILD_ROOT"
407                 did_rmroot=1
408         }
409
410         use_macros()
411
412         # 'install -d' instead 'mkdir -p'
413         if (/mkdir -p/)
414                 sub(/mkdir -p/, "install -d")
415
416         # 'install' instead 'cp -p'
417         if (/cp -p\b/)
418                 sub(/cp -p/, "install")
419
420         # No '-u root' or '-g root' for 'install'
421         if (/^install/ && /-[ug][ \t]*root/)
422                 gsub(/-[ug][ \t]*root /, "")
423
424         if (/^install/ && /-m[ \t]*[0-9]+/)
425                 gsub(/-m[ \t]*[0-9]+ /, "")
426
427         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
428         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
429                 next
430
431         # No lines contain 'chmod' if it sets the modes to '644'
432         if ($1 ~ /chmod/ && $2 ~ /644/)
433                 next
434
435         # foreign rpms
436         $0 = fixedsub("%buildroot", "$RPM_BUILD_ROOT", $0)
437         $0 = fixedsub("%{buildroot}", "$RPM_BUILD_ROOT", $0)
438
439         # atrpms
440         $0 = fixedsub("%perl_makeinstall", "%{__make} pure_install \\\n\tDESTDIR=$RPM_BUILD_ROOT", $0);
441 }
442
443 ##########
444 # %files #
445 ##########
446 /^%files/, (!/^%files/ && $0 ~ SECTIONS) {
447         preamble = 0
448
449         if ($0 ~ /^%files/)
450                 defattr = 1
451
452         use_files_macros()
453 }
454
455 ##############
456 # %changelog #
457 ##############
458 /^%changelog/, (!/^%changelog/ && $0 ~ SECTIONS) {
459         preamble = 0
460         has_changelog = 1
461         skip = 0
462         # There should be some CVS keywords on the first line of %changelog.
463         if (boc == 3) {
464                 if ($0 !~ _cvsmailfeedback)
465                         print "* %{date} " _cvsmailfeedback > changelog_file
466                 else
467                         skip = 1
468                 boc = 2
469         }
470         if (boc == 2 && !skip) {
471                 if (!/All persons listed below/) {
472                         printf "All persons listed below can be reached at " > changelog_file
473                         print "<cvs_login>" _cvsmaildomain "\n" > changelog_file
474                 } else
475                         skip = 1
476                 boc = 1
477         }
478         if (boc == 1 && !skip) {
479                 if (!/^$/) {
480                         if (!/\$.*Log:.*\$/)
481                                 print "$" "Log:$" > changelog_file
482                         boc = 0
483                 }
484         }
485         # Define date macro.
486         if (boc == 4) {
487                 if (date == 0) {
488                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
489                         print " date +\"%a %b %d %Y\"`)" > changelog_file
490                         date = 1
491                 }
492                 boc = 3
493         }
494
495         sub(/[ \t]+$/, "")
496         if (!/^%[a-z]+$/ || /changelog/)
497                 print > changelog_file
498         else
499                 print
500         next
501 }
502
503 ###########
504 # SCRIPTS #
505 ###########
506 /^%pre/, (!/^%pre/ && $0 ~ SECTIONS) {
507         preamble = 0
508
509         # %useradd and %groupadd may not be wrapped
510         if (/%(useradd|groupadd).*\\$/) {
511                 a = $0; getline;
512                 sub(/^[\s\t]*/, "");
513                 $0 = substr(a, 1, length(a) - 1) $0;
514         }
515 }
516
517 /^%post/, (!/^%post/ && $0 ~ SECTIONS) {
518         preamble = 0
519 }
520 /^%preun/, (!/^%preun/ && $0 ~ SECTIONS) {
521         preamble = 0
522 }
523 /^%postun/, (!/^%postun/ && $0 ~ SECTIONS) {
524         preamble = 0
525 }
526 /^%triggerin/, (!/^%triggerin/ && $0 ~ SECTIONS) {
527         preamble = 0
528 }
529 /^%triggerun/, (!/^%triggerun/ && $0 ~ SECTIONS) {
530         preamble = 0
531 }
532 /^%triggerpostun/, (!/^%triggerpostun/ && $0 ~ SECTIONS) {
533         preamble = 0
534 }
535 /^%pretrans/, (!/^%pretrans/ && $0 ~ SECTIONS) {
536         preamble = 0
537 }
538 /^%posttrans/, (!/^%posttrans/ && $0 ~ SECTIONS) {
539         preamble = 0
540 }
541
542 #############
543 # PREAMBLES #
544 #############
545 preamble == 1 {
546         # There should not be a space after the name of field
547         # and before the colon.
548         sub(/[ \t]*:/, ":")
549
550         if (/^%perl_module_wo_prefix/) {
551                 name = $2
552                 version = $3
553                 release = "0." fixedsub(".%{disttag}.at", "", $4)
554         }
555
556         field = tolower($1)
557         fieldnlower = $1
558         if (field ~ /summary:/) {
559                 sub(/\.$/, "", $0);
560         }
561         if (field ~ /group(\([^)]+\)):/)
562                 next
563         if (field ~ /group:/) {
564                 format_preamble()
565                 sub(/^Utilities\//,"Applications/",$2)
566                 sub(/^Games/,"Applications/Games",$2)
567                 sub(/^X11\/Games/,"X11/Applications/Games",$2)
568                 sub(/^X11\/GNOME\/Development\/Libraries/,"X11/Development/Libraries",$2)
569                 sub(/^X11\/GNOME\/Applications/,"X11/Applications",$2)
570                 sub(/^X11\/GNOME/,"X11/Applications",$2)
571                 sub(/^X11\/Utilities/,"X11/Applications",$2)
572                 sub(/^X11\/Games\/Strategy/,"X11/Applications/Games/Strategy",$2)
573                 sub(/^Shells/,"Applications/Shells",$2)
574
575                 sub(/^[^ \t]*[ \t]*/,"")
576                 Grupa = $0
577
578                 sub(/^System Environment\/Libraries$/, "Libraries", Grupa)
579                 sub(/^System Environment\/Daemons$/, "Daemons", Grupa)
580                 sub(/^Applications\/Internet$/, "Applications/Networking", Grupa)
581                 sub(/^Applications\/Daemons$/, "Daemons", Grupa)
582                 sub(/^Application\/Multimedia$/, "Applications/Multimedia", Grupa)
583                 sub(/^System\/Servers$/, "Daemons", Grupa)
584                 sub(/^X11\/Xserver$/, "X11/Servers", Grupa)
585                 sub(/^X11\/XFree86/, "X11", Grupa)
586                 sub(/^Applications\/Compilers$/, "Development/Languages", Grupa)
587                 sub(/^Applications\/Internet\/Peer to Peer/, "Applications/Networking", Grupa)
588                 sub(/^Networking\/Deamons$/, "Networking/Daemons", Grupa)
589                 sub(/^Development\/Docs$/, "Documentation", Grupa)
590
591                 print "Group:\t\t" Grupa
592                 if (Grupa ~ /^X11/ && x11 == 0) # Is it X11 application?
593                         x11 = 1
594
595                 byl_plik_z_grupami = 0
596                 byl_opis_grupy = 0
597                 while ((getline linia_grup < groups_file) > 0) {
598                         byl_plik_z_grupami = 1
599                         if (linia_grup == Grupa) {
600                                 byl_opis_grupy = 1
601                                 break
602                         }
603                 }
604
605                 if (!byl_plik_z_grupami)
606                         print "######\t\t" groups_file ": no such file"
607                 else if (!byl_opis_grupy)
608                         print "######\t\t" "Unknown group!"
609
610                 close(groups_file)
611                 next
612         }
613
614         if (field ~ /prereq:/) {
615                 sub(/PreReq:/, "Requires:", $1);
616         }
617
618         # split (build)requires, obsoletes on commas
619         if (field ~ /(obsoletes|requires):/ && $0 ~ /,/) {
620                 l = substr($0, index($0, $2));
621                 n = split(l, p, / *, */);
622                 for (i in p) {
623                         printf("%s\t%s\n", $1, p[i]);
624                 }
625                 next;
626         }
627
628         if (field ~ /packager:|distribution:|docdir:|prefix:/)
629                 next
630
631         if (field ~ /buildroot:/)
632                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
633
634         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
635         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
636                 $1 = "License:"
637
638         if (field ~ /name:/) {
639                 if ($2 == "%{name}" && name) {
640                         $2 = name
641                 }
642                 name = $2
643                 name_seen = 1;
644         }
645
646         if (field ~ /version:/) {
647                 if ($2 == "%{version}" && version) {
648                         $2 = version
649                 }
650                 version = $2
651                 version_seen = 1;
652         }
653
654         if (field ~ /release:/) {
655                 if ($2 == "%{release}" && release) {
656                         $2 = release
657                 }
658                 release = $2
659                 release_seen = 1;
660         }
661
662         if (field ~ /serial:/)
663                 $1 = "Epoch:"
664
665         # proper caps
666         if (field ~ /^url:$/)
667                 $1 = "URL:"
668
669         # Use %{name} and %{version} in the filenames in "Source:"
670         if (field ~ /^source/ || field ~ /patch/) {
671                 n = split($2, url, /\//)
672                 if (url[n] ~ /\.gz$/) {
673                         url[n+1] = ".gz" url[n+1]
674                         sub(/\.gz$/,"",url[n])
675                 }
676                 if (url[n] ~ /\.zip$/) {
677                         url[n+1] = ".zip" url[n+1]
678                         sub(/\.zip$/,"",url[n])
679                 }
680                 if (url[n] ~ /\.tar$/) {
681                         url[n+1] = ".tar" url[n+1]
682                         sub(/\.tar$/,"",url[n])
683                 }
684                 if (url[n] ~ /\.patch$/) {
685                         url[n+1] = ".patch" url[n+1]
686                         sub(/\.patch$/,"",url[n])
687                 }
688                 if (url[n] ~ /\.bz2$/) {
689                         url[n+1] = ".bz2" url[n+1]
690                         sub(/\.bz2$/,"",url[n])
691                 }
692                 if (url[n] ~ /\.logrotate$/) {
693                         url[n+1] = ".logrotate" url[n+1]
694                         sub(/\.logrotate$/,"",url[n])
695                 }
696                 if (url[n] ~ /\.pamd$/) {
697                         url[n+1] = ".pamd" url[n+1]
698                         sub(/\.pamd$/,"",url[n])
699                 }
700
701                 # allow %{name} just in last url component
702                 s = ""
703                 for (i = 1; i <= n; i++) {
704                         url[i] = fixedsub("%{name}", name, url[i])
705                         if (s) {
706                                 s = s "/" url[i]
707                         } else {
708                                 s = url[i]
709                         }
710                 }
711                 $2 = s url[n+1]
712
713                 filename = url[n]
714                 if (name) {
715                         url[n] = fixedsub(name, "%{name}", url[n])
716                 }
717                 if (field ~ /source/) {
718                         if (version) {
719                                 url[n] = fixedsub(version, "%{version}", url[n])
720                         }
721                         if (_beta) {
722                                 url[n] = fixedsub(_beta, "%{_beta}", url[n])
723                         }
724                         if (_rc) {
725                                 url[n] = fixedsub(_rc, "%{_rc}", url[n])
726                         }
727                         if (_snap) {
728                                 url[n] = fixedsub(_snap, "%{_snap}", url[n])
729                         }
730                 }
731                 $2 = fixedsub(filename, url[n], $2)
732
733                 # sourceforge urls
734                 sub("[?]use_mirror=.*$", "", $2);
735                 sub("[?]download$", "", $2);
736                 sub("^http://prdownloads\.sourceforge\.net/", "http://dl.sourceforge.net/", $2)
737
738                 sub("^http://.*\.dl\.sourceforge\.net/", "http://dl.sourceforge.net/", $2)
739                 sub("^http://dl\.sourceforge\.net/sourceforge/", "http://dl.sourceforge.net/", $2)
740                 sub("^http://dl\.sf\.net/", "http://dl.sourceforge.net/", $2)
741         }
742
743
744         if (field ~ /^source:/)
745                 $1 = "Source0:"
746
747         if (field ~ /^patch:/)
748                 $1 = "Patch0:"
749
750         format_preamble()
751
752         if (field ~ /requires/) {
753                 # atrpms
754                 $0 = fixedsub("%{eversion}", "%{epoch}:%{version}-%{release}", $0);
755         }
756 }
757
758 # main() ;-)
759 {
760         preamble = 1
761
762         sub(/[ \t]+$/, "")
763         print
764
765         if (name_seen == 0 && name) {
766                 print "Name:\t" name
767                 name_seen = 1
768         }
769
770         if (version_seen == 0 && version) {
771                 print "Version:\t" version
772                 version_seen = 1
773         }
774
775         if (release_seen == 0 && release) {
776                 print "Release:\t" release
777                 release_seen = 1
778         }
779 }
780
781
782 END {
783         if (do_not_touch_anything)
784                 exit 0
785
786         close(changelog_file)
787         while ((getline < changelog_file) > 0)
788                 print
789         system("rm -f " changelog_file)
790
791         if (did_clean == 0) {
792                 print ""
793                 print "%clean"
794                 print "rm -rf $RPM_BUILD_ROOT"
795         }
796
797         if (date == 0) {
798                 print ""
799                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
800         }
801
802         if (has_changelog == 0)
803                 print "%changelog"
804
805         if (boc > 2)
806                 print "* %{date} PLD Team <feedback@pld-linux.org>"
807         if (boc > 1) {
808                 printf "All persons listed below can be reached at "
809                 print "<cvs_login>@pld-linux.org\n"
810         }
811         if (boc > 0)
812                 print "$" "Log:$"
813 }
814
815 function fixedsub(s1,s2,t, ind) {
816 # substitutes fixed strings (not regexps)
817         if (ind = index(t,s1))
818                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
819         return t
820 }
821
822 # There should be one or two tabs after the colon.
823 function format_preamble()
824 {
825         sub(/:[ \t]*/, ":")
826         if (match($0, /[A-Za-z0-9(),#_ \t]+[ \t]*:[ \t]*/) == 1) {
827                 if (RLENGTH < 8)
828                         sub(/:/, ":\t\t")
829                 else
830                         sub(/:/, ":\t")
831         }
832 }
833
834 # Replace directly specified directories with macros
835 function use_macros()
836 {
837         # -m, --skip-macros, --no-macros -- skip macros subst
838         if (ENVIRON["SKIP_MACROS"]) {
839                 return
840         }
841
842         gsub(perl_sitearch, "%{perl_sitearch}")
843         gsub(perl_archlib, "%{perl_archlib}")
844         gsub(perl_privlib, "%{perl_privlib}")
845         gsub(perl_vendorlib, "%{perl_vendorlib}")
846         gsub(perl_vendorarch, "%{perl_vendorarch}")
847         gsub(perl_sitelib, "%{perl_sitelib}")
848         
849         gsub(py_sitescriptdir, "%{py_sitescriptdir}")
850
851         gsub(bindir, "%{_bindir}")
852         gsub("%{prefix}/bin", "%{_bindir}")
853         if(prefix"/bin" == bindir)
854                 gsub("%{_prefix}/bin", "%{_bindir}")
855
856         for (c = 1; c <= NF; c++) {
857                 if ($c ~ sbindir "/fix-info-dir")
858                         continue;
859                 if ($c ~ sbindir "/webapp")
860                         continue;
861                 if ($c ~ sbindir "/chsh")
862                         continue;
863                 if ($c ~ sbindir "/usermod")
864                         continue;
865                 gsub(sbindir, "%{_sbindir}", $c)
866         }
867
868         gsub("%{prefix}/sbin", "%{_sbindir}")
869         if (prefix"/sbin" == sbindir)
870                 gsub("%{_prefix}/sbin", "%{_sbindir}")
871
872         for (c = 1; c <= NF; c++) {
873                 if ($c ~ sysconfdir "/{?cron.")
874                         continue;
875                 if ($c ~ sysconfdir "/{?crontab.d")
876                         continue;
877                 if ($c ~ sysconfdir "/{?env.d")
878                         continue;
879                 if ($c ~ sysconfdir "/{?logrotate.d")
880                         continue;
881                 if ($c ~ sysconfdir "/{?pam.d")
882                         continue;
883                 if ($c ~ sysconfdir "/{?profile.d")
884                         continue;
885                 if ($c ~ sysconfdir "/{?rc.d")
886                         continue;
887                 if ($c ~ sysconfdir "/{?security")
888                         continue;
889                 if ($c ~ sysconfdir "/{?skel")
890                         continue;
891                 if ($c ~ sysconfdir "/{?sysconfig")
892                         continue;
893                 if ($c ~ sysconfdir "/{?shrc.d")
894                         continue;
895                 if ($c ~ sysconfdir "/{?certs")
896                         continue;
897                 if ($c ~ sysconfdir "/{?X11")
898                         continue;
899                 if ($c ~ sysconfdir "/{?ld.so.conf.d")
900                         continue;
901                 if ($c ~ sysconfdir "/{?httpd") # temp
902                         continue;
903                 gsub(sysconfdir, "%{_sysconfdir}", $c)
904         }
905
906         for (c = 1; c <= NF; c++) {
907                 if ($c ~ datadir "/automake")
908                         continue;
909                 if ($c ~ datadir "/unsermake")
910                         continue;
911                 if ($c ~ datadir "/file/magic.mime")
912                         continue;
913                 gsub(datadir, "%{_datadir}", $c)
914         }
915
916         gsub("%_sbindir", "%{_sbindir}")
917         gsub("%_mandir", "%{_mandir}")
918         gsub("%name", "%{name}")
919
920         gsub("%{prefix}/share", "%{_datadir}")
921         if (prefix"/share" == datadir)
922                 gsub("%{_prefix}/share", "%{_datadir}")
923
924         # CFLAGS="-I/usr/include/ncurses is usually correct.
925         if (!/-I\/usr\/include/) {
926                 gsub(includedir, "%{_includedir}")
927         }
928
929         gsub("%{prefix}/include", "%{_includedir}")
930         if (prefix"/include" == includedir) {
931                 gsub("%{_prefix}/include", "%{_includedir}")
932         }
933
934         gsub(mandir, "%{_mandir}")
935         if ($0 !~ "%{_datadir}/manual") {
936                 gsub("%{_datadir}/man", "%{_mandir}")
937         }
938         gsub("%{_prefix}/share/man", "%{_mandir}")
939         gsub("%{prefix}/share/man", "%{_mandir}")
940         gsub("%{prefix}/man", "%{_mandir}")
941         gsub("%{_prefix}/man", "%{_mandir}")
942
943         gsub(infodir, "%{_infodir}")
944         gsub("%{prefix}/info", "%{_infodir}")
945         gsub("%{_prefix}/info", "%{_infodir}")
946
947         if (prefix !~ "/X11R6") {
948                 gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
949         }
950
951         gsub(examplesdir, "%{_examplesdir}")
952
953         if (prefix != "/") {
954                 # leave --with-foo=/usr alone
955                 if ($0 !~ "--with.*=.*" prefix) {
956                         for (c = 1; c <= NF; c++) {
957                                 if ($c ~ prefix "/sbin/fix-info-dir")
958                                         continue;
959                                 if ($c ~ prefix "/sbin/webapp")
960                                         continue;
961                                 if ($c ~ prefix "/sbin/chsh")
962                                         continue;
963                                 if ($c ~ prefix "/sbin/usermod")
964                                         continue;
965                                 if ($c ~ prefix "/share/automake")
966                                         continue;
967                                 if ($c ~ prefix "/share/unsermake")
968                                         continue;
969                                 if ($c ~ prefix "/lib/sendmail")
970                                         continue;
971                                 if ($c ~ prefix "/lib/pkgconfig")
972                                         continue;
973
974                                 # CFLAGS="-I/usr..." is usually correct.
975                                 if (/-I\/usr/)
976                                         continue;
977                                 # same for LDFLAGS="-L/usr..."
978                                 if (/-L\/usr/)
979                                         continue;
980
981                                 gsub(prefix, "%{_prefix}", $c)
982                         }
983                 }
984                 gsub("%{prefix}", "%{_prefix}")
985         }
986
987         gsub("%{PACKAGE_VERSION}", "%{version}")
988         gsub("%{PACKAGE_NAME}", "%{name}")
989
990         gsub("^make$", "%{__make}")
991         gsub("^make ", "%{__make} ")
992         gsub("^gcc ", "%{__cc} ")
993
994         # mandrake specs
995         gsub("^%make$", "%{__make}")
996         gsub("^%make ", "%{__make} ")
997         gsub("^%makeinstall_std", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
998         gsub("^%{makeinstall}", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
999         gsub("^%makeinstall", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1000         gsub("^%{__rm} -rf %{buildroot}", "rm -rf $RPM_BUILD_ROOT")
1001         gsub("^%{__install}", "install")
1002         gsub("^%{__rm}", "rm")
1003         gsub("%optflags", "%{rpmcflags}")
1004         gsub("%{compat_perl_vendorarch}", "%{perl_vendorarch}")
1005
1006         gsub("^%{__make} install DESTDIR=\$RPM_BUILD_ROOT", "%{__make} install \\\n\tDESTDIR=$RPM_BUILD_ROOT")
1007         gsub("^fix-info-dir$", "[ ! -x /usr/sbin/fix-info-dir ] || /usr/sbin/fix-info-dir -c %{_infodir} >/dev/null 2>\&1")
1008         $0 = fixedsub("%buildroot", "$RPM_BUILD_ROOT", $0)
1009         $0 = fixedsub("%{buildroot}", "$RPM_BUILD_ROOT", $0)
1010         $0 = fixedsub("CXXFLAGS=%{rpmcflags} %configure", "CXXFLAGS=%{rpmcflags}\n%configure", $0);
1011
1012         gsub("%_bindir", "%{_bindir}")
1013         gsub("%_datadir", "%{_datadir}")
1014         gsub("%_iconsdir", "%{_iconsdir}")
1015
1016         gsub("/usr/src/linux", "%{_kernelsrcdir}")
1017         gsub("%{_prefix}/src/linux", "%{_kernelsrcdir}")
1018 }
1019
1020
1021 # insertion sort of A[1..n]
1022 # copied from mawk manual
1023 function isort(A,n,             i,j,hold) {
1024         for (i = 2; i <= n; i++) {
1025                 hold = A[j = i]
1026                 while (A[j-1] > hold) {
1027                         j-- ; A[j+1] = A[j]
1028                 }
1029                 A[j] = hold
1030         }
1031         # sentinel A[0] = "" will be created if needed
1032 }
1033
1034
1035 function use_files_macros(      i, n, t, a)
1036 {
1037         use_macros()
1038
1039         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
1040         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
1041
1042         # uid/gid nobody is not valid in %files
1043         if (/%attr([^)]*nobody[^)]*)/ && !/FIXME/) {
1044                 $0 = $0 " # FIXME nobody user/group can't own files! -adapter.awk"
1045         }
1046
1047         # replace back
1048         gsub("%{_sysconfdir}/cron\.d", "/etc/cron.d")
1049         gsub("%{_sysconfdir}/crontab\.d", "/etc/crontab.d")
1050         gsub("%{_sysconfdir}/logrotate\.d", "/etc/logrotate.d")
1051         gsub("%{_sysconfdir}/pam\.d", "/etc/pam.d")
1052         gsub("%{_sysconfdir}/profile\.d", "/etc/profile.d")
1053         gsub("%{_sysconfdir}/rc\.d", "/etc/rc.d")
1054         gsub("%{_sysconfdir}/security", "/etc/security")
1055         gsub("%{_sysconfdir}/skel", "/etc/skel")
1056         gsub("%{_sysconfdir}/sysconfig", "/etc/sysconfig")
1057         gsub("%{_sysconfdir}/certs", "/etc/certs")
1058         gsub("%{_sysconfdir}/init.d", "/etc/init.d")
1059
1060         # /etc/init.d -> /etc/rc.d/init.d
1061         if (!/^\/etc\/init\.d$/) {
1062                  gsub("/etc/init.d", "/etc/rc.d/init.d")
1063         }
1064
1065         if (/\/etc\/rc\.d\/init\.d\// && !/functions/) {
1066                 if (!/%attr.*\/etc\/rc\.d\/init\.d/) {
1067                         $0 = "%attr(754,root,root) " $0
1068                 }
1069                 if (/^%attr.*\/etc\/rc\.d\/init\.d/ && !/^%attr\(754 *,/) {
1070                         gsub("^%attr\\(... *,", "%attr(754,");
1071                 }
1072         }
1073
1074         if (/lib.+\.so/ && !/\.so$/ && !/^%attr.*/ && !/%exclude/) {
1075                 $0 = "%attr(755,root,root) " $0
1076         }
1077
1078         # /etc/sysconfig files
1079         # %attr(640,root,root) %config(noreplace) %verify(not size mtime md5) /etc/sysconfig/*
1080         # attr not required, allow default 644 attr
1081         if (!/network-scripts/ && !/%dir/ && !/\.d$/ && !/functions/ && !/\/etc\/sysconfig\/wmstyle/) {
1082                 if (/\/etc\/sysconfig\// && /%config/ && !/%config\(noreplace\)/) {
1083                         gsub("%config", "%config(noreplace)")
1084                 }
1085
1086                 if (/\/etc\/sysconfig\// && !/%config\(noreplace\)/) {
1087                         $NF = "%config(noreplace) " $NF
1088                 }
1089
1090                 if (/\/etc\/sysconfig\// && /%attr\(755/) {
1091                         gsub("^%attr\\(... *,", "%attr(640,");
1092                 }
1093
1094                 if (/\/etc\/sysconfig\// && !/%verify/) {
1095                         gsub("/etc/sysconfig", "%verify(not size mtime md5) /etc/sysconfig");
1096                 }
1097         }
1098
1099         # kill leading zeros
1100         if (/%attr\(0[1-9]/) {
1101                 gsub("%attr\\(0", "%attr(")
1102         }
1103
1104         # sort %verify attrs
1105         if (match($0, /%verify\(not([^)]+)\)/)) {
1106                 t = substr($0, RSTART, RLENGTH)
1107                 # kill commas: %verify(not,md5,size,mtime)
1108                 gsub(/,/, " ", t);
1109
1110                 gsub(/^%verify\(not |\)$/, "", t)
1111                 n = split(t, a, / /)
1112                 isort(a, n)
1113
1114                 s = "%verify(not"
1115                 for (i = 1 ; i <= n; i++) {
1116                         s = s " " a[i]
1117                 }
1118                 s = s ")"
1119
1120                 gsub(/%verify\(not[^)]+\)/, s)
1121         }
1122
1123         if (/%{_mandir}/) {
1124                 gsub("\.gz$", "*")
1125         }
1126
1127         # locale dir and no %lang -> bad
1128         if (/%{_datadir}\/locale\/.*\// && !/%(dir|lang)/) {
1129                 $(NF + 1) = "# FIXME consider using %find_lang"
1130         }
1131
1132         # atrpms
1133         $0 = fixedsub("%{perl_man1dir}", "%{_mandir}/man1", $0);
1134         $0 = fixedsub("%{perl_man3dir}", "%{_mandir}/man3", $0);
1135         $0 = fixedsub("%{perl_bin}", "%{_bindir}", $0);
1136
1137         gsub(libdir "/pkgconfig", "%{_pkgconfigdir}");
1138         gsub("%{_libdir}/pkgconfig", "%{_pkgconfigdir}");
1139         gsub("%{_prefix}/lib/pkgconfig", "%{_pkgconfigdir}");
1140
1141         gsub("%{_datadir}/applications", "%{_desktopdir}");
1142         gsub("%{_datadir}/icons", "%{_iconsdir}");
1143         gsub("%{_datadir}/pixmaps", "%{_pixmapsdir}");
1144 }
1145
1146 function fill(ch, n, i) {
1147         for (i = 0; i < n; i++)
1148                 printf("%c", ch)
1149 }
1150
1151 function format_flush(line, indent, newline, word, first_word) {
1152         first_word = 1
1153         if (format_indent == -1)
1154                 newline = ""
1155         else
1156                 newline = fill(" ", format_indent) "- "
1157
1158         while (match(line, /[^\t ]+/)) {
1159                 word = substr(line, RSTART, RLENGTH)
1160                 if (length(newline) + length(word) + 1 > tw) {
1161                         print newline
1162
1163                         if (format_indent == -1)
1164                                 newline = ""
1165                         else
1166                                 newline = fill(" ", format_indent + 2)
1167                         first_word = 1
1168                 }
1169
1170                 if (first_word) {
1171                         newline = newline word
1172                         first_word = 0
1173                 } else
1174                         newline = newline " " word
1175
1176                 line = substr(line, RSTART + RLENGTH)
1177         }
1178         if (newline ~ /[^\t ]/) {
1179                 print newline
1180         }
1181 }
1182
1183 function cflags(var)
1184 {
1185         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
1186                 removed[var] = 1
1187                 return 0
1188         }
1189
1190         if (!/!\?debug/)
1191                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
1192         return 1
1193 }
This page took 0.12332 seconds and 3 git commands to generate.