Imported Upstream version 0.50.2
[platform/upstream/intltool.git] / intltool-prepare.in
1 #!@INTLTOOL_PERL@ -w
2 # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
3
4 #  Intltool .desktop, .directory Prepare Tool
5 #
6 #  Copyright (C) 2001 Free Software Foundation.
7 #
8 #  Intltool is free software; you can redistribute it and/or
9 #  modify it under the terms of the GNU General Public License as
10 #  published by the Free Software Foundation; either version 2 of the
11 #  License, or (at your option) any later version.
12 #
13 #  Intltool is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 #  General Public License for more details.
17 #
18 #  You should have received a copy of the GNU General Public License
19 #  along with this program; if not, write to the Free Software
20 #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #
22 #  Author(s): Gediminas Paulauskas <menesis@delfi.lt>
23 #             Kenneth Christiansen <kenneth@gnu.org>
24
25 ## Release information
26 my $PROGRAM      = "intltool-prepare";
27 my $PACKAGE      = "@PACKAGE@";
28 my $VERSION      = "@VERSION@";
29
30 ## Loaded modules
31 use strict;
32 use Getopt::Long;
33 use File::Find;
34
35 ## Scalars used by the option stuff
36 my $HELP_ARG    = "0";
37 my $VERSION_ARG = "0";
38 my $VERBOSE     = "0";
39
40 my @languages;
41 my @desktop_files;
42 my $new;
43 my $file;
44
45 my $desktop_extension = "(desktop|soundlist|keys|directory)";
46
47 my $keywords = "Name|Comment|GenericName|SwallowTitle|description";
48
49 ## Always print as the first thing
50 $| = 1;
51
52 ## Handle options
53 GetOptions (
54             "help|h"            => \$HELP_ARG,
55             "version|v"         => \$VERSION_ARG,
56             "verbose|x"         => \$VERBOSE
57             ) or &invalid_option;
58
59
60 ## Use the supplied arguments
61 ## Check for options.
62 ## This section will check for the different options.
63
64 sub split_on_argument {
65
66     if ($VERSION_ARG) {
67         &version;
68
69     } elsif ($HELP_ARG) {
70         &help;
71
72     } else {
73         &main;
74     }
75 }
76
77 &split_on_argument;
78
79 sub main
80 {
81     print "Working, please wait...\n" if (! $VERBOSE);
82     &find_desktop_files;
83     &append_keywords;
84     &add_to_potfiles;
85     &perform_rescue;
86     &add_to_cvsignore;
87     &fix_makefiles;
88     &generate_empty;
89 }
90
91 sub version {
92     print <<_EOF_;
93 ${PROGRAM} ${PACKAGE} $VERSION
94 Written by Gediminas Paulauskas <menesis\@delfi.lt>, 2000.
95
96 Copyright (C) 2000 Free Software Foundation, Inc.
97 This is free software; see the source for copying conditions.  There is NO
98 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
99 _EOF_
100     exit;
101 }
102
103 sub help
104 {
105     print <<_EOF_;
106 Usage: ${PROGRAM} [OPTION] KEYWORD...
107 Automates preparation steps before software make use of intltool.
108 KEYWORD is a list of additional key other than "Name", "Comment"
109 and "description".
110
111   -h, --help                 shows this help page
112   -v, --version              shows the version
113   -x, --verbose              show lots of feedback
114
115 Report bugs to http://bugs.launchpad.net/intltool
116 _EOF_
117     exit;
118 }
119
120 sub invalid_option
121 {
122     ## Handle invalid arguments
123     ## my $opt = $ARGV[0];
124     ## print "$PROGRAM: invalid option -- $opt\n";
125     print STDERR "Try `$PROGRAM --help' for more information.\n";
126     exit 1;
127 }
128
129 sub append_keywords
130 {
131     my $arg;
132     foreach $arg (@ARGV) {
133         $keywords .= "|$arg";
134     }
135 }
136
137 sub add_to_potfiles
138 {
139     open FILE, ">>po/POTFILES.in";
140     my $intro = 0;
141     foreach my $desktop (@desktop_files) {
142         next if contains("po/POTFILES.in", "$desktop.in");
143     # Print explanation comment only once
144         unless ($intro) {
145            print FILE "# files added by intltool-prepare\n";
146            $intro = 1;
147         }
148         print FILE "$desktop.in\n";
149     }
150     close FILE;
151 }
152
153 sub perform_rescue
154 {
155     foreach $file (@desktop_files) {
156         &rescue_file($file);
157     }
158 }
159
160 sub rescue_file
161 {
162     my ($filename) = @_;
163     my ($msgid, $line, $lang);
164
165     print "Rescuing translations from $filename ...\n" if $VERBOSE;
166
167     open ORIG, "<$filename";
168     $line = 1;
169 ENTRY: while (<ORIG>) {
170         chomp;
171         $line++;
172             my $entry = $_;
173         if (($entry =~ /^($keywords)=(.*)$/) ||
174             ($entry =~ /^(\s*description)=(.*)$/)) {
175             $msgid = $2;
176             $msgid =~ s/\\/\\\\/g;
177             $msgid =~ s/\"/\\"/g;
178         } elsif (($entry =~ /^($keywords)\[(.*?)\]=(.*)$/) ||
179                  ($entry =~ /^(\s*\[)(.*?)\]description=(.*)$/)) {
180             $lang = $2;
181
182             my $msgstr = $3;
183             $msgstr =~ s/\\/\\\\/g;
184             $msgstr =~ s/"/\\"/g;
185             
186             $line--;
187             if ((-s "po/$lang.po") && 
188                 (contains("po/$lang.po", "msgid \"$msgid\""))) {
189                 next ENTRY;
190             }
191             
192             open POFILE, ">>po/$lang.po";
193
194             print POFILE "\n#: $filename.in:$line\n";
195             print POFILE "msgid \"$msgid\"\n";
196             print POFILE "msgstr \"$msgstr\"\n";
197
198             close POFILE;
199         }
200     }
201 }
202
203 sub generate_empty
204 {
205     my $all = ' ';
206     foreach my $full (@desktop_files) {
207         $new = "$full.in";
208         $all .= "$new ";
209             print "Generating empty $new ...\n" if $VERBOSE;
210         open FULL, "<$full";
211         open NEW, ">$new";
212
213         while (<FULL>) {
214             unless (
215                             (/^($keywords)\[.*?\]=.*$/) ||
216                         (/^\s*\[(.*?)\]description=.*$/)
217                            ) {
218                 if (/^($keywords)=.*$/) {
219                     print NEW "_$_";
220                 } elsif (/^(\s*)(description=.*)$/) {
221                     print NEW "$1_$2\n";
222                 } else {
223                     print NEW;
224                 }
225                 }
226         }
227
228         close NEW;
229     }
230     unless ($all eq ' ') {
231         print "*** Please add these files to CVS by following command: ***\n"
232             . "cvs add$all\n";
233     }
234 }
235
236 sub add_to_cvsignore
237 {
238     my $all = ' ';
239     my $ign;
240     foreach $file (@desktop_files) {
241         $file =~ /^(.*\/)?(.+?\.$desktop_extension$)$/;
242         if ($1) {
243             $ign = "$1.cvsignore";
244         } else {
245             $ign = ".cvsignore";
246         }
247         my $basename = $2;
248
249         next if contains($ign, $basename);
250             
251         print "Appending $basename to $ign\n" if $VERBOSE;
252         open FILE, ">>$ign";
253         print FILE "$basename\n";
254         $all .= "$file ";
255     }
256     close FILE;
257     unless ($all eq ' ') {
258         print "*** Please remove files from CVS by following command: ***\n"
259             . "cvs remove -f$all\n";
260     }
261 }
262
263 sub fix_makefiles
264 {
265     my $file;
266     foreach $file (@desktop_files) {
267         my ($makefile, $line);
268
269         $file =~ /^(.*\/)?(.+?\.$desktop_extension$)$/;
270         if ($1) {
271             $makefile = "$1Makefile.am";
272         } else {
273             $makefile = "Makefile.am";
274         }
275         my $basename = $2;
276         print "Fixing $basename entry in $makefile\n" if $VERBOSE;
277
278         open MAKE, $makefile;
279         open NEWMAKE, ">$makefile.new";
280         my $extra = 0;
281         while ($line = <MAKE>) {
282             $extra = 1 if $line =~ /^EXTRA_DIST/;
283             if ($extra) {
284                 if (($line =~ /$basename/) &&
285                     !($line =~ /$basename\.in/)) {
286                     $line =~ s/$basename/$basename\.in/;
287                 }
288                 $extra = 0 unless $line =~ /\\\s*$/
289             } else {
290                 if ($line =~ /^(\w+)_DATA\s*=\s*$basename\s*$/) {
291                     my $name = $1;
292                     $line =~ s/^$name\_DATA/$name\_in_files/;
293                     $line =~ s/$basename/$basename\.in/;
294                     $basename =~ /\.($desktop_extension)$/;
295                     my $ext = $1;
296                     $line .= "$name\_DATA = \$($name\_in_files:.$ext.in=.$ext)\n";
297                     $ext =~ tr/a-z/A-Z/;
298                     if (!contains($makefile, "\@INTLTOOL_$ext\_RULE\@")) {
299                         $line .= "\@INTLTOOL_$ext\_RULE\@\n";
300                     }
301                 }
302             }
303             print NEWMAKE $line;
304         }
305         close NEWMAKE;
306         rename "$makefile.new", $makefile;
307     }
308 }
309
310 sub contains
311 {
312     my ($name, $str) = @_;
313     open CONT, "<$name";
314     while (<CONT>) {
315          chomp;
316          return 1 if $_ eq $str;
317     }
318     return 0;
319 }
320
321 sub find_desktop_files
322 {
323     if ($VERBOSE) {
324         print "Found these interesting files:\n";
325     } else {
326         print "Finding interesting files...";
327     }
328     find (\&wanted, '.');
329     print "done\n" unless $VERBOSE;
330 }
331
332 sub wanted
333 {
334     if (/\.$desktop_extension$/) {
335         my $file = $File::Find::name;
336         $file =~ s/\.\///;
337         push @desktop_files, $file;
338         print "$file\n" if $VERBOSE;
339     }
340 }
341
342 # vim: ts=4 sw=4 expandtab