1: # -*- perl -*-
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#*************************************************************************
5#
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
14# OpenOffice.org is free software: you can redistribute it and/or modify
15# it under the terms of the GNU Lesser General Public License version 3
16# only, as published by the Free Software Foundation.
17#
18# OpenOffice.org is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU Lesser General Public License version 3 for more details
22# (a copy is included in the LICENSE file that accompanied this code).
23#
24# You should have received a copy of the GNU Lesser General Public License
25# version 3 along with OpenOffice.org.  If not, see
26# <http://www.openoffice.org/license.html>
27# for a copy of the LGPLv3 License.
28#
29#*************************************************************************
30
31# create java installer property files for all languages defined in jlf file
32
33use Cwd;
34use File::Copy;
35
36if( $#ARGV < 2 )
37  {
38    print <<ENDHELP;
39USAGE: $0 <separator> <jlf_file_path> <outputpath>
40    <separator>: separator, used on the platform (slash or backslash)
41    <jlf_file_path>: path, in which the jlf file(s) can be found
42    <outputpath>: path, in which the property files will be created
43ENDHELP
44  exit;
45  }
46
47$separator = $ARGV[0];
48$inputpath = $ARGV[1];
49$outputpath = $ARGV[2];
50
51$inputpath =~ s/\Q$separator\E\s*$//;
52$outputpath =~ s/\Q$separator\E\s*$//;
53
54if ( ! -d $outputpath ) { mkdir $outputpath; }
55
56print "Separator: $separator \n";
57print "Input path: $inputpath \n";
58print "Output path: $outputpath \n";
59
60my $localdir = cwd();
61my $all_template_files = read_directory($localdir, "properties");
62my $all_jlf_files = read_directory($inputpath, "jlf");
63my $defaultlanguage = "en-US";
64my $missing_jlf_file = "setupfiles.jlf";
65my $alllanguages = get_all_languages($all_jlf_files);
66my @allnewpropertyfiles = ();
67
68for ( my $i = 0; $i <= $#{$all_template_files}; $i++ )
69{
70  my $template_file_name = ${$all_template_files}[$i];
71  my $complete_template_file_name = $localdir . $separator . $template_file_name;
72  my $jlf_file_name = get_jlf_file_name($template_file_name);
73  my $complete_jlf_file_name = $inputpath . $separator . $jlf_file_name;
74  print "Using template file: $complete_template_file_name\n";
75  my $jlf_file = "";
76  if ( ! ( $jlf_file_name eq $missing_jlf_file ))
77  {
78    print "Using translation file: $complete_jlf_file_name\n";
79    $jlf_file = read_file($complete_jlf_file_name);
80  }
81
82  for ( my $j = 0; $j <= $#{$alllanguages}; $j++ )
83  {
84    my $language = ${$alllanguages}[$j];
85    my $template_file = read_file($complete_template_file_name);
86    my $stringhash = create_string_hash($jlf_file, $language);
87    create_property_file($template_file, $stringhash);
88    my $filename = generate_filename($template_file_name, $language);
89
90    if ( $language eq $defaultlanguage )
91    {
92      # Creating language indenpendent english file
93      make_propertyfile_language_independent($template_file);
94      $filename = generate_filename($template_file_name, "");
95      save_file($outputpath, $filename, $template_file);
96    }
97    else
98    {
99      # Saving the non-english files
100      save_file($outputpath, $filename, $template_file);
101    }
102  }
103}
104
105exit;
106
107sub main::read_directory
108{
109  my ($dir, $ext) = @_;
110
111  my @content = ();
112  my $direntry;
113  opendir(DIR, $dir);
114
115  foreach $direntry (readdir (DIR))
116  {
117    next if $direntry eq ".";
118    next if $direntry eq "..";
119    next if ( ! ( $direntry =~ /\.\Q$ext\E\s*$/ ));
120
121    # my $completeentry = $dir . $separator . $direntry;
122    # push(@content, $completeentry);
123    push(@content, $direntry);
124  }
125
126  closedir(DIR);
127  return \@content;
128}
129
130sub main::read_file
131{
132  my ($filename) = @_;
133
134  open( IN, "<$filename" ) || die "cannot open $filename";
135  my @content = <IN>;
136  close( IN );
137
138  return \@content;
139}
140
141sub main::get_jlf_file_name
142{
143  my ($tempfilename) = @_;
144
145  my $jlffilename = "";
146
147  if ( $tempfilename =~ /^\s*(\w+)_template/ ) { $tempfilename = $1; }
148  $jlffilename = $tempfilename . "\.jlf";
149
150  return $jlffilename;
151}
152
153sub main::get_all_languages
154{
155  my ($alljlffiles) = @_;
156
157  my @languages = ();
158  my $record = 0;
159
160  my $first_jlf_file_name = $inputpath . $separator . ${$alljlffiles}[0];
161  my $jlffile = read_file($first_jlf_file_name);
162
163  for ( my $i = 0; $i <= $#{$jlffile}; $i++ )
164  {
165    if (( ${$jlffile}[$i] =~ /^\s*\[.*]\s*$/ ) && ( $record )) { last; }
166    if (( ${$jlffile}[$i] =~ /^\s*\[.*]\s*$/ ) && ( $record == 0 )) { $record = 1; }
167
168    if (( $record ) && ( ${$jlffile}[$i] =~ /^\s*(.+?)\s*\=/ ))
169    {
170      $language = $1;
171      push(@languages, $language);
172    }
173  }
174
175  my $languagestring = "";
176  for ( my $i = 0; $i <= $#languages; $i++ ) { $languagestring = $languagestring . $languages[$i] . ","; }
177  $languagestring =~ s/,\s*$//;
178  print "Languages: $languagestring\n";
179
180  return \@languages;
181}
182
183sub main::create_string_hash
184{
185  my ($jlffile, $language) = @_;
186
187  my %stringhash = ();
188  my $key = "";
189  my $value_defined = 0;
190
191  for ( my $i = 0; $i <= $#{$jlffile}; $i++ )
192  {
193    if ( ${$jlffile}[$i] =~ /^\s*\[(.*)\]\s*$/ )
194    {
195      $key = $1;
196      $value_defined = 0;
197    }
198
199    if (( ${$jlffile}[$i] =~ /^\s*\Q$defaultlanguage\E\s*=\s*\"(.*)\"\s*$/ ) && ( ! $value_defined ))
200    {
201      $value = $1;	# defaulting to english
202      $stringhash{$key} = $value;
203    }
204
205    if (( ${$jlffile}[$i] =~ /^\s*\Q$language\E\s*=\s*\"(.*)\"\s*$/ ) && ( ! $value_defined ))
206    {
207      $value = $1;
208      $stringhash{$key} = $value;
209      $value_defined = 1;
210    }
211  }
212
213  # additional replacement for ${LANGUAGE}, not defined in jlf file
214  my $languagekey = "LANGUAGE";
215  $stringhash{$languagekey} = $language;
216
217  # print_hash(\%stringhash);
218
219  return \%stringhash;
220}
221
222sub main::print_hash
223{
224  my ( $hashref ) = @_;
225
226  print "Hash contains:\n";
227
228  my $key;
229  foreach $key (keys %{$hashref} ) { print "Key: $key, Value: $hashref->{$key}\n"; }
230}
231
232sub main::create_property_file
233{
234  my ($template_file, $stringhash) = @_;
235
236  for ( my $i = 0; $i <= $#{$template_file}; $i++ )
237  {
238    if ( ${$template_file}[$i] =~ /\$\{(\w+)\}/ )
239    {
240      my $key = $1;
241
242      if ( exists($stringhash->{$key}) )
243      {
244        my $value = $stringhash->{$key};
245        ${$template_file}[$i] =~ s/\$\{\Q$key\E\}/$value/g;
246      }
247      else
248      {
249        print "Error: No value found for key: $key\n";
250        exit;
251      }
252    }
253  }
254}
255
256sub main::generate_filename
257{
258  my ($template_file_name, $onelanguage) = @_;
259
260  my $filename = $template_file_name;
261
262  if ( $onelanguage )
263  {
264    $onelanguage =~ s/-/_/;   # zh-TW -> zh_TW
265    $onelanguage = "_" . $onelanguage;
266    $filename =~ s/_template\./$onelanguage\./;
267  }
268  else
269  {
270    $filename =~ s/_template//;
271  }
272
273  return $filename;
274}
275
276sub make_propertyfile_language_independent
277{
278  my ($property_file) = @_;
279
280  for ( my $i = 0; $i <= $#{$property_file}; $i++ )
281  {
282#    if ( ${$property_file}[$i] =~ /^\s*#/ ) # only comment lines
283#    {
284      ${$property_file}[$i] =~ s/_\Q$defaultlanguage\E//;
285#    }
286  }
287}
288
289sub main::save_file
290{
291  my ($outputpath, $filename, $filecontent) = @_;
292
293  $filename = $outputpath . $separator . $filename;
294
295  if ( open( OUT, ">$filename" ) )
296  {
297    print OUT @{$filecontent};
298    close( OUT);
299  }
300
301  push(@allnewpropertyfiles, $filename);
302  print "Created file: $filename\n";
303}
304