]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- added s#%{_datadir}/aclocal#%{_aclocaldir}#.
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.16. 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 # %files section:
161 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
162         preamble = 0
163         
164         if ($0 ~ /^%files/)
165                 defattr = 1
166         
167         use_macros()
168 }
169
170 # %changelog section:
171 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
172         preamble = 0
173         
174         # There should be some CVS keywords on the first line of %changelog.
175         if (boc == 1) {
176                 if (!/PLD Team/) {
177                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
178                         printf "All persons listed below can be reached at " > changelog_file
179                         print "<cvs_login>@pld.org.pl\n" > changelog_file
180                         print "$" "Log:$" > changelog_file
181                 }
182                 boc = 0
183         }
184         
185         # Define date macro.
186         if (boc == 2) {
187                 if (date == 0) {
188                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
189                         print " date +\"%a %b %d %Y\"`)" > changelog_file
190                 }
191         boc--
192         }
193
194         if (!/^%[a-z]+$/ || /changelog/)
195                 print > changelog_file
196         else
197                 print
198         next
199 }
200
201 # preambles:
202 preamble == 1 {
203         # There should not be a space after the name of field
204         # and before the colon.
205         sub(/[ \t]*:/, ":")
206         
207         field = tolower($1)
208
209         if (field ~ /packager:|distribution:|docdir:|prefix:/)
210                 next
211         
212         if (field ~ /buildroot:/)
213                 $2 = "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
214
215         if (field ~ /group:/) {
216                 format_preamble()
217                 print $0
218                 
219                 translate_group($2)
220                 close(groups_file)
221                 
222                 if ($2 ~ /^X11/ && x11 == 0)    # Is it X11 application?
223                        x11 = 1
224
225                 next    # Line is already formatted and printed
226         }
227                 
228         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
229         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
230                 $1 = "License:"
231         
232         if (field ~ /name:/)
233                 name = $2
234
235         if (field ~ /version:/)
236                 version = $2
237
238         # Use %{name} and %{version} in the filenames in "Source:"
239         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
240                 n = split($2, url, /\//)
241                 filename = url[n]
242                 sub(name, "%{name}", url[n])
243                 sub(version, "%{version}", url[n])
244                 sub(filename, url[n], $2)
245         }
246
247         if (field ~ /source:/)
248                 $1 = "Source0:" 
249
250         if (field ~ /patch:/)
251                 $1 = "Patch0:"
252         
253         format_preamble()
254         
255         if ($1 ~ /%define/) {
256                 # Do not add %define of _prefix if it already is.
257                 if ($2 ~ /_prefix/) {
258                         prefix = $3
259                         x11 = 2
260                 }
261                 if ($2 ~ /_bindir/ && !/_sbindir/)
262                         bindir = $3
263                 if ($2 ~ /_sbindir/)
264                         sbindir = $3
265                 if ($2 ~ /_libdir/)
266                         libdir = $3
267                 if ($2 ~ /_sysconfdir/)
268                         sysconfdir = $3
269                 if ($2 ~ /_datadir/)
270                         datadir = $3
271                 if ($2 ~ /_includedir/)
272                         includedir = $3
273                 if ($2 ~ /_mandir/)
274                         mandir = $3
275                 if ($2 ~ /_infodir/)
276                         infodir = $3
277         }
278 }
279
280
281 # main()  ;-)
282 {
283         preamble = 1
284         
285         print
286 }
287
288
289 END {
290         close(changelog_file)
291         while ((getline < changelog_file) > 0)
292                 print
293         system("rm -f " changelog_file)
294
295         if (boc == 1) {
296                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
297                 printf "All persons listed below can be reached at "
298                 print "<cvs_login>@pld.org.pl\n"
299                 print "$" "Log:$"
300         }
301 }
302
303 # This function uses grep to determine if there is line (in the current file)
304 # which matches regexp.
305 function is_there_line(line, l)
306 {
307         command = "grep \"" line "\" " ARGV[1]
308         command | getline l
309         close(command)
310
311         if (l != "")
312                 return 1
313         else
314                 return 0
315 }
316
317 # This function prints translated names of groups.
318 function translate_group(group)
319 {
320         for(;;) {
321                 result = getline line < groups_file
322                 
323                 if (result == -1) {
324                         print "######\t\t" groups_file ": no such file"
325                         return
326                 }
327
328                 if (result == 0) {
329                         print "######\t\t" "Unknown group!"
330                         return
331                 }
332                 
333                 if (line == group) {
334                         found = 1
335                         continue
336                 }
337
338                 if (found == 1)
339                         if (line ~ /\[[a-z][a-z]\]:/) {
340                                 split(line, g, /\[|\]|\:/)
341                                 if (!is_there_line("^Group(" g[2] "):"))
342                                                 printf("Group(%s):%s\n", g[2], g[4])
343                         } else {
344                                 found = 0
345                                 return
346                         }
347         }
348 }
349
350 # There should be one or two tabs after the colon.
351 function format_preamble()
352 {
353         sub(/:[ \t]*/, ":")
354         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
355                 if (RLENGTH < 8)
356                         sub(/:/, ":\t\t")
357                 else
358                         sub(/:/, ":\t")
359         }
360 }
361
362 # Replace directly specified directories with macros
363 function use_macros()
364 {
365         gsub(bindir, "%{_bindir}")
366         gsub("%{_prefix}/bin", "%{_bindir}")
367         gsub("%{prefix}/bin", "%{_bindir}")
368
369         gsub(sbindir, "%{_sbindir}")
370         gsub("%{prefix}/sbin", "%{_sbindir}")
371         gsub("%{_prefix}/sbib", "%{_sbindir}")
372
373         gsub(libdir, "%{_libdir}")
374         gsub("%{prefix}/lib", "%{_libdir}")
375         gsub("%{_prefix}/lib", "%{_libdir}")
376
377         gsub(sysconfdir, "%{_sysconfdir}")
378
379         gsub(datadir, "%{_datadir}")
380         gsub("%{prefix}/share", "%{_datadir}")
381         gsub("%{_prefix}/share", "%{_datadir}")
382
383         gsub(includedir, "%{_includedir}")
384         gsub("%{prefix}/include", "%{_includedir}")
385         gsub("%{_prefix}/include", "%{_includedir}")
386
387         gsub(mandir, "%{_mandir}")
388         gsub("%{prefix}/man", "%{_mandir}")
389         gsub("%{_prefix}/man", "%{_mandir}")
390
391         gsub(infodir, "%{_infodir}")
392         gsub("%{prefix}/info", "%{_infodir}")
393         gsub("%{_prefix}/info", "%{_infodir}")
394
395         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
396
397         if (prefix != "/") {
398                 gsub(prefix, "%{_prefix}")
399                 gsub("%{prefix}", "%{_prefix}")
400         }
401
402         gsub("%{PACKAGE_VERSION}", "%{version}")
403         gsub("%{PACKAGE_NAME}", "%{name}")
404 }
405
This page took 0.768725 seconds and 4 git commands to generate.