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