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