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