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