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