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