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 help files in html format for all languages defined in ulf 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> <ulf_file_path> <outputpath>
40*cdf0e10cSrcweir    <separator>: separator, used on the platform (slash or backslash)
41*cdf0e10cSrcweir    <ulf_file_path>: path, in which the ulf file(s) can be found
42*cdf0e10cSrcweir    <outputpath>: path, in which the help 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*cdf0e10cSrcweir
62*cdf0e10cSrcweirmy $ulffilename = "setupstrings.ulf";
63*cdf0e10cSrcweirmy $helpfilename = "helpfilenames.txt";
64*cdf0e10cSrcweirmy $defaultlanguage = "en-US";
65*cdf0e10cSrcweir
66*cdf0e10cSrcweir$ulffilename = $inputpath . $separator . $ulffilename;
67*cdf0e10cSrcweirmy $ulffile = read_file($ulffilename);
68*cdf0e10cSrcweir
69*cdf0e10cSrcweirmy $helpfilenames = read_file($helpfilename);
70*cdf0e10cSrcweirmy $allhelpfilenames = collect_helpfile_names($helpfilenames);
71*cdf0e10cSrcweir
72*cdf0e10cSrcweirmy $alllanguages = get_all_languages($ulffile);
73*cdf0e10cSrcweirmy @allnewpropertyfiles = ();
74*cdf0e10cSrcweir
75*cdf0e10cSrcweirfor ( my $i = 0; $i <= $#{$allhelpfilenames}; $i++ )
76*cdf0e10cSrcweir{
77*cdf0e10cSrcweir  my $helpfilename = ${$allhelpfilenames}[$i];
78*cdf0e10cSrcweir
79*cdf0e10cSrcweir  for ( my $j = 0; $j <= $#{$alllanguages}; $j++ )
80*cdf0e10cSrcweir  {
81*cdf0e10cSrcweir    my $language = ${$alllanguages}[$j];
82*cdf0e10cSrcweir
83*cdf0e10cSrcweir    # Creating content of help file
84*cdf0e10cSrcweir    my $helpfilecontent = collect_helpfile_content($helpfilename, $ulffile, $language);
85*cdf0e10cSrcweir
86*cdf0e10cSrcweir    # Saving helpfile
87*cdf0e10cSrcweir    my $savefilename = $helpfilename . "_" . $language . ".html";
88*cdf0e10cSrcweir    $savefilename = $outputpath . $separator . $savefilename;
89*cdf0e10cSrcweir    save_file($savefilename, $helpfilecontent);
90*cdf0e10cSrcweir
91*cdf0e10cSrcweir    if ( $language eq $defaultlanguage )
92*cdf0e10cSrcweir    {
93*cdf0e10cSrcweir      $savefilename = $helpfilename . ".html";
94*cdf0e10cSrcweir      $savefilename = $outputpath . $separator . $savefilename;
95*cdf0e10cSrcweir      save_file($savefilename, $helpfilecontent);
96*cdf0e10cSrcweir    }
97*cdf0e10cSrcweir  }
98*cdf0e10cSrcweir}
99*cdf0e10cSrcweir
100*cdf0e10cSrcweirexit;
101*cdf0e10cSrcweir
102*cdf0e10cSrcweirsub main::read_directory
103*cdf0e10cSrcweir{
104*cdf0e10cSrcweir  my ($dir, $ext) = @_;
105*cdf0e10cSrcweir
106*cdf0e10cSrcweir  my @content = ();
107*cdf0e10cSrcweir  my $direntry;
108*cdf0e10cSrcweir  opendir(DIR, $dir);
109*cdf0e10cSrcweir
110*cdf0e10cSrcweir  foreach $direntry (readdir (DIR))
111*cdf0e10cSrcweir  {
112*cdf0e10cSrcweir    next if $direntry eq ".";
113*cdf0e10cSrcweir    next if $direntry eq "..";
114*cdf0e10cSrcweir    next if ( ! ( $direntry =~ /\.\Q$ext\E\s*$/ ));
115*cdf0e10cSrcweir
116*cdf0e10cSrcweir    # my $completeentry = $dir . $separator . $direntry;
117*cdf0e10cSrcweir    # push(@content, $completeentry);
118*cdf0e10cSrcweir    push(@content, $direntry);
119*cdf0e10cSrcweir  }
120*cdf0e10cSrcweir
121*cdf0e10cSrcweir  closedir(DIR);
122*cdf0e10cSrcweir  return \@content;
123*cdf0e10cSrcweir}
124*cdf0e10cSrcweir
125*cdf0e10cSrcweirsub main::read_file
126*cdf0e10cSrcweir{
127*cdf0e10cSrcweir  my ($filename) = @_;
128*cdf0e10cSrcweir
129*cdf0e10cSrcweir  open( IN, "<$filename" ) || die "cannot open $filename";
130*cdf0e10cSrcweir  my @content = <IN>;
131*cdf0e10cSrcweir  close( IN );
132*cdf0e10cSrcweir
133*cdf0e10cSrcweir  return \@content;
134*cdf0e10cSrcweir}
135*cdf0e10cSrcweir
136*cdf0e10cSrcweirsub main::collect_helpfile_content
137*cdf0e10cSrcweir{
138*cdf0e10cSrcweir  my ($helpfilename, $ulffile, $language) = @_;
139*cdf0e10cSrcweir
140*cdf0e10cSrcweir  my @helpfilecontent = ();
141*cdf0e10cSrcweir  my $stringhash = create_string_hash($ulffile, $language);
142*cdf0e10cSrcweir
143*cdf0e10cSrcweir  # Collecting all strings for one html file.
144*cdf0e10cSrcweir  # For "Prologue_de.html" all files need to begin with "STRING_PROLOGUE_X"
145*cdf0e10cSrcweir  # The "X" is the ordering number.
146*cdf0e10cSrcweir
147*cdf0e10cSrcweir  my $basestring = "STRING_" . uc($helpfilename) . "_";
148*cdf0e10cSrcweir
149*cdf0e10cSrcweir  for ( my $i = 0; $i <= 10; $i++ )  # 10 strings possible for each html file
150*cdf0e10cSrcweir  {
151*cdf0e10cSrcweir    my $key = $basestring . $i;
152*cdf0e10cSrcweir    if ( exists $stringhash->{$key} )
153*cdf0e10cSrcweir    {
154*cdf0e10cSrcweir      my $content = $stringhash->{$key} . "\n<p>\n";
155*cdf0e10cSrcweir      push(@helpfilecontent, $content);
156*cdf0e10cSrcweir    }
157*cdf0e10cSrcweir  }
158*cdf0e10cSrcweir
159*cdf0e10cSrcweir  return \@helpfilecontent;
160*cdf0e10cSrcweir}
161*cdf0e10cSrcweir
162*cdf0e10cSrcweirsub main::collect_helpfile_names
163*cdf0e10cSrcweir{
164*cdf0e10cSrcweir	my ($helpfilecontent) = @_;
165*cdf0e10cSrcweir
166*cdf0e10cSrcweir	my @allhelpfiles = ();
167*cdf0e10cSrcweir
168*cdf0e10cSrcweir    for ( my $i = 0; $i <= $#{$helpfilecontent}; $i++ )
169*cdf0e10cSrcweir    {
170*cdf0e10cSrcweir		if ( ${$helpfilecontent}[$i] =~ /^\s*#/ ) { next; }  # comment line
171*cdf0e10cSrcweir		if ( ${$helpfilecontent}[$i] =~ /^\s*$/ ) { next; }  # empty line
172*cdf0e10cSrcweir		my $filename = ${$helpfilecontent}[$i];
173*cdf0e10cSrcweir		$filename =~ s/\s//g;
174*cdf0e10cSrcweir		push(@allhelpfiles, $filename);
175*cdf0e10cSrcweir	}
176*cdf0e10cSrcweir
177*cdf0e10cSrcweir	return \@allhelpfiles;
178*cdf0e10cSrcweir}
179*cdf0e10cSrcweir
180*cdf0e10cSrcweirsub main::get_all_languages
181*cdf0e10cSrcweir{
182*cdf0e10cSrcweir  my ($ulffile) = @_;
183*cdf0e10cSrcweir
184*cdf0e10cSrcweir  my @languages = ();
185*cdf0e10cSrcweir  my $record = 0;
186*cdf0e10cSrcweir
187*cdf0e10cSrcweir  for ( my $i = 0; $i <= $#{$ulffile}; $i++ )
188*cdf0e10cSrcweir  {
189*cdf0e10cSrcweir    if (( ${$ulffile}[$i] =~ /^\s*\[.*]\s*$/ ) && ( $record )) { last; }
190*cdf0e10cSrcweir    if (( ${$ulffile}[$i] =~ /^\s*\[.*]\s*$/ ) && ( $record == 0 )) { $record = 1; }
191*cdf0e10cSrcweir
192*cdf0e10cSrcweir    if (( $record ) && ( ${$ulffile}[$i] =~ /^\s*(.+?)\s*\=/ ))
193*cdf0e10cSrcweir    {
194*cdf0e10cSrcweir      $language = $1;
195*cdf0e10cSrcweir      push(@languages, $language);
196*cdf0e10cSrcweir    }
197*cdf0e10cSrcweir  }
198*cdf0e10cSrcweir
199*cdf0e10cSrcweir  my $languagestring = "";
200*cdf0e10cSrcweir  for ( my $i = 0; $i <= $#languages; $i++ ) { $languagestring = $languagestring . $languages[$i] . ","; }
201*cdf0e10cSrcweir  $languagestring =~ s/,\s*$//;
202*cdf0e10cSrcweir  print "Languages: $languagestring\n";
203*cdf0e10cSrcweir
204*cdf0e10cSrcweir  return \@languages;
205*cdf0e10cSrcweir}
206*cdf0e10cSrcweir
207*cdf0e10cSrcweirsub main::create_string_hash
208*cdf0e10cSrcweir{
209*cdf0e10cSrcweir  my ($ulffile, $language) = @_;
210*cdf0e10cSrcweir
211*cdf0e10cSrcweir  my %stringhash = ();
212*cdf0e10cSrcweir  my $key = "";
213*cdf0e10cSrcweir  my $value_defined = 0;
214*cdf0e10cSrcweir
215*cdf0e10cSrcweir  for ( my $i = 0; $i <= $#{$ulffile}; $i++ )
216*cdf0e10cSrcweir  {
217*cdf0e10cSrcweir    if ( ${$ulffile}[$i] =~ /^\s*\[(.*)\]\s*$/ )
218*cdf0e10cSrcweir    {
219*cdf0e10cSrcweir      $key = $1;
220*cdf0e10cSrcweir      $value_defined = 0;
221*cdf0e10cSrcweir    }
222*cdf0e10cSrcweir
223*cdf0e10cSrcweir    if (( ${$ulffile}[$i] =~ /^\s*\Q$defaultlanguage\E\s*=\s*\"(.*)\"\s*$/ ) && ( ! $value_defined ))
224*cdf0e10cSrcweir    {
225*cdf0e10cSrcweir      $value = $1;	# defaulting to english
226*cdf0e10cSrcweir      $stringhash{$key} = $value;
227*cdf0e10cSrcweir    }
228*cdf0e10cSrcweir
229*cdf0e10cSrcweir    if (( ${$ulffile}[$i] =~ /^\s*\Q$language\E\s*=\s*\"(.*)\"\s*$/ ) && ( ! $value_defined ))
230*cdf0e10cSrcweir    {
231*cdf0e10cSrcweir      $value = $1;
232*cdf0e10cSrcweir      $stringhash{$key} = $value;
233*cdf0e10cSrcweir      $value_defined = 1;
234*cdf0e10cSrcweir    }
235*cdf0e10cSrcweir  }
236*cdf0e10cSrcweir
237*cdf0e10cSrcweir  # additional replacement for ${LANGUAGE}, not defined in ulf file
238*cdf0e10cSrcweir  my $languagekey = "LANGUAGE";
239*cdf0e10cSrcweir  $stringhash{$languagekey} = $language;
240*cdf0e10cSrcweir
241*cdf0e10cSrcweir  # print_hash(\%stringhash);
242*cdf0e10cSrcweir
243*cdf0e10cSrcweir  return \%stringhash;
244*cdf0e10cSrcweir}
245*cdf0e10cSrcweir
246*cdf0e10cSrcweirsub main::print_hash
247*cdf0e10cSrcweir{
248*cdf0e10cSrcweir  my ( $hashref ) = @_;
249*cdf0e10cSrcweir
250*cdf0e10cSrcweir  print "Hash contains:\n";
251*cdf0e10cSrcweir
252*cdf0e10cSrcweir  my $key;
253*cdf0e10cSrcweir  foreach $key (keys %{$hashref} ) { print "Key: $key, Value: $hashref->{$key}\n"; }
254*cdf0e10cSrcweir}
255*cdf0e10cSrcweir
256*cdf0e10cSrcweirsub main::save_file
257*cdf0e10cSrcweir{
258*cdf0e10cSrcweir  my ($filename, $filecontent) = @_;
259*cdf0e10cSrcweir
260*cdf0e10cSrcweir  if ( open( OUT, ">$filename" ) )
261*cdf0e10cSrcweir  {
262*cdf0e10cSrcweir    print OUT @{$filecontent};
263*cdf0e10cSrcweir    close( OUT);
264*cdf0e10cSrcweir  }
265*cdf0e10cSrcweir
266*cdf0e10cSrcweir  push(@allnewpropertyfiles, $filename);
267*cdf0e10cSrcweir  print "Created file: $filename\n";
268*cdf0e10cSrcweir}
269