1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28package installer::languagepack;
29
30use installer::converter;
31use installer::existence;
32use installer::files;
33use installer::globals;
34use installer::logger;
35use installer::pathanalyzer;
36use installer::scpzipfiles;
37use installer::scriptitems;
38use installer::systemactions;
39use installer::worker;
40
41####################################################
42# Selecting all files with the correct language
43####################################################
44
45sub select_language_items
46{
47	my ( $itemsref, $languagesarrayref, $itemname ) = @_;
48
49	installer::logger::include_header_into_logfile("Selecting languages for language pack. Item: $itemname");
50
51	my @itemsarray = ();
52
53	for ( my $i = 0; $i <= $#{$itemsref}; $i++ )
54	{
55		my $oneitem = ${$itemsref}[$i];
56
57		my $ismultilingual = $oneitem->{'ismultilingual'};
58
59		if (!($ismultilingual))
60		{
61			# Files with style "LANGUAGEPACK" and "FORCELANGUAGEPACK" also have to be included into the language pack.
62			# Files with style "LANGUAGEPACK" are only included into language packs.
63			# Files with style "FORCELANGUAGEPACK" are included into language packs and non language packs. They are
64			# forced, because otherwise they not not be included into languagepacks.
65
66			my $styles = "";
67			if ( $oneitem->{'Styles'} ) { $styles = $oneitem->{'Styles'}; }
68
69			if (( $styles =~ /\bLANGUAGEPACK\b/ ) || ( $styles =~ /\bFORCELANGUAGEPACK\b/ )) { push(@itemsarray, $oneitem); }
70
71			next; 	# single language files are not included into language pack
72		}
73
74		my $specificlanguage = "";
75		if ( $oneitem->{'specificlanguage'} ) { $specificlanguage = $oneitem->{'specificlanguage'}; }
76
77		for ( my $j = 0; $j <= $#{$languagesarrayref}; $j++ )	# iterating over all languages
78		{
79			my $onelanguage = ${$languagesarrayref}[$j];
80			my $locallang = $onelanguage;
81			$locallang =~ s/-/_/;
82
83			if ( $specificlanguage eq $onelanguage )
84			{
85				# $oneitem->{'modules'} = $installer::globals::rootmodulegid; 	 # all files in a language pack are root files
86				# Using $installer::globals::languagemodulesbase (?)
87
88#				# no more automatic change of module assignments
89#				$oneitem->{'modules'} = $installer::globals::rootmodulegid . "_$locallang"; 	 # all files in a language pack are root files
90#
91#				if (( $installer::globals::islinuxbuild ) || ( $installer::globals::issolarispkgbuild ))
92#				{
93#					if ( $oneitem->{'Dir'} )
94#					{
95#						if ( $oneitem->{'Dir'} eq "gid_Dir_Fonts_Truetype" ) { $oneitem->{'modules'} = "gid_Module_Langpack_Fonts_$locallang"; }
96#						if ( $oneitem->{'Dir'} eq "gid_Dir_Resource" ) { $oneitem->{'modules'} = "gid_Module_Langpack_Resource_$locallang"; }
97#						if ( $oneitem->{'Dir'} eq "gid_Dir_Help_Isolanguage" ) { $oneitem->{'modules'} = "gid_Module_Langpack_Help_$locallang"; }
98#					}
99#				}
100
101				# preparing different modules for Windows Installer language packs
102				# my $underlinelanguage = $specificlanguage;
103				# $underlinelanguage =~ s/-/_/;
104				# if ( $installer::globals::iswindowsbuild ) { $oneitem->{'modules'} = $installer::globals::languagemodulesbase . $underlinelanguage; }
105
106#               # no more collecting of language pack feature
107#				if (! installer::existence::exists_in_array($oneitem->{'modules'}, \@installer::globals::languagepackfeature))
108#				{
109#					push(@installer::globals::languagepackfeature, $oneitem->{'modules'});	# Collecting all language pack feature
110#				}
111
112				push(@itemsarray, $oneitem);
113			}
114		}
115	}
116
117	return \@itemsarray;
118}
119
120sub replace_languagestring_variable
121{
122	my ($onepackageref, $languagestringref) = @_;
123
124	my $key;
125
126	foreach $key (keys %{$onepackageref})
127	{
128		my $value = $onepackageref->{$key};
129		$value =~ s/\%LANGUAGESTRING/$$languagestringref/g;
130		$onepackageref->{$key} = $value;
131	}
132}
133
134#########################################################
135# Including the license text into the script template
136#########################################################
137
138sub put_license_file_into_script
139{
140	my ($scriptfile, $licensefile) = @_;
141
142	my $infoline = "Adding licensefile into language pack script\n";
143	push( @installer::globals::logfileinfo, $infoline);
144
145	my $includestring = "";
146
147	for ( my $i = 0; $i <= $#{$licensefile}; $i++ )
148	{
149		$includestring = $includestring . ${$licensefile}[$i];
150	}
151
152	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
153	{
154		${$scriptfile}[$i] =~ s/LICENSEFILEPLACEHOLDER/$includestring/;
155	}
156}
157
158#########################################################
159# Creating a tar.gz file from a Solaris package
160#########################################################
161
162sub create_tar_gz_file
163{
164	my ($installdir, $packagename, $packagestring) = @_;
165
166	$packagename =~ s/\.rpm\s*$//;
167	my $targzname = $packagename . ".tar.gz";
168	$systemcall = "cd $installdir; tar -cf - $packagestring | gzip > $targzname";
169	installer::logger::print_message( "... $systemcall ...\n" );
170
171	my $returnvalue = system($systemcall);
172
173	my $infoline = "Systemcall: $systemcall\n";
174	push( @installer::globals::logfileinfo, $infoline);
175
176	if ($returnvalue)
177	{
178		$infoline = "ERROR: Could not execute \"$systemcall\"!\n";
179		push( @installer::globals::logfileinfo, $infoline);
180	}
181	else
182	{
183		$infoline = "Success: Executed \"$systemcall\" successfully!\n";
184		push( @installer::globals::logfileinfo, $infoline);
185	}
186
187	return $targzname;
188}
189
190#########################################################
191# Determining the name of the package file
192#########################################################
193
194sub get_packagename_from_packagelist
195{
196	my ( $alldirs, $allvariables, $languagestringref ) = @_;
197
198	# my $packagename = "";
199
200	# for ( my $i = 0; $i <= $#{$alldirs}; $i++ )
201	# {
202	#	if ( ${$alldirs}[$i] =~ /-fonts/ ) { next; }
203	#	if ( ${$alldirs}[$i] =~ /-help/ ) { next; }
204	#	if ( ${$alldirs}[$i] =~ /-res/ ) { next; }
205	#
206	#	$packagename = ${$alldirs}[$i];
207	#	last;
208	# }
209
210	# if ( $packagename eq "" ) { installer::exiter::exit_program("ERROR: Could not find base package in directory $installdir!", "get_packagename_from_packagelist"); }
211
212	my $localproductname = $allvariables->{'PRODUCTNAME'};
213	$localproductname = lc($localproductname);
214	$localproductname =~ s/ //g;
215	$localproductname =~ s/-/_/g;
216
217	my $packagename = $localproductname . "_" . $$languagestringref;
218
219	return $packagename;
220}
221
222#########################################################
223# Determining the name of the package file or the rpm
224# in the installation directory. For language packs
225# there is only one file in this directory
226#########################################################
227
228sub determine_packagename
229{
230	my ( $installdir, $allvariables, $languagestringref ) = @_;
231
232	my $packagename = "";
233	my $allnames = "";
234
235	if ( $installer::globals::islinuxrpmbuild )
236	{
237		# determining the rpm file in directory $installdir
238
239		my $fileextension = "rpm";
240		my $rpmfiles = installer::systemactions::find_file_with_file_extension($fileextension, $installdir);
241		if ( ! ( $#{$rpmfiles} > -1 )) { installer::exiter::exit_program("ERROR: Could not find package in directory $installdir!", "determine_packagename"); }
242		my $rpmsav = installer::converter::copy_array_from_references($rpmfiles);
243		for ( my $i = 0; $i <= $#{$rpmfiles}; $i++ ) { installer::pathanalyzer::make_absolute_filename_to_relative_filename(\${$rpmfiles}[$i]); }
244
245		$packagename = get_packagename_from_packagelist($rpmfiles, $allvariables, $languagestringref);
246
247		my $packagestring = installer::converter::convert_array_to_space_separated_string($rpmfiles);
248		$packagename = create_tar_gz_file($installdir, $packagename, $packagestring);	# only one file
249		for ( my $i = 0; $i <= $#{$rpmsav}; $i++ )
250		{
251			my $onefile = $installdir . $installer::globals::separator . ${$rpmsav}[$i];
252			unlink($onefile);
253		}
254
255		$allnames = $rpmfiles;
256	}
257
258	if ( $installer::globals::issolarisbuild )
259	{
260		# determining the Solaris package file in directory $installdir
261		my $alldirs = installer::systemactions::get_all_directories($installdir);
262
263		if ( ! ( $#{$alldirs} > -1 )) { installer::exiter::exit_program("ERROR: Could not find package in directory $installdir!", "determine_packagename"); }
264		my $alldirssav = installer::converter::copy_array_from_references($alldirs);
265		for ( my $i = 0; $i <= $#{$alldirs}; $i++ ) { installer::pathanalyzer::make_absolute_filename_to_relative_filename(\${$alldirs}[$i]); }
266
267		$packagename = get_packagename_from_packagelist($alldirs, $allvariables, $languagestringref);
268		my $packagestring = installer::converter::convert_array_to_space_separated_string($alldirs);
269		$packagename = create_tar_gz_file($installdir, $packagename, $packagestring);	# only a file (not a directory) can be included into the shell script
270		for ( my $i = 0; $i <= $#{$alldirssav}; $i++ ) { installer::systemactions::remove_complete_directory(${$alldirssav}[$i], 1); }
271		$allnames = $alldirs;
272	}
273
274	my $infoline = "Found package in installation directory $installdir : $packagename\n";
275	push( @installer::globals::logfileinfo, $infoline);
276
277	return ( $packagename, $allnames);
278}
279
280#########################################################
281# Including the name of the package file or the rpm
282# into the script template
283#########################################################
284
285sub put_packagename_into_script
286{
287	my ($scriptfile, $packagename, $allnames) = @_;
288
289	my $localpackagename = $packagename;
290	$localpackagename =~ s/\.tar\.gz//;	# making "OOOopenoffice-it-ea.tar.gz" to "OOOopenoffice-it-ea"
291	my $infoline = "Adding packagename $localpackagename into language pack script\n";
292	push( @installer::globals::logfileinfo, $infoline);
293
294	my $installline = "";
295
296	if ( $installer::globals::issolarisbuild ) { $installline = "  /usr/sbin/pkgadd -d \$outdir -a \$adminfile"; }
297
298	if ( $installer::globals::islinuxrpmbuild ) { $installline = "  rpm --prefix \$PRODUCTINSTALLLOCATION --replacepkgs -i"; }
299
300	for ( my $i = 0; $i <= $#{$allnames}; $i++ )
301	{
302		if ( $installer::globals::issolarisbuild ) { $installline = $installline . " ${$allnames}[$i]"; }
303
304		if ( $installer::globals::islinuxrpmbuild ) { $installline = $installline . " \$outdir/${$allnames}[$i]"; }
305	}
306
307	for ( my $j = 0; $j <= $#{$scriptfile}; $j++ )
308	{
309		${$scriptfile}[$j] =~ s/INSTALLLINES/$installline/;
310	}
311}
312
313##################################################################
314# Including the lowercase product name into the script template
315##################################################################
316
317sub put_productname_into_script
318{
319	my ($scriptfile, $variableshashref) = @_;
320
321	my $productname = $variableshashref->{'PRODUCTNAME'};
322	$productname = lc($productname);
323	$productname =~ s/\.//g;	# openoffice.org -> openofficeorg
324
325	my $infoline = "Adding productname $productname into language pack script\n";
326	push( @installer::globals::logfileinfo, $infoline);
327
328	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
329	{
330		${$scriptfile}[$i] =~ s/PRODUCTNAMEPLACEHOLDER/$productname/;
331	}
332}
333
334##################################################################
335# Including the full product name into the script template
336# (name and version)
337##################################################################
338
339sub put_fullproductname_into_script
340{
341	my ($scriptfile, $variableshashref) = @_;
342
343	my $productname = $variableshashref->{'PRODUCTNAME'};
344	my $productversion = "";
345	if ( $variableshashref->{'PRODUCTVERSION'} ) { $productversion = $variableshashref->{'PRODUCTVERSION'}; };
346	my $fullproductname = $productname . " " . $productversion;
347
348	my $infoline = "Adding full productname \"$fullproductname\" into language pack script\n";
349	push( @installer::globals::logfileinfo, $infoline);
350
351	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
352	{
353		${$scriptfile}[$i] =~ s/FULLPRODUCTNAMELONGPLACEHOLDER/$fullproductname/;
354	}
355}
356
357##################################################################
358# Including the name of the search package (-core01)
359# into the script template
360##################################################################
361
362sub put_searchpackage_into_script
363{
364	my ($scriptfile, $variableshashref) = @_;
365
366	my $basispackageprefix = $variableshashref->{'BASISPACKAGEPREFIX'};
367	my $basispackageversion = $variableshashref->{'OOOBASEVERSION'};
368
369	if ( $installer::globals::issolarisbuild ) { $basispackageversion =~ s/\.//g; }	# "3.0" -> "30"
370
371	my $infoline = "Adding basis package prefix $basispackageprefix into language pack script\n";
372	push( @installer::globals::logfileinfo, $infoline);
373
374	$infoline = "Adding basis package version $basispackageversion into language pack script\n";
375	push( @installer::globals::logfileinfo, $infoline);
376
377	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
378	{
379		${$scriptfile}[$i] =~ s/BASISPACKAGEPREFIXPLACEHOLDER/$basispackageprefix/;
380		${$scriptfile}[$i] =~ s/OOOBASEVERSIONPLACEHOLDER/$basispackageversion/;
381	}
382
383}
384
385#########################################################
386# Including the linenumber into the script template
387#########################################################
388
389sub put_linenumber_into_script
390{
391	my ( $scriptfile, $licensefile, $allnames ) = @_;
392
393	my $linenumber =  $#{$scriptfile} + $#{$licensefile} + 3;	# also adding the content of the license file!
394
395	my $infoline = "Adding linenumber $linenumber into language pack script\n";
396	push( @installer::globals::logfileinfo, $infoline);
397
398	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
399	{
400		${$scriptfile}[$i] =~ s/LINENUMBERPLACEHOLDER/$linenumber/;
401	}
402}
403
404#########################################################
405# Determining the name of the new scriptfile
406#########################################################
407
408sub determine_scriptfile_name
409{
410	my ( $packagename ) = @_;
411
412	my $scriptfilename = $packagename;
413
414#	if ( $installer::globals::islinuxrpmbuild ) { $scriptfilename =~ s/\.rpm\s*$/\.sh/; }
415#	if ( $installer::globals::issolarisbuild ) { $scriptfilename =~ s/\.tar\.gz\s*$/\.sh/; }
416
417	$scriptfilename =~ s/\.tar\.gz\s*$/\.sh/;
418
419	my $infoline = "Setting language pack script file name to $scriptfilename\n";
420	push( @installer::globals::logfileinfo, $infoline);
421
422	return $scriptfilename;
423}
424
425#########################################################
426# Saving the script file in the installation directory
427#########################################################
428
429sub save_script_file
430{
431	my ($installdir, $newscriptfilename, $scriptfile) = @_;
432
433	$newscriptfilename = $installdir . $installer::globals::separator . $newscriptfilename;
434	installer::files::save_file($newscriptfilename, $scriptfile);
435
436	my $infoline = "Saving script file $newscriptfilename\n";
437	push( @installer::globals::logfileinfo, $infoline);
438
439	return $newscriptfilename;
440}
441
442#########################################################
443# Including the binary package into the script
444#########################################################
445
446sub include_package_into_script
447{
448	my ( $scriptfilename, $installdir, $packagename ) = @_;
449
450	my $longpackagename = $installdir . $installer::globals::separator . $packagename;
451	my $systemcall = "cat $longpackagename >>$scriptfilename";
452
453	my $returnvalue = system($systemcall);
454
455	my $infoline = "Systemcall: $systemcall\n";
456	push( @installer::globals::logfileinfo, $infoline);
457
458	if ($returnvalue)
459	{
460		$infoline = "ERROR: Could not execute \"$systemcall\"!\n";
461		push( @installer::globals::logfileinfo, $infoline);
462	}
463	else
464	{
465		$infoline = "Success: Executed \"$systemcall\" successfully!\n";
466		push( @installer::globals::logfileinfo, $infoline);
467	}
468
469	my $localcall = "chmod 775 $scriptfilename \>\/dev\/null 2\>\&1";
470	system($localcall);
471
472}
473
474#########################################################
475# Removing the binary package
476#########################################################
477
478sub remove_package
479{
480	my ( $installdir, $packagename ) = @_;
481
482	my $remove_package = 1;
483
484	if ( $ENV{'DONT_REMOVE_PACKAGE'} ) { $remove_package = 0; }
485
486	if ( $remove_package )
487	{
488		my $longpackagename = $installdir . $installer::globals::separator . $packagename;
489		unlink $longpackagename;
490
491		my $infoline = "Removing package: $longpackagename \n";
492		push( @installer::globals::logfileinfo, $infoline);
493	}
494}
495
496####################################################
497# Unix language packs, that are not part of
498# multilingual installation sets, need a
499# shell script installer
500####################################################
501
502sub build_installer_for_languagepack
503{
504	my ($installdir, $allvariableshashref, $includepatharrayref, $languagesarrayref, $languagestringref) = @_;
505
506	installer::logger::print_message( "... creating shell script installer ...\n" );
507
508	installer::logger::include_header_into_logfile("Creating shell script installer:");
509
510	# find and read setup script template
511
512	my $scriptfilename = "langpackscript.sh";
513	my $scriptref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$scriptfilename, $includepatharrayref, 0);
514	if ($$scriptref eq "") { installer::exiter::exit_program("ERROR: Could not find script file $scriptfilename!", "build_installer_for_languagepack"); }
515	my $scriptfile = installer::files::read_file($$scriptref);
516
517	my $infoline = "Found  script file $scriptfilename: $$scriptref \n";
518	push( @installer::globals::logfileinfo, $infoline);
519
520	# find and read english license file
521	my $licenselanguage = "en-US";					# always english !
522	my $licensefilename = "LICENSE_" . $licenselanguage;
523	my $licenseincludepatharrayref = installer::worker::get_language_specific_include_pathes($includepatharrayref, $licenselanguage);
524
525	my $licenseref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$licensefilename, $licenseincludepatharrayref, 0);
526	if ($$licenseref eq "") { installer::exiter::exit_program("ERROR: Could not find License file $licensefilename!", "build_installer_for_languagepack"); }
527	my $licensefile = installer::files::read_file($$licenseref);
528
529	$infoline = "Found licensefile $licensefilename: $$licenseref \n";
530	push( @installer::globals::logfileinfo, $infoline);
531
532	# including variables into license file
533	installer::scpzipfiles::replace_all_ziplistvariables_in_file($licensefile, $allvariableshashref);
534
535	# add license text into script template
536	put_license_file_into_script($scriptfile, $licensefile);
537
538	# add rpm or package file name into script template
539	my ( $packagename, $allnames) = determine_packagename($installdir, $allvariableshashref, $languagestringref);
540	put_packagename_into_script($scriptfile, $packagename, $allnames);
541
542	# add product name into script template
543	put_productname_into_script($scriptfile, $allvariableshashref);
544
545	# add product name into script template
546	put_fullproductname_into_script($scriptfile, $allvariableshashref);
547
548	# add product name into script template
549	put_searchpackage_into_script($scriptfile, $allvariableshashref);
550
551	# replace linenumber in script template
552	put_linenumber_into_script($scriptfile, $licensefile, $allnames);
553
554	# saving the script file
555	my $newscriptfilename = determine_scriptfile_name($packagename);
556	$newscriptfilename = save_script_file($installdir, $newscriptfilename, $scriptfile);
557
558	# include rpm or package into script
559	include_package_into_script($newscriptfilename, $installdir, $packagename);
560
561	# remove rpm or package
562	remove_package($installdir, $packagename);
563}
564
5651;
566