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