]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- fixed bug in is_there_line() ( close(ARGV[1]) -> close(command) )
[packages/rpm-build-tools.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         format_preamble();
230         
231         if ($1 ~ /%define/) {
232                 # Do not add %define of _prefix if it already is.
233                 if ($2 ~ /_prefix/) {
234                         prefix = $3;
235                         x11 = 2;
236                 }
237                 if ($2 ~ /_bindir/ && !/_sbindir/)
238                         bindir = $3;
239                 if ($2 ~ /_sbindir/)
240                         sbindir = $3;
241                 if ($2 ~ /_libdir/)
242                         libdir = $3;
243                 if ($2 ~ /_sysconfdir/)
244                         sysconfdir = $3;
245                 if ($2 ~ /_datadir/)
246                         datadir = $3;
247                 if ($2 ~ /_includedir/)
248                         includedir = $3;
249                 if ($2 ~ /_mandir/)
250                         mandir = $3;
251                 if ($2 ~ /_infodir/)
252                         infodir = $3;
253         }
254 }
255
256 {
257         preamble = 1;
258         
259         print;
260 }
261
262 END {
263         if (boc == 1) {
264                 print "* %{date} PLD Team <pld-list@pld.org.pl>";
265                 printf "All below listed persons can be reached on ";
266                 print "<cvs_login>@pld.org.pl\n";
267                 print "$" "Log:$";
268         }
269 }
270
271 # This function uses grep to determine if there is line (in the current file)
272 # which matches regexp.
273 function is_there_line(line, l)
274 {
275         command = "grep \"" line "\" " ARGV[1];
276         command | getline l;
277         close(command);
278
279         if (l != "")
280                 return 1;
281         else
282                 return 0;
283 }
284
285 # This function prints translated names of groups.
286 function translate_group(group)
287 {
288         for(;;) {
289                 result = getline line < groups_file;
290                 
291                 if (result == -1) {
292                         print "######\t\t" groups_file ": no such file";
293                         return;
294                 }
295
296                 if (result == 0) {
297                         print "######\t\t" "Unknown group!";
298                         return;
299                 }
300                 
301                 if (line ~ group) {
302                         found = 1;
303                         continue;
304                 }
305
306                 if (found == 1)
307                         if (line ~ /\[[a-z][a-z]\]:/) {
308                                 split(line, g, /\[|\]|\:/);
309                                 if (!is_there_line("^Group(" g[2] "):"))
310                                                 printf("Group(%s):%s\n", g[2], g[4]);
311                         } else {
312                                 found = 0;
313                                 return;
314                         }
315         }
316 }
317
318 # There should be one or two tabs after the colon.
319 function format_preamble()
320 {
321         sub(/:[ \t]*/, ":");
322         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
323                 if (RLENGTH < 8)
324                         sub(/:/, ":\t\t");
325                 else
326                         sub(/:/, ":\t");
327         }
328 }
329
330 # Replace directly specified directories with macros
331 function use_macros()
332 {
333         gsub(bindir, "%{_bindir}");
334         gsub(sbindir, "%{_sbindir}");
335         gsub(libdir, "%{_libdir}");
336         gsub(sysconfdir, "%{_sysconfdir}");
337         gsub(datadir, "%{_datadir}");
338         gsub(includedir, "%{_includedir}");
339         gsub(mandir, "%{_mandir}");
340         gsub(infodir, "%{_infodir}");
341         gsub(prefix, "%{_prefix}");
342 }
343
This page took 0.065627 seconds and 4 git commands to generate.