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