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