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