]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- fixed problem with substitutions:
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.21. Adapter adapts .spec files for PLD.
4 #
5 # Copyright (C) 1999, 2000 PLD-Team <pld-list@pld.org.pl>
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.org.pl>
11 #       Michal Kochanowicz <mkochano@ee.pw.edu.pl>
12 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
13
14 BEGIN {
15         preamble = 1            # Is it part of preamble? Default - yes
16         boc = 2                 # Beggining of %changelog
17         bod = 0                 # Beggining of %description
18         tw = 70                 # Descriptions width
19
20         # File with rpm groups
21         "rpm --eval %_topdir" | getline groups_file
22         groups_file = groups_file "/groups"
23
24         # Temporary file for changelog section
25         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
26
27         # Load rpm macros
28         "rpm --eval %_prefix"   | getline prefix
29         "rpm --eval %_bindir"   | getline bindir
30         "rpm --eval %_sbindir"  | getline sbindir
31         "rpm --eval %_libdir"   | getline libdir
32         "rpm --eval %_sysconfdir" | getline sysconfdir
33         "rpm --eval %_datadir"  | getline datadir
34         "rpm --eval %_includedir" | getline includedir
35         "rpm --eval %_mandir"   | getline mandir
36         "rpm --eval %_infodir"  | getline infodir
37 }
38
39 # There should be a comment with CVS keywords on the first line of file.
40 FNR == 1 {
41         if (!/# \$Revision:/)   # If this line is already OK?
42                 print "# $" "Revision:$, " "$" "Date:$" # No
43         else {
44                 print $0                                # Yes
45                 next            # It is enough for first line
46         }
47 }
48
49 # If the latest line matched /%files/
50 defattr == 1 {
51         if ($0 !~ /defattr/)    # If no %defattr
52                 print "%defattr(644,root,root,755)"     # Add it
53         else
54                 $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
55         defattr = 0
56 }
57
58 # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
59 /^%define/ {
60         if ($2 == "_applnkdir")
61                 next
62         if ($2 == "date")
63                 date = 1
64 }
65
66 # descriptions:
67 /^%description/, (/^%[a-z]+/ && !/^%description/) {
68         preamble = 0
69
70         if (/^%description/) {
71                 bod++
72                 format_line = ""
73                 format_indent = -1
74         }
75
76         # Define _prefix and _mandir if it is X11 application
77         if (/^%description$/ && x11 == 1) {
78                 print "%define\t\t_prefix\t\t/usr/X11R6"
79                 print "%define\t\t_mandir\t\t%{_prefix}/man\n"
80                 prefix = "/usr/X11R6"
81                 x11 = 2
82         }
83         
84         # Format description
85         if (description == 1 && !/^%[a-z]+/ && !/^%description/) {
86                 if (/^[ \t]*$/) {
87                         format_flush(format_line, format_indent)
88                         print ""
89                         format_line = ""
90                         format_indent = -1
91                 } else if (/^[ \t]*[-\*][ \t]*/) {
92                         format_flush(format_line, format_indent)
93                         match($0, /^[ \t]*/)    
94                         format_indent = RLENGTH
95                         match($0, /^[ \t]*[-\*][ \t]/)
96                         format_line = substr($0, RLENGTH)
97                 } else 
98                         format_line = format_line " " $0
99                 next
100         }
101  
102         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
103                 format_flush(format_line, format_indent)
104                 if (bod == 2) {
105                         bod = 1
106                         description = 1
107                 } else {
108                         bod = 0
109                         description = 0
110                 }
111         } else
112                 description = 1
113 }
114
115 # %prep section:
116 /^%prep/, (/^%[a-z]+$/ && !/^%prep/) {
117         preamble = 0
118         
119         # Add '-q' to %setup
120         if (/^%setup/ && !/-q/)
121                 sub(/^%setup/, "%setup -q")
122 }
123
124 # %build section:
125 /^%build/, (/^%[a-z]+$/ && !/^%build/) {
126         preamble = 0
127
128         use_macros()
129 }
130
131 # %clean section:
132 /^%clean/, (/^%[a-z]+$/ && !/^%clean/) {
133         did_clean = 1
134 }
135
136 # %install section:
137 /^%install/, (/^%[a-z]+$/ && !/^%install/) {
138         
139         preamble = 0
140         
141         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+\${?RPM_BUILD_ROOT}?/ && did_rmroot==0) {
142                 did_rmroot=1
143                 print "rm -rf $RPM_BUILD_ROOT"
144                 next
145         }
146
147         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
148                 print "rm -rf $RPM_BUILD_ROOT"
149                 did_rmroot=1
150         }
151         
152         use_macros()
153         
154         # 'install -d' instead 'mkdir -p'
155         if (/mkdir -p/)
156                 sub(/mkdir -p/, "install -d")
157                 
158         # No '-u root' or '-g root' for 'install'
159         if (/^install/ && /-[ug][ \t]*root/)
160                 gsub(/-[ug][ \t]*root /, "")
161         
162         if (/^install/ && /-m[ \t]*644/)
163                 gsub(/-m[ \t]*644 /, "")
164         
165         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
166         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
167                 next
168         
169         # No lines contain 'chmod' if it sets the modes to '644'
170         if ($1 ~ /chmod/ && $2 ~ /644/)
171                 next
172         
173         # 'gzip -9nf' for compressing
174         if ($1 ~ /gzip|bzip2/) {
175                 if ($2 ~ /^-/)
176                         sub(/-[A-Za-z0-9]+ /, "", $0)
177                 sub($1, "gzip -9nf")
178         }
179 }
180
181 # Scripts
182 script = 0
183 /^%pre/, (/^[a-z]+$/ && !/^%pre/) { script = 1 }
184 /^%preun/, (/^[a-z]+$/ && !/^%preun/) { script = 1 }
185 /^%post/, (/^[a-z]+$/ && !/^%post/) {   script = 1 }
186 /^%postun/, (/^[a-z]+$/ && !/^%postun/) { script = 1 }
187 script == 1 {
188         preamble = 0
189         if ($1 ~ /^mv$/) {
190                 if ($2 ~ /^-/)
191                         sub(/-[A-Za-z0-9]+ /, "", $0)
192                 sub($1, "mv -f")
193         }
194         if ($1 ~ /^rm$/) {
195                 recursive = 0
196                 if ($2 ~ /^-/) {
197                         if (match($2, "r"))
198                                 recursive = 1
199                         sub(/-[A-Za-z0-9]+ /, "", $0)
200                 }
201                 if (recursive)
202                         sub($1, "rm -rf")
203                 else
204                         sub($1, "rm -f")
205         }
206 }
207
208 # %files section:
209 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
210         preamble = 0
211         
212         if ($0 ~ /^%files/)
213                 defattr = 1
214         
215         use_macros()
216 }
217
218 # %changelog section:
219 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
220         preamble = 0
221         has_changelog = 1
222         # There should be some CVS keywords on the first line of %changelog.
223         if (boc == 1) {
224                 if (!/PLD Team/) {
225                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
226                         printf "All persons listed below can be reached at " > changelog_file
227                         print "<cvs_login>@pld.org.pl\n" > changelog_file
228                         print "$" "Log:$" > changelog_file
229                 }
230                 boc = 0
231         }
232         
233         # Define date macro.
234         if (boc == 2) {
235                 if (date == 0) {
236                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
237                         print " date +\"%a %b %d %Y\"`)" > changelog_file
238                         date = 1
239                 }
240                 boc = 1
241         }
242
243         if (!/^%[a-z]+$/ || /changelog/)
244                 print > changelog_file
245         else
246                 print
247         next
248 }
249
250 # preambles:
251 preamble == 1 {
252         # There should not be a space after the name of field
253         # and before the colon.
254         sub(/[ \t]*:/, ":")
255         
256         field = tolower($1)
257         
258         if (Byla_grupa == 1 && field !~ /group(\(..\))?:/) {
259                 Byla_grupa = 0
260                 print "Group:\t\t" Grupa["en"]
261                 if (Grupa["en"] ~ /^X11/ && x11 == 0)   # Is it X11 application?
262                        x11 = 1
263
264                 byl_plik_z_grupami = 0
265                 byl_opis_grupy = 0
266                 while ((getline linia_grup < groups_file) > 0) {
267                         byl_plik_z_grupami = 1
268                         if (linia_grup == Grupa["en"]) {
269                                 byl_opis_grupy = 1
270                                 break
271                         }
272                 }
273
274                 if (!byl_plik_z_grupami)
275                         print "######\t\t" groups_file ": no such file"
276                 else if (!byl_opis_grupy)
277                         print "######\t\t" "Unknown group!"
278                 else
279                         while (getline linia_grup < groups_file) {
280                                 if (linia_grup == "")
281                                         break
282                                 split(linia_grup, g, /[\[\]:]/)
283                                 sub(/^[ \t]*/,"",g[4])
284                                 Grupa[g[2]]=g[4]
285                         }
286                 
287                 close(groups_file)
288
289                 delete Grupa["en"]
290                 for (jezyk in Grupa) {
291                         print "Group(" jezyk "):\t" Grupa[jezyk]
292                         delete Grupa[jezyk]
293                 }
294         }
295         
296         if (field ~ /packager:|distribution:|docdir:|prefix:/)
297                 next
298         
299         if (field ~ /buildroot:/)
300                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
301
302         if (field ~ /group(\(..\))?:/) {
303                 format_preamble()
304                 
305                 if (!match(field,/\(..\):/))
306                         glang="en"
307                 else
308                         glang=substr(field,RSTART+1,2)
309                 sub(/^[^ \t]*[ \t]*/,"")
310                 Grupa[glang] = $0
311                 Byla_grupa = 1
312                 
313                 next    # Line is already formatted and printed
314         }
315                 
316         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
317         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
318                 $1 = "License:"
319         
320         if (field ~ /name:/)
321                 name = $2
322
323         if (field ~ /version:/)
324                 version = $2
325
326         if (field ~ /serial:/)
327                 $1 = "Epoch:"
328
329         # Use %{name} and %{version} in the filenames in "Source:"
330         if (field ~ /source/ || field ~ /patch/) {
331                 n = split($2, url, /\//)
332                 if (url[n] ~ /\.gz$/) {
333                         url[n+1] = ".gz" url[n+1]
334                         sub(/\.gz$/,"",url[n])
335                 }
336                 if (url[n] ~ /\.zip$/) {
337                         url[n+1] = ".zip" url[n+1]
338                         sub(/\.zip$/,"",url[n])
339                 }
340                 if (url[n] ~ /\.tar$/) {
341                         url[n+1] = ".tar" url[n+1]
342                         sub(/\.tar$/,"",url[n])
343                 }
344                 if (url[n] ~ /\.patch$/) {
345                         url[n+1] = ".patch" url[n+1]
346                         sub(/\.patch$/,"",url[n])
347                 }
348                 if (url[n] ~ /\.bz2$/) {
349                         url[n+1] = ".bz2" url[n+1]
350                         sub(/\.bz2$/,"",url[n])
351                 }
352                 filename = url[n]
353                 url[n] = fixedsub(name, "%{name}", url[n])
354                 if (field ~ /source/) 
355                         url[n] = fixedsub(version, "%{version}", url[n])
356                 $2 = fixedsub(filename, url[n], $2)
357         }
358
359         if (field ~ /source:/)
360                 $1 = "Source0:" 
361
362         if (field ~ /patch:/)
363                 $1 = "Patch0:"
364         
365         format_preamble()
366         
367         if ($1 ~ /%define/) {
368                 # Do not add %define of _prefix if it already is.
369                 if ($2 ~ /_prefix/) {
370                         sub("^"prefix, $3, bindir)
371                         sub("^"prefix, $3, sbindir)
372                         sub("^"prefix, $3, libdir)
373                         sub("^"prefix, $3, datadir)
374                         sub("^"prefix, $3, includedir)
375                         prefix = $3
376                         x11 = 2
377                 }
378                 if ($2 ~ /_bindir/ && !/_sbindir/)
379                         bindir = $3
380                 if ($2 ~ /_sbindir/)
381                         sbindir = $3
382                 if ($2 ~ /_libdir/)
383                         libdir = $3
384                 if ($2 ~ /_sysconfdir/)
385                         sysconfdir = $3
386                 if ($2 ~ /_datadir/)
387                         datadir = $3
388                 if ($2 ~ /_includedir/)
389                         includedir = $3
390                 if ($2 ~ /_mandir/)
391                         mandir = $3
392                 if ($2 ~ /_infodir/)
393                         infodir = $3
394         }
395 }
396
397
398 # main()  ;-)
399 {
400         preamble = 1
401         
402         print
403 }
404
405
406 END {
407         close(changelog_file)
408         while ((getline < changelog_file) > 0)
409                 print
410         system("rm -f " changelog_file)
411
412         if (did_clean == 0) {
413                 print ""
414                 print "%clean"
415                 print "rm -rf $RPM_BUILD_ROOT"
416         }
417
418         if (date == 0) {
419                 print ""
420                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
421         }
422         
423         if (has_changelog == 0)
424                 print "%changelog"
425
426         if (boc > 0) {
427                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
428                 printf "All persons listed below can be reached at "
429                 print "<cvs_login>@pld.org.pl\n"
430                 print "$" "Log:$"
431         }
432 }
433
434 function fixedsub(s1,s2,t,      ind) {
435 # substitutes fixed strings (not regexps)
436         if (ind = index(t,s1))
437                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
438         return t
439 }
440
441 # There should be one or two tabs after the colon.
442 function format_preamble()
443 {
444         sub(/:[ \t]*/, ":")
445         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
446                 if (RLENGTH < 8)
447                         sub(/:/, ":\t\t")
448                 else
449                         sub(/:/, ":\t")
450         }
451 }
452
453 # Replace directly specified directories with macros
454 function use_macros()
455 {
456         gsub(bindir, "%{_bindir}")
457         gsub("%{_prefix}/bin", "%{_bindir}")
458         gsub("%{prefix}/bin", "%{_bindir}")
459
460         for (c = 1; c <= NF; c++) {
461                 if ($c ~ sbindir "/fix-info-dir")
462                         continue;
463                 gsub(sbindir, "%{_sbindir}", $c)
464         }
465
466         gsub("%{prefix}/sbin", "%{_sbindir}")
467         gsub("%{_prefix}/sbib", "%{_sbindir}")
468
469         gsub(libdir, "%{_libdir}")
470         gsub("%{prefix}/lib", "%{_libdir}")
471         gsub("%{_prefix}/lib", "%{_libdir}")
472
473         for (c = 1; c <= NF; c++) {
474                 if ($c ~ sysconfdir "/cron.d")
475                         continue;
476                 if ($c ~ sysconfdir "/crontab.d")
477                         continue;
478                 if ($c ~ sysconfdir "/logrotate.d")
479                         continue;
480                 if ($c ~ sysconfdir "/pam.d")
481                         continue;
482                 if ($c ~ sysconfdir "/profile.d")
483                         continue;
484                 if ($c ~ sysconfdir "/rc.d")
485                         continue;
486                 if ($c ~ sysconfdir "/security")
487                         continue;
488                 if ($c ~ sysconfdir "/skel")
489                         continue;
490                 if ($c ~ sysconfdir "/sysconfig")
491                         continue;
492                 gsub(sysconfdir, "%{_sysconfdir}", $c)
493         }
494
495         gsub(datadir, "%{_datadir}")
496         gsub("%{prefix}/share", "%{_datadir}")
497         gsub("%{_prefix}/share", "%{_datadir}")
498
499         gsub(includedir, "%{_includedir}")
500         gsub("%{prefix}/include", "%{_includedir}")
501         gsub("%{_prefix}/include", "%{_includedir}")
502
503         gsub(mandir, "%{_mandir}")
504         gsub("%{_datadir}/man", "%{_mandir}")
505         gsub("%{_prefix}/share/man", "%{_mandir}")
506         gsub("%{prefix}/share/man", "%{_mandir}")
507         gsub("%{prefix}/man", "%{_mandir}")
508         gsub("%{_prefix}/man", "%{_mandir}")
509
510         gsub(infodir, "%{_infodir}")
511         gsub("%{prefix}/info", "%{_infodir}")
512         gsub("%{_prefix}/info", "%{_infodir}")
513
514         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
515
516         if (prefix != "/") {
517                 for (c = 1; c <= NF; c++) {
518                         if ($c ~ prefix "/sbin/fix-info-dir")
519                                 continue;
520                         gsub(prefix, "%{_prefix}", $c)
521                 }
522                 gsub("%{prefix}", "%{_prefix}")
523         }
524
525         gsub("%{PACKAGE_VERSION}", "%{version}")
526         gsub("%{PACKAGE_NAME}", "%{name}")
527
528         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
529         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
530
531         gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
532         gsub("%{_datadir}/applnk", "%{_applnkdir}")
533
534         gsub("^make$", "%{__make}")
535         gsub("^make ", "%{__make} ")
536 }
537
538 function fill(ch, n, i) {
539         for (i = 0; i < n; i++)
540                 printf("%c", ch)
541 }
542
543 function format_flush(line, indent, newline, word, first_word) {
544         first_word = 1
545         if (format_indent == -1) 
546                 newline = ""
547         else
548                 newline = fill(" ", format_indent) "- "
549
550         while (match(line, /[^\t ]+/)) {
551                 word = substr(line, RSTART, RLENGTH)
552                 if (length(newline) + length(word) + 1 > tw) {
553                         print newline
554                         
555                         if (format_indent == -1)
556                                 newline = ""
557                         else
558                                 newline = fill(" ", format_indent + 2)
559                         first_word = 1
560                 }
561
562                 if (first_word) {
563                         newline = newline word
564                         first_word = 0
565                 } else
566                         newline = newline " " word
567                         
568                 line = substr(line, RSTART + RLENGTH)
569         }
570         if (newline ~ /[^\t ]/) {
571                 print newline
572         }
573 }
574
This page took 0.078618 seconds and 4 git commands to generate.