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