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