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