]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
version 0.19
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.19. 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@sith.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 = 75                 # Descriptions width
19
20         groups_file = ENVIRON["HOME"] "/rpm/groups"     # File with rpm groups
21
22         # Temporary file for changelog section
23         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
24
25         # Is 'date' macro already defined?
26         if (is_there_line("%define date"))
27                 date = 1
28         
29         # Load rpm macros
30         "rpm --eval %_prefix"   | getline prefix
31         "rpm --eval %_bindir"   | getline bindir
32         "rpm --eval %_sbindir"  | getline sbindir
33         "rpm --eval %_libdir"   | getline libdir
34         "rpm --eval %_sysconfdir" | getline sysconfdir
35         "rpm --eval %_datadir"  | getline datadir
36         "rpm --eval %_includedir" | getline includedir
37         "rpm --eval %_mandir"   | getline mandir
38         "rpm --eval %_infodir"  | getline infodir
39 }
40
41 # There should be a comment with CVS keywords on the first line of file.
42 FNR == 1 {
43         if (!/# \$Revision:/)   # If this line is already OK?
44                 print "# $" "Revision:$, " "$" "Date:$" # No
45         else {
46                 print $0                                # Yes
47                 next            # It is enough for first line
48         }
49 }
50
51 # If the latest line matched /%files/
52 defattr == 1 {
53         if ($0 !~ /defattr/)    # If no %defattr
54                 print "%defattr(644,root,root,755)"     # Add it
55         else
56                 $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
57         defattr = 0
58 }
59
60 # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
61 /^%define/ {
62         if ($2 == "_applnkdir")
63                 next                                    
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 # %install section:
132 /^%install/, (/^%[a-z]+$/ && !/^%install/) {
133         preamble = 0
134         
135         use_macros()
136
137         # 'install -d' instead 'mkdir -p'
138         if (/mkdir -p/)
139                 sub(/mkdir -p/, "install -d")
140                 
141         # No '-u root' or '-g root' for 'install'
142         if (/^install/ && /-[ug][ \t]*root/)
143                 gsub(/-[ug][ \t]*root /, "")
144         
145         if (/^install/ && /-m[ \t]*644/)
146                 gsub(/-m[ \t]*644 /, "")
147         
148         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
149         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
150                 next
151         
152         # No lines contain 'chmod' if it sets the modes to '644'
153         if ($1 ~ /chmod/ && $2 ~ /644/)
154                 next
155         
156         # 'gzip -9nf' for compressing
157         if ($1 ~ /gzip|bzip2/) {
158                 if ($2 ~ /^-/)
159                         sub(/-[A-Za-z0-9]+ /, "", $0)
160                 sub($1, "gzip -9nf")
161         }
162 }
163
164 # Scripts
165 /^%pre /, (/^[a-z]+$/ && !/^%pre /) { preamble = 0 }
166 /^%preun/, (/^[a-z]+$/ && !/^%preun/) { preamble = 0 }
167 /^%post /, (/^[a-z]+$/ && !/^%post /) { preamble = 0 }
168 /^%postun/, (/^[a-z]+$/ && !/^%postun/) { preamble = 0 }
169         
170 # %files section:
171 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
172         preamble = 0
173         
174         if ($0 ~ /^%files/)
175                 defattr = 1
176         
177         use_macros()
178 }
179
180 # %changelog section:
181 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
182         preamble = 0
183         
184         # There should be some CVS keywords on the first line of %changelog.
185         if (boc == 1) {
186                 if (!/PLD Team/) {
187                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
188                         printf "All persons listed below can be reached at " > changelog_file
189                         print "<cvs_login>@pld.org.pl\n" > changelog_file
190                         print "$" "Log:$" > changelog_file
191                 }
192                 boc = 0
193         }
194         
195         # Define date macro.
196         if (boc == 2) {
197                 if (date == 0) {
198                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
199                         print " date +\"%a %b %d %Y\"`)" > changelog_file
200                 }
201         boc--
202         }
203
204         if (!/^%[a-z]+$/ || /changelog/)
205                 print > changelog_file
206         else
207                 print
208         next
209 }
210
211 # preambles:
212 preamble == 1 {
213         # There should not be a space after the name of field
214         # and before the colon.
215         sub(/[ \t]*:/, ":")
216         
217         field = tolower($1)
218
219         if (field ~ /packager:|distribution:|docdir:|prefix:/)
220                 next
221         
222         if (field ~ /buildroot:/)
223                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
224
225         if (field ~ /group:/) {
226                 format_preamble()
227                 print $0
228                 
229                 translate_group($2)
230                 close(groups_file)
231                 
232                 if ($2 ~ /^X11/ && x11 == 0)    # Is it X11 application?
233                        x11 = 1
234
235                 next    # Line is already formatted and printed
236         }
237                 
238         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
239         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
240                 $1 = "License:"
241         
242         if (field ~ /name:/)
243                 name = $2
244
245         if (field ~ /version:/)
246                 version = $2
247
248         # Use %{name} and %{version} in the filenames in "Source:"
249         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
250                 n = split($2, url, /\//)
251                 filename = url[n]
252                 sub(name, "%{name}", url[n])
253                 sub(version, "%{version}", url[n])
254                 sub(filename, url[n], $2)
255         }
256
257         if (field ~ /source:/)
258                 $1 = "Source0:" 
259
260         if (field ~ /patch:/)
261                 $1 = "Patch0:"
262         
263         format_preamble()
264         
265         if ($1 ~ /%define/) {
266                 # Do not add %define of _prefix if it already is.
267                 if ($2 ~ /_prefix/) {
268                         prefix = $3
269                         x11 = 2
270                 }
271                 if ($2 ~ /_bindir/ && !/_sbindir/)
272                         bindir = $3
273                 if ($2 ~ /_sbindir/)
274                         sbindir = $3
275                 if ($2 ~ /_libdir/)
276                         libdir = $3
277                 if ($2 ~ /_sysconfdir/)
278                         sysconfdir = $3
279                 if ($2 ~ /_datadir/)
280                         datadir = $3
281                 if ($2 ~ /_includedir/)
282                         includedir = $3
283                 if ($2 ~ /_mandir/)
284                         mandir = $3
285                 if ($2 ~ /_infodir/)
286                         infodir = $3
287         }
288 }
289
290
291 # main()  ;-)
292 {
293         preamble = 1
294         
295         print
296 }
297
298
299 END {
300         close(changelog_file)
301         while ((getline < changelog_file) > 0)
302                 print
303         system("rm -f " changelog_file)
304
305         if (boc == 1) {
306                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
307                 printf "All persons listed below can be reached at "
308                 print "<cvs_login>@pld.org.pl\n"
309                 print "$" "Log:$"
310         }
311 }
312
313 # This function uses grep to determine if there is line (in the current file)
314 # which matches regexp.
315 function is_there_line(line, l)
316 {
317         command = "grep \"" line "\" " ARGV[1]
318         command | getline l
319         close(command)
320
321         if (l != "")
322                 return 1
323         else
324                 return 0
325 }
326
327 # This function prints translated names of groups.
328 function translate_group(group)
329 {
330         for(;;) {
331                 result = getline line < groups_file
332                 
333                 if (result == -1) {
334                         print "######\t\t" groups_file ": no such file"
335                         return
336                 }
337
338                 if (result == 0) {
339                         print "######\t\t" "Unknown group!"
340                         return
341                 }
342                 
343                 if (line == group) {
344                         found = 1
345                         continue
346                 }
347
348                 if (found == 1)
349                         if (line ~ /\[[a-z][a-z]\]:/) {
350                                 split(line, g, /\[|\]|\:/)
351                                 if (!is_there_line("^Group(" g[2] "):"))
352                                                 printf("Group(%s):%s\n", g[2], g[4])
353                         } else {
354                                 found = 0
355                                 return
356                         }
357         }
358 }
359
360 # There should be one or two tabs after the colon.
361 function format_preamble()
362 {
363         sub(/:[ \t]*/, ":")
364         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
365                 if (RLENGTH < 8)
366                         sub(/:/, ":\t\t")
367                 else
368                         sub(/:/, ":\t")
369         }
370 }
371
372 # Replace directly specified directories with macros
373 function use_macros()
374 {
375         gsub(bindir, "%{_bindir}")
376         gsub("%{_prefix}/bin", "%{_bindir}")
377         gsub("%{prefix}/bin", "%{_bindir}")
378
379         gsub(sbindir, "%{_sbindir}")
380         gsub("%{prefix}/sbin", "%{_sbindir}")
381         gsub("%{_prefix}/sbib", "%{_sbindir}")
382
383         gsub(libdir, "%{_libdir}")
384         gsub("%{prefix}/lib", "%{_libdir}")
385         gsub("%{_prefix}/lib", "%{_libdir}")
386
387         gsub(sysconfdir, "%{_sysconfdir}")
388
389         gsub(datadir, "%{_datadir}")
390         gsub("%{prefix}/share", "%{_datadir}")
391         gsub("%{_prefix}/share", "%{_datadir}")
392
393         gsub(includedir, "%{_includedir}")
394         gsub("%{prefix}/include", "%{_includedir}")
395         gsub("%{_prefix}/include", "%{_includedir}")
396
397         gsub(mandir, "%{_mandir}")
398         gsub("%{prefix}/man", "%{_mandir}")
399         gsub("%{_prefix}/man", "%{_mandir}")
400
401         gsub(infodir, "%{_infodir}")
402         gsub("%{prefix}/info", "%{_infodir}")
403         gsub("%{_prefix}/info", "%{_infodir}")
404
405         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
406
407         if (prefix != "/") {
408                 gsub(prefix, "%{_prefix}")
409                 gsub("%{prefix}", "%{_prefix}")
410         }
411
412         gsub("%{PACKAGE_VERSION}", "%{version}")
413         gsub("%{PACKAGE_NAME}", "%{name}")
414
415         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
416         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
417
418         gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
419 }
420
421 function fill(ch, n, i) {
422         for (i = 0; i < n; i++)
423                 printf("%c", ch)
424 }
425
426 function format_flush(line, indent, newline, word, first_word) {
427         first_word = 1
428         if (format_indent == -1) 
429                 newline = ""
430         else
431                 newline = fill(" ", format_indent) "- "
432
433         while (match(line, /[^\t ]+/)) {
434                 word = substr(line, RSTART, RLENGTH)
435                 if (length(newline) + length(word) + 1 > tw) {
436                         print newline
437                         
438                         if (format_indent == -1)
439                                 newline = ""
440                         else
441                                 newline = fill(" ", format_indent + 2)
442                         first_word = 1
443                 }
444
445                 if (first_word) {
446                         newline = newline word
447                         first_word = 0
448                 } else
449                         newline = newline " " word
450                         
451                 line = substr(line, RSTART + RLENGTH)
452         }
453         if (newline ~ /[^\t ]/) {
454                 print newline
455         }
456 }
457
This page took 0.058459 seconds and 4 git commands to generate.