]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- "can be reached on" -> "can be reached at"
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.14. 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 persons listed below can be reached at " > 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-%(id -u -n)"
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                 while ((getline < changelog_file) > 0)
281                         print
282                 system("rm -f " changelog_file)
283         }
284
285         if (boc == 1) {
286                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
287                 printf "All below listed persons can be reached on "
288                 print "<cvs_login>@pld.org.pl\n"
289                 print "$" "Log:$"
290         }
291 }
292
293 # This function uses grep to determine if there is line (in the current file)
294 # which matches regexp.
295 function is_there_line(line, l)
296 {
297         command = "grep \"" line "\" " ARGV[1]
298         command | getline l
299         close(command)
300
301         if (l != "")
302                 return 1
303         else
304                 return 0
305 }
306
307 # This function prints translated names of groups.
308 function translate_group(group)
309 {
310         for(;;) {
311                 result = getline line < groups_file
312                 
313                 if (result == -1) {
314                         print "######\t\t" groups_file ": no such file"
315                         return
316                 }
317
318                 if (result == 0) {
319                         print "######\t\t" "Unknown group!"
320                         return
321                 }
322                 
323                 if (line == group) {
324                         found = 1
325                         continue
326                 }
327
328                 if (found == 1)
329                         if (line ~ /\[[a-z][a-z]\]:/) {
330                                 split(line, g, /\[|\]|\:/)
331                                 if (!is_there_line("^Group(" g[2] "):"))
332                                                 printf("Group(%s):%s\n", g[2], g[4])
333                         } else {
334                                 found = 0
335                                 return
336                         }
337         }
338 }
339
340 # There should be one or two tabs after the colon.
341 function format_preamble()
342 {
343         sub(/:[ \t]*/, ":")
344         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
345                 if (RLENGTH < 8)
346                         sub(/:/, ":\t\t")
347                 else
348                         sub(/:/, ":\t")
349         }
350 }
351
352 # Replace directly specified directories with macros
353 function use_macros()
354 {
355         gsub(bindir, "%{_bindir}")
356         gsub("%{_prefix}/bin", "%{_bindir}")
357         gsub("%{prefix}/bin", "%{_bindir}")
358
359         gsub(sbindir, "%{_sbindir}")
360         gsub("%{prefix}/sbin", "%{_sbindir}")
361         gsub("%{_prefix}/sbib", "%{_sbindir}")
362
363         gsub(libdir, "%{_libdir}")
364         gsub("%{prefix}/lib", "%{_libdir}")
365         gsub("%{_prefix}/lib", "%{_libdir}")
366
367         gsub(sysconfdir, "%{_sysconfdir}")
368
369         gsub(datadir, "%{_datadir}")
370         gsub("%{prefix}/share", "%{_datadir}")
371         gsub("%{_prefix}/share", "%{_datadir}")
372
373         gsub(includedir, "%{_includedir}")
374         gsub("%{prefix}/include", "%{_includedir}")
375         gsub("%{_prefix}/include", "%{_includedir}")
376
377         gsub(mandir, "%{_mandir}")
378         gsub("%{prefix}/man", "%{_mandir}")
379         gsub("%{_prefix}/man", "%{_mandir}")
380
381         gsub(infodir, "%{_infodir}")
382         gsub("%{prefix}/info", "%{_infodir}")
383         gsub("%{_prefix}/info", "%{_infodir}")
384
385         if (prefix != "/") {
386                 gsub(prefix, "%{_prefix}")
387                 gsub("%{prefix}", "%{_prefix}")
388         }
389 }
390
This page took 0.057318 seconds and 4 git commands to generate.