]> git.pld-linux.org Git - packages/adapter.git/blob - adapter.awk
- added adding translations of Group fields
[packages/adapter.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.9. 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
18 # There should be a comment with CVS keywords on the first line of file.
19 FNR == 1 {
20         if (!/# \$Revision:/)           # If this line is already OK?
21                 print "# $" "Revision:$, " "$" "Date:$";        # No
22         else
23                 print $0;                                       # Yes
24
25         next;                           # It is enough for first line
26 }
27
28 # descriptions:
29 /%description/, (/^%[a-z]+/ && !/%description/) {
30         preamble = 0;
31
32         if (/^%description/)
33                 bod++;
34         
35         # Define _prefix and _mandir if it is X11 application
36         if (/^%description$/ && x11 == 1) {
37                 print "%define\t\t_prefix\t\t/usr/X11R6";
38                 print "%define\t\t_mandir\t\t%{_prefix}/man\n";
39                 x11 == 2;
40         }
41
42         # Collect whole text of description
43         if (description == 1 && !/^%[a-z]+/ && !/%description/) {
44                 description_text = description_text $0 " ";
45                 next;
46         }
47  
48         # Formt description to the length of tw (default == 77)
49         if (/^%[a-z]+/ && (!/%description/ || bod == 2)) {
50                 n = split(description_text, dt, / /);
51                 for (i = 1; i <= n; i++) {
52                         if (length(line) + length(dt[i]) + 1 < tw)
53                                 line = line dt[i] " ";
54                         else {
55                                 print line;
56                                 line = "";
57                                 i--;
58                         }
59                 }
60
61                 print line "\n";
62                 line = "";
63                 delete dt;
64                 description_text = "";
65                 if (bod == 2) {
66                         bod = 1;
67                         description = 1;
68                 } else {
69                         bod = 0;
70                         description = 0;
71                 }
72         } else
73                 description = 1;
74 }
75
76 # %prep section:
77 /%prep/, (/^%[a-z]+$/ && !/%prep/) {
78         preamble = 0;
79         
80         # add '-q' to %setup
81         if (/%setup/ && !/-q/)
82                 sub(/%setup/, "%setup -q");
83 }
84
85 # %build section:
86 /%build/, (/^%[a-z]+$/ && !/%build/) {
87         preamble = 0;
88
89         # Any ideas?
90 }
91
92 # %install section:
93 /%install/, (/^[a-z]+$/ && !/%install/) {
94         preamble = 0;
95         
96         # 'install -d' instead 'mkdir -p'
97         if (/mkdir -p/)
98                 sub(/mkdir -p/, "install -d");
99                 
100         # no '-u root' or '-g root' for 'install'
101         if (/^install/ && /-[ug][ \t]*root/)
102                 gsub(/-[ug][ \t]*root /, "");
103         
104         if (/^install/ && /-m[ \t]*644/)
105                 gsub(/-m[ \t]*644 /, "");
106         
107         # no lines contain 'chown' or 'chgrp', which changes
108         # owner/group to 'root'
109         if ($1 ~ /chown|chgrp/ && $2 ~ /root|root.root/)
110                 next;
111         
112         # no lines contain 'chmod' if it sets the modes to '644'
113         if ($1 ~ /chmod/ && $2 ~ /644/)
114                 next;
115         
116         # 'gzip -9nf' for compressing
117         if ($1 ~ /gzip|bzip2/) {
118                 if ($2 ~ /^-/)
119                         sub(/-[A-Za-z0-9]+ /, "", $0);
120                 sub($1, "gzip -9nf");
121         }
122 }
123
124 # %files section:
125 /%files/, (/^%[a-z]+$/ && !/%files/) {
126         preamble = 0;
127         
128         if (/%defattr/)
129                 $0 = "%defattr(644,root,root,755)";
130 }
131
132 # %changelog section:
133 /%changelog/, (/^%[a-z]+$/ && !/%changelog/) {
134         preamble = 0;
135         
136         # There should be some CVS keywords on the first line of %changelog.
137         if (boc == 1) {
138                 if (!/PLD Team/) {
139                         print "* %{date} PLD Team <pld-list@pld.org.pl>";
140                         printf "All below listed persons can be reached on ";
141                         print "<cvs_login>@pld.org.pl\n";
142                         print "$" "Log:$";
143                 }
144                 boc = 0;
145         }
146         
147         # Define date macro.
148         if (boc == 2) {
149                 if (date == 0) {
150                         printf "%%define date\t%%(echo `LC_ALL=\"C\"";
151                         print " date +\"%a %b %d %Y\"`)"
152                 }
153         boc--;
154         }
155 }
156
157 # preambles:
158 preamble == 1 {
159         # There should not be a space after the name of field
160         # and before the colon.
161         sub(/[ \t]*:/, ":");
162         
163         field = tolower($1);
164
165         if (field ~ /packager:|distribution:|prefix:/)
166                 next;
167         
168         if (field ~ /buildroot:/)
169                 $2 = "/tmp/%{name}-%{version}-root";
170
171         if (field ~ /group:/) {
172                 format_preamble();
173                 print $0;
174                 
175                 translate_group($2);
176                 close(groups_file);
177                 
178                 if ($2 ~ /^X11/ && x11 == 0)    # Is it X11 application?
179                        x11 = 1;
180
181                 next;   # Line is already formatted and printed
182         }
183                 
184         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
185         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
186                 $1 = "License:";
187         
188         if (field ~ /name:/)
189                 name = $2;
190
191         if (field ~ /version:/)
192                 version = $2;
193
194         # Use %{name} and %{version} in the filenames in "Source:"
195         if (field ~ /source/ && $2 ~ /^ftp:|^http:/) {
196                 n = split($2, url, /\//);
197                 filename = url[n];
198                 sub(name, "%{name}", url[n]);
199                 sub(version, "%{version}", url[n]);
200                 sub(filename, url[n], $2);
201         }
202
203         format_preamble();
204         
205         # Do not add %define of _prefix if it already is.
206         if ($1 ~ /%define/ && $2 ~ /_prefix/)
207                 x11 = 2;
208                         
209 }
210
211 {
212         preamble = 1;
213         
214         print;
215 }
216
217 END {
218         if (boc == 1) {
219                 print "* %{date} PLD Team <pld-list@pld.org.pl>";
220                 printf "All below listed persons can be reached on ";
221                 print "<cvs_login>@pld.org.pl\n";
222                 print "$" "Log:$";
223         }
224 }
225
226 # This function uses grep to determine if there is line (in the current file)
227 # which matches regexp.
228 function is_there_line(line, l)
229 {
230         command = "grep \"" line "\" " ARGV[1];
231         command | getline l;
232         close(ARGV[1]);
233
234         if (l != "")
235                 return 1;
236         else
237                 return 0;
238 }
239
240 # This function prints translated names of groups.
241 function translate_group(group)
242 {
243         for(;;) {
244                 result = getline line < groups_file;
245                 
246                 if (result == -1) {
247                         print "######\t\t" groups_file ": no such file";
248                         return;
249                 }
250
251                 if (result == 0) {
252                         print "######\t\t" "Unknown group!";
253                         return;
254                 }
255                 
256                 if (line ~ group) {
257                         found = 1;
258                         continue;
259                 }
260
261                 if (found == 1)
262                         if (line ~ /\[[a-z][a-z]\]:/) {
263                                 split(line, g, /\[|\]|\:/);
264                                 if (!is_there_line("^Group(" g[2] "):"))
265                                                 printf("Group(%s):%s\n", g[2], g[4]);
266                         } else {
267                                 found = 0;
268                                 return;
269                         }
270         }
271 }
272
273 # There should be one or two tabs after the colon.
274 function format_preamble()
275 {
276         sub(/:[ \t]*/, ":");
277         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
278                 if (RLENGTH < 8)
279                         sub(/:/, ":\t\t");
280                 else
281                         sub(/:/, ":\t");
282         }
283 }
284
This page took 0.057202 seconds and 4 git commands to generate.