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