1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::download;
25
26use File::Spec;
27use installer::exiter;
28use installer::files;
29use installer::globals;
30use installer::logger;
31use installer::pathanalyzer;
32use installer::remover;
33use installer::systemactions;
34
35use strict;
36
37BEGIN { # This is needed so that cygwin's perl evaluates ACLs
38	# (needed for correctly evaluating the -x test.)
39	if( $^O =~ /cygwin/i ) {
40		require filetest; import filetest "access";
41	}
42}
43
44##################################################################
45# Including the lowercase product name into the script template
46##################################################################
47
48sub put_productname_into_script
49{
50	my ($scriptfile, $variableshashref) = @_;
51
52	my $productname = $variableshashref->{'PRODUCTNAME'};
53	$productname = lc($productname);
54	$productname =~ s/\.//g;	# openoffice.org -> openofficeorg
55	$productname =~ s/\s*//g;
56
57    $installer::logger::Lang->printf("Adding productname %s into download shell script\n", $productname);
58
59	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
60	{
61		${$scriptfile}[$i] =~ s/PRODUCTNAMEPLACEHOLDER/$productname/;
62	}
63}
64
65#########################################################
66# Including the linenumber into the script template
67#########################################################
68
69sub put_linenumber_into_script
70{
71	my ( $scriptfile ) = @_;
72
73	my $linenumber =  $#{$scriptfile} + 2;
74
75    $installer::logger::Lang->printf("Adding linenumber %d into download shell script\n", $linenumber);
76
77	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
78	{
79		${$scriptfile}[$i] =~ s/LINENUMBERPLACEHOLDER/$linenumber/;
80	}
81}
82
83#########################################################
84# Determining the name of the new scriptfile
85#########################################################
86
87sub determine_scriptfile_name
88{
89	my ( $filename ) = @_;
90
91	$installer::globals::downloadfileextension = ".sh";
92	$filename = $filename . $installer::globals::downloadfileextension;
93	$installer::globals::downloadfilename = $filename;
94
95    $installer::logger::Lang->printf("Setting download shell script file name to %s\n", $filename);
96
97	return $filename;
98}
99
100#########################################################
101# Saving the script file in the installation directory
102#########################################################
103
104sub save_script_file
105{
106	my ($directory, $newscriptfilename, $scriptfile) = @_;
107
108	$newscriptfilename = $directory . $installer::globals::separator . $newscriptfilename;
109	installer::files::save_file($newscriptfilename, $scriptfile);
110
111    $installer::logger::Lang->printf("Saving script file %s\n", $newscriptfilename);
112
113	if ( ! $installer::globals::iswindowsbuild )
114	{
115		my $localcall = "chmod 775 $newscriptfilename \>\/dev\/null 2\>\&1";
116		system($localcall);
117	}
118
119	return $newscriptfilename;
120}
121
122#########################################################
123# Including checksum and size into script file
124#########################################################
125
126sub put_checksum_and_size_into_script
127{
128	my ($scriptfile, $sumout) = @_;
129
130	my $checksum = "";
131	my $size = "";
132
133	if  ( $sumout =~ /^\s*(\d+)\s+(\d+)\s*$/ )
134	{
135		$checksum = $1;
136		$size = $2;
137	}
138	else
139	{
140		installer::exiter::exit_program("ERROR: Incorrect return value from /usr/bin/sum: $sumout", "put_checksum_and_size_into_script");
141	}
142
143    $installer::logger::Lang->printf(
144        "Adding checksum %s and size %s into download shell script\n", $checksum, $size);
145
146	for ( my $i = 0; $i <= $#{$scriptfile}; $i++ )
147	{
148		${$scriptfile}[$i] =~ s/CHECKSUMPLACEHOLDER/$checksum/;
149		${$scriptfile}[$i] =~ s/DISCSPACEPLACEHOLDER/$size/;
150	}
151
152}
153
154#########################################################
155# Calling md5sum
156#########################################################
157
158sub call_md5sum
159{
160	my ($filename) = @_;
161
162	my $md5sumfile = "/usr/bin/md5sum";
163
164	if ( ! -f $md5sumfile ) { installer::exiter::exit_program("ERROR: No file /usr/bin/md5sum", "call_md5sum"); }
165
166	my $systemcall = "$md5sumfile $filename |";
167
168	my $md5sumoutput = "";
169
170	open (SUM, "$systemcall");
171	$md5sumoutput = <SUM>;
172	close (SUM);
173
174	my $returnvalue = $?;	# $? contains the return value of the systemcall
175
176    $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
177
178	if ($returnvalue)
179	{
180        $installer::logger::Lang->printf("ERROR: Could not execute \"%s\"!\n", $systemcall);
181	}
182	else
183	{
184        $installer::logger::Lang->print("Success: Executed \"%s\" successfully!\n", $systemcall);
185	}
186
187	return $md5sumoutput;
188}
189
190#########################################################
191# Calling md5sum
192#########################################################
193
194sub get_md5sum
195{
196	my ($md5sumoutput) = @_;
197
198	my $md5sum;
199
200	if  ( $md5sumoutput =~ /^\s*(\w+?)\s+/ )
201	{
202		$md5sum = $1;
203	}
204	else
205	{
206		installer::exiter::exit_program("ERROR: Incorrect return value from /usr/bin/md5sum: $md5sumoutput", "get_md5sum");
207	}
208
209    $installer::logger::Lang->printf("Setting md5sum: %s\n", $md5sum);
210
211	return $md5sum;
212}
213
214#########################################################
215# Determining checksum and size of tar file
216#########################################################
217
218sub call_sum
219{
220	my ($filename, $getuidlibrary) = @_;
221
222	my $systemcall = "/usr/bin/sum $filename |";
223
224	my $sumoutput = "";
225
226	open (SUM, "$systemcall");
227	$sumoutput = <SUM>;
228	close (SUM);
229
230	my $returnvalue = $?;	# $? contains the return value of the systemcall
231
232    $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
233
234	if ($returnvalue)
235	{
236        $installer::logger::Lang->printf("ERROR: Could not execute \"%s\"!\n", $systemcall);
237	}
238	else
239	{
240        $installer::logger::Lang->printf("Success: Executed \"%s\" successfully!\n", $systemcall);
241	}
242
243	$sumoutput =~ s/\s+$filename\s$//;
244	return $sumoutput;
245}
246
247#########################################################
248# Searching for the getuid.so in the solver
249#########################################################
250
251sub get_path_for_library
252{
253	my ($includepatharrayref) = @_;
254
255	my $getuidlibraryname = "getuid.so";
256
257	my $getuidlibraryref = "";
258
259	if ( $installer::globals::include_pathes_read )
260	{
261		$getuidlibraryref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$getuidlibraryname, $includepatharrayref, 0);
262	}
263	else
264	{
265		$getuidlibraryref = installer::scriptitems::get_sourcepath_from_filename_and_includepath_classic(\$getuidlibraryname, $includepatharrayref, 0);
266	}
267
268	if ($$getuidlibraryref eq "") { installer::exiter::exit_program("ERROR: Could not find $getuidlibraryname!", "get_path_for_library"); }
269
270	return $$getuidlibraryref;
271}
272
273#########################################################
274# Include the tar file into the script
275#########################################################
276
277sub include_tar_into_script
278{
279	my ($scriptfile, $temporary_tarfile) = @_;
280
281	my $systemcall = "cat $temporary_tarfile >> $scriptfile && rm $temporary_tarfile";
282	my $returnvalue = system($systemcall);
283
284    $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
285
286	if ($returnvalue)
287	{
288        $installer::logger::Lang->printf("ERROR: Could not execute \"%s\"!\n", $systemcall);
289	}
290	else
291	{
292        $installer::logger::Lang->printf("Success: Executed \"%s\" successfully!\n", $systemcall);
293	}
294	return $returnvalue;
295}
296
297#########################################################
298# Create a tar file from the binary package
299#########################################################
300
301sub tar_package
302{
303	my ( $installdir, $tarfilename, $getuidlibrary) = @_;
304
305	my $ldpreloadstring = "";
306	if ( $getuidlibrary ne "" ) { $ldpreloadstring = "LD_PRELOAD=" . $getuidlibrary; }
307
308	my $systemcall = "cd $installdir; $ldpreloadstring tar -cf - * > $tarfilename";
309
310	my $returnvalue = system($systemcall);
311
312    $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
313
314	if ($returnvalue)
315	{
316        $installer::logger::Lang->printf("ERROR: Could not execute \"%s\"!\n", $systemcall);
317	}
318	else
319	{
320        $installer::logger::Lang->printf("Success: Executed \"\" successfully!\n", $systemcall);
321	}
322
323	my $localcall = "chmod 775 $tarfilename \>\/dev\/null 2\>\&1";
324	$returnvalue = system($localcall);
325
326	return ( -s $tarfilename );
327}
328
329#########################################################
330# Creating a tar.gz file
331#########################################################
332
333sub create_tar_gz_file_from_package
334{
335	my ($installdir, $getuidlibrary) = @_;
336
337	my $alldirs = installer::systemactions::get_all_directories($installdir);
338	my $onedir = ${$alldirs}[0];
339	$installdir = $onedir;
340
341	my $allfiles = installer::systemactions::get_all_files_from_one_directory($installdir);
342
343	for ( my $i = 0; $i <= $#{$allfiles}; $i++ )
344	{
345		my $onefile = ${$allfiles}[$i];
346		my $systemcall = "cd $installdir; rm $onefile";
347		my $returnvalue = system($systemcall);
348
349        $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
350
351		if ($returnvalue)
352		{
353            $installer::logger::Lang->printf("ERROR: Could not execute \"%s\"!\n", $systemcall);
354		}
355		else
356		{
357            $installer::logger::Lang->printf("Success: Executed \"%s\" successfully!\n", $systemcall);
358		}
359	}
360
361	$alldirs = installer::systemactions::get_all_directories($installdir);
362	my $packagename = ${$alldirs}[0]; # only taking the first Solaris package
363	if ( $packagename eq "" ) { installer::exiter::exit_program("ERROR: Could not find package in directory $installdir!", "determine_packagename"); }
364
365	installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$packagename);
366
367	$installer::globals::downloadfileextension = ".tar.gz";
368	my $targzname = $packagename . $installer::globals::downloadfileextension;
369	$installer::globals::downloadfilename = $targzname;
370	my $ldpreloadstring = "";
371	if ( $getuidlibrary ne "" ) { $ldpreloadstring = "LD_PRELOAD=" . $getuidlibrary; }
372
373	my $systemcall = "cd $installdir; $ldpreloadstring tar -cf - $packagename | gzip > $targzname";
374    $installer::logger::Info->printf("... %s ...\n", $systemcall);
375
376	my $returnvalue = system($systemcall);
377
378    $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
379
380	if ($returnvalue)
381	{
382        $installer::logger::Lang->printf("ERROR: Could not execute \"%s\"!\n", $systemcall);
383	}
384	else
385	{
386        $installer::logger::Lang->printf("Success: Executed \"%s\" successfully!\n", $systemcall);
387	}
388}
389
390#########################################################
391# Setting type of installation
392#########################################################
393
394sub get_installation_type
395{
396	my $type = "";
397
398	if ( $installer::globals::languagepack ) { $type = "langpack"; }
399	else { $type = "install"; }
400
401	return $type;
402}
403
404#########################################################
405# Setting installation languages
406#########################################################
407
408sub get_downloadname_language
409{
410	my ($languagestringref) = @_;
411
412	my $languages = $$languagestringref;
413
414	if ( $installer::globals::added_english )
415	{
416		$languages =~ s/en-US_//;
417		$languages =~ s/_en-US//;
418	}
419
420	# en-US is default language and can be removed therefore
421	# for one-language installation sets
422
423	# if ( $languages =~ /^\s*en-US\s*$/ )
424	# {
425	#	$languages = "";
426	# }
427
428	if ( length ($languages) > $installer::globals::max_lang_length )
429	{
430		$languages = 'multi';
431	}
432
433	return $languages;
434}
435
436#########################################################
437# Setting download name
438#########################################################
439
440sub get_downloadname_productname
441{
442    my ($allvariables) = @_;
443
444    my $start;
445
446    if ( $allvariables->{'AOODOWNLOADNAMEPREFIX'} )
447    {
448        $start = $allvariables->{'AOODOWNLOADNAMEPREFIX'};
449    }
450    else
451    {
452        $start = "Apache_OpenOffice";
453        if ( $allvariables->{'PRODUCTNAME'} eq "OpenOffice" )
454        {
455            if ( $allvariables->{'POSTVERSIONEXTENSION'} eq "SDK" )
456            {
457                $start .= "-SDK";
458            }
459        }
460
461        if ( $allvariables->{'PRODUCTNAME'} eq "AOO-Developer-Build" )
462        {
463            if ( $allvariables->{'POSTVERSIONEXTENSION'} eq "SDK" )
464            {
465                $start .= "-Dev-SDK";
466            }
467            else
468            {
469                $start .= "-Dev";
470            }
471        }
472
473        if ( $allvariables->{'PRODUCTNAME'} eq "URE" )
474        {
475            $start .= "-URE";
476        }
477    }
478
479    return $start;
480}
481
482#########################################################
483# Setting download version
484#########################################################
485
486sub get_download_version
487{
488	my ($allvariables) = @_;
489
490	my $version = "";
491
492	my $devproduct = 0;
493	if (( $allvariables->{'DEVELOPMENTPRODUCT'} ) && ( $allvariables->{'DEVELOPMENTPRODUCT'} == 1 )) { $devproduct = 1; }
494
495	my $cwsproduct = 0;
496	# the environment variable CWS_WORK_STAMP is set only in CWS
497	if ( $ENV{'CWS_WORK_STAMP'} ) { $cwsproduct = 1; }
498
499	if (( $cwsproduct ) || ( $devproduct ))  # use "DEV300m75"
500	{
501		my $source = uc($installer::globals::build); # DEV300
502		my $localminor = "";
503		if ( $installer::globals::minor ne "" ) { $localminor = $installer::globals::minor; }
504		else { $localminor = $installer::globals::lastminor; }
505		$version = $source . $localminor;
506	}
507	else  # use 3.2.0rc1
508	{
509		$version = $allvariables->{'PRODUCTVERSION'};
510		if (( $allvariables->{'ABOUTBOXPRODUCTVERSION'} ) && ( $allvariables->{'ABOUTBOXPRODUCTVERSION'} ne "" )) { $version = $allvariables->{'ABOUTBOXPRODUCTVERSION'}; }
511		if (( $allvariables->{'SHORT_PRODUCTEXTENSION'} ) && ( $allvariables->{'SHORT_PRODUCTEXTENSION'} ne "" )) { $version = $version . $allvariables->{'SHORT_PRODUCTEXTENSION'}; }
512	}
513
514	return $version;
515}
516
517###############################################################
518# Set date string, format: yymmdd
519###############################################################
520
521sub set_date_string
522{
523	my ($allvariables) = @_;
524
525	my $datestring = "";
526
527	my $devproduct = 0;
528	if (( $allvariables->{'DEVELOPMENTPRODUCT'} ) && ( $allvariables->{'DEVELOPMENTPRODUCT'} == 1 )) { $devproduct = 1; }
529
530	my $cwsproduct = 0;
531	# the environment variable CWS_WORK_STAMP is set only in CWS
532	if ( $ENV{'CWS_WORK_STAMP'} ) { $cwsproduct = 1; }
533
534	my $releasebuild = 1;
535	if (( $allvariables->{'SHORT_PRODUCTEXTENSION'} ) && ( $allvariables->{'SHORT_PRODUCTEXTENSION'} ne "" )) { $releasebuild = 0; }
536
537	if (( ! $devproduct ) && ( ! $cwsproduct ) && ( ! $releasebuild ))
538	{
539		my @timearray = localtime(time);
540
541		my $day = $timearray[3];
542		my $month = $timearray[4] + 1;
543		my $year = $timearray[5] + 1900;
544
545		if ( $month < 10 ) { $month = "0" . $month; }
546		if ( $day < 10 ) { $day = "0" . $day; }
547
548		$datestring = $year . $month . $day;
549	}
550
551	return $datestring;
552}
553
554#################################################################
555# Setting the platform name for download
556#################################################################
557
558sub get_download_platformname
559{
560	my $platformname = "";
561
562	if ( $installer::globals::islinuxbuild )
563	{
564		$platformname = "Linux";
565	}
566	elsif ( $installer::globals::issolarisbuild )
567	{
568		$platformname = "Solaris";
569	}
570	elsif ( $installer::globals::iswindowsbuild )
571	{
572		$platformname = "Win";
573	}
574	elsif ( $installer::globals::isfreebsdbuild )
575	{
576		$platformname = "FreeBSD";
577	}
578	elsif ( $installer::globals::ismacbuild )
579	{
580		$platformname = "MacOS";
581	}
582	else
583	{
584		# $platformname = $installer::globals::packageformat;
585		$platformname = $installer::globals::compiler;
586	}
587
588	return $platformname;
589}
590
591#########################################################
592# Setting the architecture for the download name
593#########################################################
594
595sub get_download_architecture
596{
597	my $arch = "";
598
599	if(( $installer::globals::compiler =~ /^unxlngi/ )
600	|| ( $installer::globals::compiler =~ /^unxmac.i/ )
601	|| ( $installer::globals::issolarisx86build )
602	|| ( $installer::globals::iswindowsbuild ))
603	{
604		$arch = "x86";
605	}
606	elsif(( $installer::globals::compiler =~ /^unxlngx/ )
607	||    ( $installer::globals::compiler =~ /^unxmaccx/ ))
608	{
609		$arch = "x86-64";
610	}
611	elsif ( $installer::globals::issolarissparcbuild )
612	{
613		$arch = "Sparc";
614	}
615	elsif(( $installer::globals::compiler =~ /^unxmacxp/ )
616	||    ( $installer::globals::compiler =~ /^unxlngppc/ ))
617	{
618		$arch = "PPC";
619	}
620
621	return $arch;
622}
623
624#########################################################
625# Setting the installation type for the download name
626#########################################################
627
628sub get_install_type
629{
630	my ($allvariables) = @_;
631
632	my $type = "";
633
634	if ( $installer::globals::languagepack )
635	{
636		$type = "langpack";
637
638		if ( $installer::globals::islinuxrpmbuild )
639		{
640			$type = $type . "-rpm";
641		}
642
643		if ( $installer::globals::islinuxdebbuild )
644		{
645			$type = $type . "-deb";
646		}
647
648		if ( $installer::globals::packageformat eq "archive" )
649		{
650			$type = $type . "-arc";
651		}
652	}
653	else
654	{
655		$type = "install";
656
657		if ( $installer::globals::islinuxrpmbuild )
658		{
659			$type = $type . "-rpm";
660		}
661
662		if ( $installer::globals::islinuxdebbuild )
663		{
664			$type = $type . "-deb";
665		}
666
667		if ( $installer::globals::packageformat eq "archive" )
668		{
669			$type = $type . "-arc";
670		}
671
672	}
673
674	return $type;
675}
676
677#########################################################
678# Setting installation addons
679#########################################################
680
681sub get_downloadname_addon
682{
683	my $addon = "";
684
685	if ( $installer::globals::islinuxdebbuild ) { $addon = $addon . "_deb"; }
686
687	return $addon;
688}
689
690#########################################################
691# Looking for versionstring in version.info
692# This has to be the only content of this file.
693#########################################################
694
695sub get_versionstring
696{
697	my ( $versionfile ) = @_;
698
699	my $versionstring = "";
700
701	for ( my $i = 0; $i <= $#{$versionfile}; $i++ )
702	{
703		my $oneline = ${$versionfile}[$i];
704
705		if ( $oneline =~ /^\s*\#/ ) { next; } # comment line
706		if ( $oneline =~ /^\s*\"\s*(.*?)\s*\"\s*$/ )
707		{
708			$versionstring = $1;
709			last;
710		}
711	}
712
713	return $versionstring;
714}
715
716#########################################################
717# Returning the current product version
718# This has to be defined in file "version.info"
719# in directory $installer::globals::ooouploaddir
720#########################################################
721
722sub get_current_version
723{
724	my $versionstring = "";
725	my $filename = "version.info";
726	# $filename = $installer::globals::ooouploaddir . $installer::globals::separator . $filename;
727
728	if ( -f $filename )
729	{
730        $installer::logger::Lang->printf("File %s exists. Trying to find current version.\n", $filename);
731		my $versionfile = installer::files::read_file($filename);
732		$versionstring = get_versionstring($versionfile);
733        $installer::logger::Lang->printf("Setting version string: %s\n", $versionstring);
734	}
735	else
736	{
737        $installer::logger::Lang->printf("File %s does not exist. No version setting in download file name.\n", $filename);
738	}
739
740	$installer::globals::oooversionstring = $versionstring;
741
742	return $versionstring;
743}
744
745###############################################################################################
746# Setting the download file name
747# Syntax:
748# (PRODUCTNAME)_(VERSION)_(TIMESTAMP)_(OS)_(ARCH)_(INSTALLTYPE)_(LANGUAGE).(FILEEXTENSION)
749# Rules:
750# Timestamp only for Beta and Release Candidate
751###############################################################################################
752
753sub set_download_filename
754{
755	my ($languagestringref, $allvariables) = @_;
756
757	my $start = get_downloadname_productname($allvariables);
758	my $versionstring = get_download_version($allvariables);
759	my $date = set_date_string($allvariables);
760	my $platform = get_download_platformname();
761	my $architecture = get_download_architecture();
762	my $type = get_install_type($allvariables);
763	my $language = get_downloadname_language($languagestringref);
764
765	# Setting the extension happens automatically
766
767	my $filename = $start . "_" . $versionstring . "_" . $date . "_" . $platform . "_" . $architecture . "_" . $type . "_" . $language;
768
769	$filename =~ s/\_\_/\_/g;	# necessary, if $versionstring or $platform or $language are empty
770	$filename =~ s/\_\s*$//;	# necessary, if $language and $addon are empty
771
772	$installer::globals::ooodownloadfilename = $filename;
773
774	return $filename;
775}
776
777#########################################################
778# Creating a tar.gz file
779#########################################################
780
781sub create_tar_gz_file_from_directory
782{
783	my ($installdir, $getuidlibrary, $downloaddir, $downloadfilename) = @_;
784
785	my $packdir = $installdir;
786	installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$packdir);
787	my $changedir = $installdir;
788	installer::pathanalyzer::get_path_from_fullqualifiedname(\$changedir);
789
790	my $ldpreloadstring = "";
791	if ( $getuidlibrary ne "" ) { $ldpreloadstring = "LD_PRELOAD=" . $getuidlibrary; }
792
793	$installer::globals::downloadfileextension = ".tar.gz";
794	$installer::globals::downloadfilename = $downloadfilename . $installer::globals::downloadfileextension;
795	my $targzname = $downloaddir . $installer::globals::separator . $installer::globals::downloadfilename;
796
797	my $systemcall = "cd $changedir; $ldpreloadstring tar -cf - $packdir | gzip > $targzname";
798
799	my $returnvalue = system($systemcall);
800
801    $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
802
803	if ($returnvalue)
804	{
805        $installer::logger::Lang->printf("ERROR: Could not execute \"%s\"!\n", $systemcall);
806	}
807	else
808	{
809        $installer::logger::Lang->printf("Success: Executed \"%s\" successfully!\n", $systemcall);
810	}
811
812	return $targzname;
813}
814
815#########################################################
816# Setting the variables in the download name
817#########################################################
818
819sub resolve_variables_in_downloadname
820{
821	my ($allvariables, $downloadname, $languagestringref) = @_;
822
823	# Typical name: soa-{productversion}-{extension}-bin-{os}-{languages}
824
825	my $productversion = $allvariables->{'PRODUCTVERSION'};
826	$productversion = "" unless defined $productversion;
827	$downloadname =~ s/\{productversion\}/$productversion/;
828
829	my $packageversion = $allvariables->{'PACKAGEVERSION'};
830	$packageversion = "" unless defined $packageversion;
831	$downloadname =~ s/\{packageversion\}/$packageversion/;
832
833	my $extension = $allvariables->{'SHORT_PRODUCTEXTENSION'};
834	$extension = "" unless defined $extension;
835	$extension = lc($extension);
836	$downloadname =~ s/\{extension\}/$extension/;
837
838	my $os = "";
839	if ( $installer::globals::iswindowsbuild ) { $os = "windows"; }
840	elsif ( $installer::globals::issolarissparcbuild ) { $os = "solsparc"; }
841	elsif ( $installer::globals::issolarisx86build ) { $os = "solia"; }
842	elsif ( $installer::globals::islinuxbuild ) { $os = "linux"; }
843	elsif ( $installer::globals::compiler =~ /unxmac.i/ ) { $os = "macosi"; }
844	elsif ( $installer::globals::compiler =~ /unxmac.x/ ) { $os = "macosx"; }
845	elsif ( $installer::globals::compiler =~ /unxmacxp/ ) { $os = "macosp"; }
846	else { $os = ""; }
847	$downloadname =~ s/\{os\}/$os/;
848
849	my $languages = $$languagestringref;
850	$downloadname =~ s/\{languages\}/$languages/;
851
852	$downloadname =~ s/\-\-\-/\-/g;
853	$downloadname =~ s/\-\-/\-/g;
854	$downloadname =~ s/\-\s*$//;
855
856	return $downloadname;
857}
858
859##################################################################
860# Windows: Replacing one placeholder with the specified value
861##################################################################
862
863sub replace_one_variable
864{
865	my ($templatefile, $placeholder, $value) = @_;
866
867    $installer::logger::Lang->printf("Replacing %s by %s in nsi file\n", $placeholder, $value);
868
869	for ( my $i = 0; $i <= $#{$templatefile}; $i++ )
870	{
871		${$templatefile}[$i] =~ s/$placeholder/$value/g;
872	}
873
874}
875
876########################################################################################
877# Converting a string to a unicode string
878########################################################################################
879
880sub convert_to_unicode
881{
882	my ($string) = @_;
883
884	my $unicodestring = "";
885
886	my $stringlength = length($string);
887
888	for ( my $i = 0; $i < $stringlength; $i++ )
889	{
890		$unicodestring = $unicodestring . substr($string, $i, 1);
891		$unicodestring = $unicodestring . chr(0);
892	}
893
894	return $unicodestring;
895}
896
897##################################################################
898# Windows: Including the product name into nsi template
899##################################################################
900
901sub put_windows_productname_into_template
902{
903	my ($templatefile, $variableshashref) = @_;
904
905	my $productname = $variableshashref->{'PRODUCTNAME'};
906	$productname =~ s/\.//g;	# OpenOffice.org -> OpenOfficeorg
907
908	replace_one_variable($templatefile, "PRODUCTNAMEPLACEHOLDER", $productname);
909}
910
911##################################################################
912# Windows: Including the path to the banner.bmp into nsi template
913##################################################################
914
915sub put_banner_bmp_into_template
916{
917	my ($templatefile, $includepatharrayref, $allvariables) = @_;
918
919	# my $filename = "downloadbanner.bmp";
920	if ( ! $allvariables->{'DOWNLOADBANNER'} ) { installer::exiter::exit_program("ERROR: DOWNLOADBANNER not defined in product definition!", "put_banner_bmp_into_template"); }
921	my $filename = $allvariables->{'DOWNLOADBANNER'};
922
923	my $completefilenameref = "";
924
925	if ( $installer::globals::include_pathes_read )
926	{
927		$completefilenameref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$filename, $includepatharrayref, 0);
928	}
929	else
930	{
931		$completefilenameref = installer::scriptitems::get_sourcepath_from_filename_and_includepath_classic(\$filename, $includepatharrayref, 0);
932	}
933
934	if ($$completefilenameref eq "") { installer::exiter::exit_program("ERROR: Could not find download file $filename!", "put_banner_bmp_into_template"); }
935
936	if ( $^O =~ /cygwin/i ) { $$completefilenameref =~ s/\//\\/g; }
937
938	replace_one_variable($templatefile, "BANNERBMPPLACEHOLDER", $$completefilenameref);
939}
940
941##################################################################
942# Windows: Including the path to the welcome.bmp into nsi template
943##################################################################
944
945sub put_welcome_bmp_into_template
946{
947	my ($templatefile, $includepatharrayref, $allvariables) = @_;
948
949	# my $filename = "downloadbitmap.bmp";
950	if ( ! $allvariables->{'DOWNLOADBITMAP'} ) { installer::exiter::exit_program("ERROR: DOWNLOADBITMAP not defined in product definition!", "put_welcome_bmp_into_template"); }
951	my $filename = $allvariables->{'DOWNLOADBITMAP'};
952
953	my $completefilenameref = "";
954
955	if ( $installer::globals::include_pathes_read )
956	{
957		$completefilenameref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$filename, $includepatharrayref, 0);
958	}
959	else
960	{
961		$completefilenameref = installer::scriptitems::get_sourcepath_from_filename_and_includepath_classic(\$filename, $includepatharrayref, 0);
962	}
963
964	if ($$completefilenameref eq "") { installer::exiter::exit_program("ERROR: Could not find download file $filename!", "put_welcome_bmp_into_template"); }
965
966	if ( $^O =~ /cygwin/i ) { $$completefilenameref =~ s/\//\\/g; }
967
968	replace_one_variable($templatefile, "WELCOMEBMPPLACEHOLDER", $$completefilenameref);
969}
970
971##################################################################
972# Windows: Including the path to the setup.ico into nsi template
973##################################################################
974
975sub put_setup_ico_into_template
976{
977	my ($templatefile, $includepatharrayref, $allvariables) = @_;
978
979	# my $filename = "downloadsetup.ico";
980	if ( ! $allvariables->{'DOWNLOADSETUPICO'} ) { installer::exiter::exit_program("ERROR: DOWNLOADSETUPICO not defined in product definition!", "put_setup_ico_into_template"); }
981	my $filename = $allvariables->{'DOWNLOADSETUPICO'};
982
983	my $completefilenameref = "";
984
985	if ( $installer::globals::include_pathes_read )
986	{
987		$completefilenameref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$filename, $includepatharrayref, 0);
988	}
989	else
990	{
991		$completefilenameref = installer::scriptitems::get_sourcepath_from_filename_and_includepath_classic(\$filename, $includepatharrayref, 0);
992	}
993
994	if ($$completefilenameref eq "") { installer::exiter::exit_program("ERROR: Could not find download file $filename!", "put_setup_ico_into_template"); }
995
996	if ( $^O =~ /cygwin/i ) { $$completefilenameref =~ s/\//\\/g; }
997
998	replace_one_variable($templatefile, "SETUPICOPLACEHOLDER", $$completefilenameref);
999}
1000
1001##################################################################
1002# Windows: Including the publisher into nsi template
1003##################################################################
1004
1005sub put_publisher_into_template ($$)
1006{
1007    my ($templatefile, $variables) = @_;
1008
1009    my $publisher = $variables->{'OOOVENDOR'};
1010    $publisher = "" unless defined $publisher;
1011
1012    replace_one_variable($templatefile, "PUBLISHERPLACEHOLDER", $publisher);
1013}
1014
1015##################################################################
1016# Windows: Including the web site into nsi template
1017##################################################################
1018
1019sub put_website_into_template ($$)
1020{
1021    my ($templatefile, $variables) = @_;
1022
1023    my $website = $variables->{'STARTCENTER_INFO_URL'};
1024    $website = "" unless defined $website;
1025
1026    replace_one_variable($templatefile, "WEBSITEPLACEHOLDER", $website);
1027}
1028
1029##################################################################
1030# Windows: Including the Java file name into nsi template
1031##################################################################
1032
1033sub put_javafilename_into_template
1034{
1035	my ($templatefile, $variableshashref) = @_;
1036
1037	my $javaversion = "";
1038
1039	if ( $variableshashref->{'WINDOWSJAVAFILENAME'} ) { $javaversion = $variableshashref->{'WINDOWSJAVAFILENAME'}; }
1040
1041	replace_one_variable($templatefile, "WINDOWSJAVAFILENAMEPLACEHOLDER", $javaversion);
1042}
1043
1044##################################################################
1045# Windows: Including the product version into nsi template
1046##################################################################
1047
1048sub put_windows_productversion_into_template
1049{
1050	my ($templatefile, $variableshashref) = @_;
1051
1052	my $productversion = $variableshashref->{'PRODUCTVERSION'};
1053
1054	replace_one_variable($templatefile, "PRODUCTVERSIONPLACEHOLDER", $productversion);
1055}
1056
1057##################################################################
1058# Windows: Including the product version into nsi template
1059##################################################################
1060
1061sub put_windows_productpath_into_template
1062{
1063	my ($templatefile, $variableshashref, $languagestringref, $localnsisdir) = @_;
1064
1065	my $productpath = $variableshashref->{'PROPERTYTABLEPRODUCTNAME'};
1066
1067	my $locallangs = $$languagestringref;
1068	$locallangs =~ s/_/ /g;
1069	if (length($locallangs) > $installer::globals::max_lang_length) { $locallangs = "multi lingual"; }
1070
1071	if ( ! $installer::globals::languagepack ) { $productpath = $productpath . " (" . $locallangs . ")"; }
1072
1073	replace_one_variable($templatefile, "PRODUCTPATHPLACEHOLDER", $productpath);
1074}
1075
1076##################################################################
1077# Windows: Including download file name into nsi template
1078##################################################################
1079
1080sub put_outputfilename_into_template
1081{
1082	my ($templatefile, $downloadname) = @_;
1083
1084	$installer::globals::downloadfileextension = ".exe";
1085	$downloadname = $downloadname . $installer::globals::downloadfileextension;
1086	$installer::globals::downloadfilename = $downloadname;
1087
1088	replace_one_variable($templatefile, "DOWNLOADNAMEPLACEHOLDER", $downloadname);
1089}
1090
1091##################################################################
1092# Windows: Generating the file list in nsi file format
1093##################################################################
1094
1095sub get_file_list
1096{
1097	my ( $basedir ) = @_;
1098
1099	my @filelist = ();
1100
1101	my $alldirs = installer::systemactions::get_all_directories($basedir);
1102	unshift(@{$alldirs}, $basedir);	# $basedir is the first directory in $alldirs
1103
1104	for ( my $i = 0; $i <= $#{$alldirs}; $i++ )
1105	{
1106		my $onedir = ${$alldirs}[$i];
1107
1108		# Syntax:
1109		# SetOutPath "$INSTDIR"
1110
1111		my $relativedir = $onedir;
1112		$relativedir =~ s/\Q$basedir\E//;
1113
1114		my $oneline = " " . "SetOutPath" . " " . "\"\$INSTDIR" . $relativedir . "\"" . "\n";
1115
1116		if ( $^O =~ /cygwin/i ) {
1117			$oneline =~ s/\//\\/g;
1118		}
1119		push(@filelist, $oneline);
1120
1121		# Collecting all files in the specific directory
1122
1123		my $files = installer::systemactions::get_all_files_from_one_directory($onedir);
1124
1125		for ( my $j = 0; $j <= $#{$files}; $j++ )
1126		{
1127			my $onefile = ${$files}[$j];
1128
1129			my $fileline = "  " . "File" . " " . "\"" . $onefile . "\"" . "\n";
1130
1131			if ( $^O =~ /cygwin/i ) {
1132				$fileline =~ s/\//\\/g;
1133			}
1134			push(@filelist, $fileline);
1135		}
1136	}
1137
1138	return \@filelist;
1139}
1140
1141##################################################################
1142# Windows: Including list of all files into nsi template
1143##################################################################
1144
1145sub put_filelist_into_template
1146{
1147	my ($templatefile, $installationdir) = @_;
1148
1149	my $filelist = get_file_list($installationdir);
1150
1151	my $filestring = "";
1152
1153	for ( my $i = 0; $i <= $#{$filelist}; $i++ )
1154	{
1155		$filestring = $filestring . ${$filelist}[$i];
1156	}
1157
1158	$filestring =~ s/\s*$//;
1159
1160	replace_one_variable($templatefile, "ALLFILESPLACEHOLDER", $filestring);
1161}
1162
1163##################################################################
1164# Windows: NSIS uses specific language names
1165##################################################################
1166
1167sub nsis_language_converter
1168{
1169	my ($language) = @_;
1170
1171	my $nsislanguage = "";
1172
1173	# Assign language used by NSIS.
1174	# The files "$nsislanguage.nsh" and "$nsislanguage.nlf"
1175	# are needed in the NSIS environment.
1176	# Directory: <NSIS-Dir>/Contrib/Language files
1177	if ( $language eq "en-US" ) { $nsislanguage = "English"; }
1178	elsif ( $language eq "sq" ) { $nsislanguage = "Albanian"; }
1179	elsif ( $language eq "ar" ) { $nsislanguage = "Arabic"; }
1180	elsif ( $language eq "bg" ) { $nsislanguage = "Bulgarian"; }
1181	elsif ( $language eq "ca" ) { $nsislanguage = "Catalan"; }
1182	elsif ( $language eq "hr" ) { $nsislanguage = "Croatian"; }
1183	elsif ( $language eq "cs" ) { $nsislanguage = "Czech"; }
1184	elsif ( $language eq "da" ) { $nsislanguage = "Danish"; }
1185	elsif ( $language eq "nl" ) { $nsislanguage = "Dutch"; }
1186	elsif ( $language eq "de" ) { $nsislanguage = "German"; }
1187	elsif ( $language eq "de-LU" ) { $nsislanguage = "Luxembourgish"; }
1188	elsif ( $language eq "et" ) { $nsislanguage = "Estonian"; }
1189	elsif ( $language eq "fa" ) { $nsislanguage = "Farsi"; }
1190	elsif ( $language eq "el" ) { $nsislanguage = "Greek"; }
1191	elsif ( $language eq "fi" ) { $nsislanguage = "Finnish"; }
1192	elsif ( $language eq "fr" ) { $nsislanguage = "French"; }
1193	elsif ( $language eq "hu" ) { $nsislanguage = "Hungarian"; }
1194	elsif ( $language eq "he" ) { $nsislanguage = "Hebrew"; }
1195	elsif ( $language eq "is" ) { $nsislanguage = "Icelandic"; }
1196	elsif ( $language eq "id" ) { $nsislanguage = "Indonesian"; }
1197	elsif ( $language eq "it" ) { $nsislanguage = "Italian"; }
1198	elsif ( $language eq "lv" ) { $nsislanguage = "Latvian"; }
1199	elsif ( $language eq "lt" ) { $nsislanguage = "Lithuanian"; }
1200	elsif ( $language eq "mk" ) { $nsislanguage = "Macedonian"; }
1201	elsif ( $language eq "mn" ) { $nsislanguage = "Mongolian"; }
1202	elsif ( $language eq "no" ) { $nsislanguage = "Norwegian"; }
1203	elsif ( $language eq "no-NO" ) { $nsislanguage = "Norwegian"; }
1204	elsif ( $language eq "es" ) { $nsislanguage = "Spanish"; }
1205	elsif ( $language eq "sl" ) { $nsislanguage = "Slovenian"; }
1206	elsif ( $language eq "sv" ) { $nsislanguage = "Swedish"; }
1207	elsif ( $language eq "sk" ) { $nsislanguage = "Slovak"; }
1208	elsif ( $language eq "pl" ) { $nsislanguage = "Polish"; }
1209	elsif ( $language eq "pt-BR" ) { $nsislanguage = "PortugueseBR"; }
1210	elsif ( $language eq "pt" ) { $nsislanguage = "Portuguese"; }
1211	elsif ( $language eq "ro" ) { $nsislanguage = "Romanian"; }
1212	elsif ( $language eq "ru" ) { $nsislanguage = "Russian"; }
1213	elsif ( $language eq "sh" ) { $nsislanguage = "SerbianLatin"; }
1214	elsif ( $language eq "sr" ) { $nsislanguage = "Serbian"; }
1215	elsif ( $language eq "sr-SP" ) { $nsislanguage = "Serbian"; }
1216	elsif ( $language eq "uk" ) { $nsislanguage = "Ukrainian"; }
1217	elsif ( $language eq "tr" ) { $nsislanguage = "Turkish"; }
1218	elsif ( $language eq "ja" ) { $nsislanguage = "Japanese"; }
1219	elsif ( $language eq "ko" ) { $nsislanguage = "Korean"; }
1220	elsif ( $language eq "th" ) { $nsislanguage = "Thai"; }
1221	elsif ( $language eq "vi" ) { $nsislanguage = "Vietnamese"; }
1222	elsif ( $language eq "zh-CN" ) { $nsislanguage = "SimpChinese"; }
1223	elsif ( $language eq "zh-TW" ) { $nsislanguage = "TradChinese"; }
1224	else
1225    {
1226        $installer::logger::Lang->printf("NSIS language_converter : Could not find nsis language for %s!\n", $language);
1227		$nsislanguage = "English";
1228	}
1229
1230	return $nsislanguage;
1231}
1232
1233##################################################################
1234# Windows: Including list of all languages into nsi template
1235##################################################################
1236
1237sub put_language_list_into_template
1238{
1239	my ($templatefile, $languagesarrayref) = @_;
1240
1241	my $alllangstring = "";
1242	my %nsislangs;
1243
1244	for ( my $i = 0; $i <= $#{$languagesarrayref}; $i++ )
1245	{
1246		my $onelanguage = ${$languagesarrayref}[$i];
1247		my $nsislanguage = nsis_language_converter($onelanguage);
1248		$nsislangs{$nsislanguage}++;
1249	}
1250
1251	foreach my $nsislanguage ( keys(%nsislangs) )
1252	{
1253		# Syntax: !insertmacro MUI_LANGUAGE "English"
1254		my $langstring = "\!insertmacro MUI_LANGUAGE_PACK " . $nsislanguage . "\n";
1255		if ( $nsislanguage eq "English" )
1256		{
1257			$alllangstring = $langstring . $alllangstring;
1258		}
1259		else
1260		{
1261			$alllangstring = $alllangstring . $langstring;
1262		}
1263	}
1264
1265	$alllangstring =~ s/\s*$//;
1266
1267	replace_one_variable($templatefile, "ALLLANGUAGESPLACEHOLDER", $alllangstring);
1268}
1269
1270##################################################################
1271# Windows: Collecting all identifier from mlf file
1272##################################################################
1273
1274sub get_identifier
1275{
1276	my ( $mlffile ) = @_;
1277
1278	my @identifier = ();
1279
1280	for ( my $i = 0; $i <= $#{$mlffile}; $i++ )
1281	{
1282		my $oneline = ${$mlffile}[$i];
1283
1284		if ( $oneline =~ /^\s*\[(.+)\]\s*$/ )
1285		{
1286			my $identifier = $1;
1287			push(@identifier, $identifier);
1288		}
1289	}
1290
1291	return \@identifier;
1292}
1293
1294##############################################################
1295# Returning the complete block in all languages
1296# for a specified string
1297##############################################################
1298
1299sub get_language_block_from_language_file
1300{
1301	my ($searchstring, $languagefile) = @_;
1302
1303	my @language_block = ();
1304
1305	for ( my $i = 0; $i <= $#{$languagefile}; $i++ )
1306	{
1307		if ( ${$languagefile}[$i] =~ /^\s*\[\s*$searchstring\s*\]\s*$/ )
1308		{
1309			my $counter = $i;
1310
1311			push(@language_block, ${$languagefile}[$counter]);
1312			$counter++;
1313
1314			while (( $counter <= $#{$languagefile} ) && (!( ${$languagefile}[$counter] =~ /^\s*\[/ )))
1315			{
1316				push(@language_block, ${$languagefile}[$counter]);
1317				$counter++;
1318			}
1319
1320			last;
1321		}
1322	}
1323
1324	return \@language_block;
1325}
1326
1327##############################################################
1328# Returning a specific language string from the block
1329# of all translations
1330##############################################################
1331
1332sub get_language_string_from_language_block
1333{
1334	my ($language_block, $language) = @_;
1335
1336	my $newstring = "";
1337
1338	for ( my $i = 0; $i <= $#{$language_block}; $i++ )
1339	{
1340		if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
1341		{
1342			$newstring = $1;
1343			last;
1344		}
1345	}
1346
1347	if ( $newstring eq "" )
1348	{
1349		$language = "en-US"; 	# defaulting to english
1350
1351		for ( my $i = 0; $i <= $#{$language_block}; $i++ )
1352		{
1353			if ( ${$language_block}[$i] =~ /^\s*$language\s*\=\s*\"(.*)\"\s*$/ )
1354			{
1355				$newstring = $1;
1356				last;
1357			}
1358		}
1359	}
1360
1361	return $newstring;
1362}
1363
1364##################################################################
1365# Windows: Replacing strings in NSIS nsh file
1366# nsh file syntax:
1367# !define MUI_TEXT_DIRECTORY_TITLE "Zielverzeichnis ausw�hlen"
1368##################################################################
1369
1370sub replace_identifier_in_nshfile
1371{
1372	my ( $nshfile, $identifier, $newstring, $nshfilename, $onelanguage ) = @_;
1373
1374	for ( my $i = 0; $i <= $#{$nshfile}; $i++ )
1375	{
1376		if ( ${$nshfile}[$i] =~ /\s+\Q$identifier\E\s+\"(.+)\"\s*$/ )
1377		{
1378			my $oldstring = $1;
1379			${$nshfile}[$i] =~ s/\Q$oldstring\E/$newstring/;
1380            $installer::logger::Lang->printf("NSIS replacement in %s (%s):  \-\> %s\n",
1381                $nshfilename,
1382                $onelanguage,
1383                $oldstring,
1384                $newstring);
1385		}
1386	}
1387}
1388
1389##################################################################
1390# Windows: Replacing strings in NSIS nlf file
1391# nlf file syntax (2 lines):
1392# # ^DirSubText
1393# Zielverzeichnis
1394##################################################################
1395
1396sub replace_identifier_in_nlffile
1397{
1398	my ( $nlffile, $identifier, $newstring, $nlffilename, $onelanguage ) = @_;
1399
1400	for ( my $i = 0; $i <= $#{$nlffile}; $i++ )
1401	{
1402		if ( ${$nlffile}[$i] =~ /^\s*\#\s+\^\s*\Q$identifier\E\s*$/ )
1403		{
1404			my $next = $i+1;
1405			my $oldstring = ${$nlffile}[$next];
1406			${$nlffile}[$next] = $newstring . "\n";
1407			$oldstring =~ s/\s*$//;
1408            $installer::logger::Lang->printf("NSIS replacement in %s (%s): %s \-\> %s\n",
1409                $nlffilename,
1410                $onelanguage,
1411                $oldstring,
1412                $newstring);
1413		}
1414	}
1415}
1416
1417##################################################################
1418# Windows: Translating the NSIS nsh and nlf file
1419##################################################################
1420
1421sub translate_nsh_nlf_file
1422{
1423	my ($nshfile, $nlffile, $mlffile, $onelanguage, $nshfilename, $nlffilename, $nsislanguage) = @_;
1424
1425	# Analyzing the mlf file, collecting all Identifier
1426	my $allidentifier = get_identifier($mlffile);
1427
1428	$onelanguage = "en-US" if ( $nsislanguage eq "English" && $onelanguage ne "en-US");
1429	for ( my $i = 0; $i <= $#{$allidentifier}; $i++ )
1430	{
1431		my $identifier = ${$allidentifier}[$i];
1432		my $language_block = get_language_block_from_language_file($identifier, $mlffile);
1433		my $newstring = get_language_string_from_language_block($language_block, $onelanguage);
1434
1435		# removing mask
1436		$newstring =~ s/\\\'/\'/g;
1437
1438		replace_identifier_in_nshfile($nshfile, $identifier, $newstring, $nshfilename, $onelanguage);
1439		replace_identifier_in_nlffile($nlffile, $identifier, $newstring, $nlffilename, $onelanguage);
1440	}
1441}
1442
1443##################################################################
1444# Converting utf 16 file to utf 8
1445##################################################################
1446
1447sub convert_utf16_to_utf8
1448{
1449	my ( $filename ) = @_;
1450
1451	my @localfile = ();
1452
1453	my $savfilename = $filename . "_before.utf16";
1454	installer::systemactions::copy_one_file($filename, $savfilename);
1455
1456#	open( IN, "<:utf16", $filename ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "convert_utf16_to_utf8");
1457#	open( IN, "<:para:crlf:uni", $filename ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "convert_utf16_to_utf8");
1458	open( IN, "<:encoding(UTF16-LE)", $filename ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "convert_utf16_to_utf8");
1459	while ( my $line = <IN> )
1460    {
1461		push @localfile, $line;
1462	}
1463	close( IN );
1464
1465	if ( open( OUT, ">:utf8", $filename ) )
1466	{
1467		print OUT @localfile;
1468		close(OUT);
1469	}
1470
1471	$savfilename = $filename . "_before.utf8";
1472	installer::systemactions::copy_one_file($filename, $savfilename);
1473}
1474
1475##################################################################
1476# Converting utf 8 file to utf 16
1477##################################################################
1478
1479sub convert_utf8_to_utf16
1480{
1481	my ( $filename ) = @_;
1482
1483	my @localfile = ();
1484
1485	my $savfilename = $filename . "_after.utf8";
1486	installer::systemactions::copy_one_file($filename, $savfilename);
1487
1488	open( IN, "<:utf8", $filename ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "convert_utf8_to_utf16");
1489	while (my  $line = <IN>)
1490    {
1491		push @localfile, $line;
1492	}
1493	close( IN );
1494
1495	if ( open( OUT, ">:raw:encoding(UTF16-LE):crlf:utf8", $filename ) )
1496	{
1497		print OUT @localfile;
1498		close(OUT);
1499	}
1500
1501	$savfilename = $filename . "_after.utf16";
1502	installer::systemactions::copy_one_file($filename, $savfilename);
1503}
1504
1505##################################################################
1506# Converting text string to utf 16
1507##################################################################
1508
1509sub convert_textstring_to_utf16
1510{
1511	my ( $textstring, $localnsisdir, $shortfilename ) = @_;
1512
1513	my $filename = $localnsisdir . $installer::globals::separator . $shortfilename;
1514	my @filecontent = ();
1515	push(@filecontent, $textstring);
1516	installer::files::save_file($filename, \@filecontent);
1517	convert_utf8_to_utf16($filename);
1518	my $newfile = installer::files::read_file($filename);
1519	my $utf16string = "";
1520	if ( ${$newfile}[0] ne "" ) { $utf16string = ${$newfile}[0]; }
1521
1522	return $utf16string;
1523}
1524
1525##################################################################
1526# Windows: Copying NSIS language files to local nsis directory
1527##################################################################
1528
1529sub copy_and_translate_nsis_language_files
1530{
1531	my ($nsispath, $localnsisdir, $languagesarrayref, $allvariables) = @_;
1532
1533	my $nlffilepath = $nsispath . $installer::globals::separator . "Contrib" . $installer::globals::separator . "Language\ files" . $installer::globals::separator;
1534	my $nshfilepath = $nsispath . $installer::globals::separator . "Contrib" . $installer::globals::separator . "Modern\ UI" . $installer::globals::separator . "Language files" . $installer::globals::separator;
1535
1536	for ( my $i = 0; $i <= $#{$languagesarrayref}; $i++ )
1537	{
1538		my $onelanguage = ${$languagesarrayref}[$i];
1539		my $nsislanguage = nsis_language_converter($onelanguage);
1540
1541		# Copying the nlf file
1542		my $sourcepath = $nlffilepath . $nsislanguage . "\.nlf";
1543		if ( ! -f $sourcepath ) { installer::exiter::exit_program("ERROR: Could not find nsis file: $sourcepath!", "copy_and_translate_nsis_language_files"); }
1544		my $nlffilename = $localnsisdir . $installer::globals::separator . $nsislanguage . "_pack.nlf";
1545		if ( $^O =~ /cygwin/i ) { $nlffilename =~ s/\//\\/g; }
1546		installer::systemactions::copy_one_file($sourcepath, $nlffilename);
1547
1548		# Copying the nsh file
1549		# In newer nsis versions, the nsh file is located next to the nlf file
1550		$sourcepath = $nshfilepath . $nsislanguage . "\.nsh";
1551		if ( ! -f $sourcepath )
1552		{
1553			# trying to find the nsh file next to the nlf file
1554			$sourcepath = $nlffilepath . $nsislanguage . "\.nsh";
1555			if ( ! -f $sourcepath )
1556			{
1557				installer::exiter::exit_program("ERROR: Could not find nsis file: $sourcepath!", "copy_and_translate_nsis_language_files");
1558			}
1559		}
1560		my $nshfilename = $localnsisdir . $installer::globals::separator . $nsislanguage . "_pack.nsh";
1561		if ( $^O =~ /cygwin/i ) { $nshfilename =~ s/\//\\/g; }
1562		installer::systemactions::copy_one_file($sourcepath, $nshfilename);
1563
1564		# Changing the macro name in nsh file: MUI_LANGUAGEFILE_BEGIN -> MUI_LANGUAGEFILE_PACK_BEGIN
1565        convert_utf16_to_utf8($nshfilename);
1566        convert_utf16_to_utf8($nlffilename);
1567        my $nshfile = installer::files::read_file($nshfilename);
1568		replace_one_variable($nshfile, "MUI_LANGUAGEFILE_BEGIN", "MUI_LANGUAGEFILE_PACK_BEGIN");
1569
1570		# find the ulf file for translation
1571		my $mlffile = get_translation_file($allvariables);
1572
1573		# Translate the files
1574		my $nlffile = installer::files::read_file($nlffilename);
1575		translate_nsh_nlf_file($nshfile, $nlffile, $mlffile, $onelanguage, $nshfilename, $nlffilename, $nsislanguage);
1576
1577		installer::files::save_file($nshfilename, $nshfile);
1578		installer::files::save_file($nlffilename, $nlffile);
1579
1580        convert_utf8_to_utf16($nshfilename);
1581        convert_utf8_to_utf16($nlffilename);
1582	}
1583
1584}
1585
1586##################################################################
1587# Windows: Including the nsis path into the nsi template
1588##################################################################
1589
1590sub put_nsis_path_into_template
1591{
1592	my ($templatefile, $nsisdir) = @_;
1593
1594	replace_one_variable($templatefile, "NSISPATHPLACEHOLDER", $nsisdir);
1595}
1596
1597##################################################################
1598# Windows: Including the output path into the nsi template
1599##################################################################
1600
1601sub put_output_path_into_template
1602{
1603	my ($templatefile, $downloaddir) = @_;
1604
1605	if ( $^O =~ /cygwin/i ) { $downloaddir =~ s/\//\\/g; }
1606
1607	replace_one_variable($templatefile, "OUTPUTDIRPLACEHOLDER", $downloaddir);
1608}
1609
1610##################################################################
1611# Windows: Finding the path to the nsis SDK
1612##################################################################
1613
1614sub get_path_to_nsis_sdk
1615{
1616	my $nsispath = "";
1617
1618	if ( $ENV{'NSIS_PATH'} )
1619    {
1620		$nsispath = $ENV{'NSIS_PATH'};
1621	}
1622	if ( $nsispath eq "" )
1623	{
1624        $installer::logger::Info->print("... no Environment variable \"NSIS_PATH\"!\n");
1625	}
1626    elsif ( ! -d $nsispath )
1627	{
1628		installer::exiter::exit_program("ERROR: NSIS path $nsispath does not exist!", "get_path_to_nsis_sdk");
1629	}
1630
1631	return $nsispath;
1632}
1633
1634##################################################################
1635# Windows: Executing NSIS to create the installation set
1636##################################################################
1637
1638sub call_nsis
1639{
1640	my ( $nsispath, $nsifile ) = @_;
1641
1642	my $makensisexe = $nsispath . $installer::globals::separator . "makensis.exe";
1643
1644    $installer::logger::Info->printf("... starting %s ... \n", $makensisexe);
1645
1646	if( $^O =~ /cygwin/i ) { $nsifile =~ s/\\/\//g;	}
1647
1648	my $systemcall = "$makensisexe $nsifile |";
1649
1650    $installer::logger::Lang->printf("Systemcall: %s\n", $systemcall);
1651
1652	my @nsisoutput = ();
1653
1654	open (NSI, "$systemcall");
1655	while (<NSI>) {push(@nsisoutput, $_); }
1656	close (NSI);
1657
1658	my $returnvalue = $?;	# $? contains the return value of the systemcall
1659
1660	if ($returnvalue)
1661	{
1662        $installer::logger::Lang->printf("ERROR: %s !\n", $systemcall);
1663	}
1664	else
1665	{
1666        $installer::logger::Lang->printf("Success: %s\n", $systemcall);
1667	}
1668
1669	foreach my $line (@nsisoutput)
1670    {
1671        $installer::logger::Lang->print($line);
1672    }
1673}
1674
1675#################################################################################
1676# Replacing one variable in one files
1677#################################################################################
1678
1679sub replace_one_variable_in_translationfile
1680{
1681	my ($translationfile, $variable, $searchstring) = @_;
1682
1683	for ( my $i = 0; $i <= $#{$translationfile}; $i++ )
1684	{
1685		${$translationfile}[$i] =~ s/\%$searchstring/$variable/g;
1686	}
1687}
1688
1689#################################################################################
1690# Replacing the variables in the translation file
1691#################################################################################
1692
1693sub replace_variables
1694{
1695	my ($translationfile, $variableshashref) = @_;
1696
1697	foreach my $key (keys %{$variableshashref})
1698	{
1699		my $value = $variableshashref->{$key};
1700
1701		# special handling for PRODUCTVERSION, if $allvariables->{'POSTVERSIONEXTENSION'}
1702		if (( $key eq "PRODUCTVERSION" ) && ( $variableshashref->{'POSTVERSIONEXTENSION'} )) { $value = $value . " " . $variableshashref->{'POSTVERSIONEXTENSION'}; }
1703
1704		replace_one_variable_in_translationfile($translationfile, $value, $key);
1705	}
1706}
1707
1708#########################################################
1709# Getting the translation file for the nsis installer
1710#########################################################
1711
1712sub get_translation_file
1713{
1714	my ($allvariableshashref) = @_;
1715	my $translationfilename = $installer::globals::idtlanguagepath . $installer::globals::separator . $installer::globals::nsisfilename . ".uulf";
1716	if ( ! -f $translationfilename ) { installer::exiter::exit_program("ERROR: Could not find language file $translationfilename!", "get_translation_file"); }
1717	my $translationfile = installer::files::read_file($translationfilename);
1718	replace_variables($translationfile, $allvariableshashref);
1719
1720    $installer::logger::Lang->printf("Reading translation file: %s\n", $translationfilename);
1721
1722	return $translationfile;
1723}
1724
1725####################################################
1726# Removing english, if it was added before
1727####################################################
1728
1729sub remove_english_for_nsis_installer
1730{
1731	my ($languagestringref, $languagesarrayref) = @_;
1732
1733	# $$languagestringref =~ s/en-US_//;
1734	# shift(@{$languagesarrayref});
1735
1736	@{$languagesarrayref} = ("en-US");	# only english for NSIS installer!
1737}
1738
1739####################################################
1740# Creating link tree for upload
1741####################################################
1742
1743sub create_link_tree
1744{
1745	my ($sourcedownloadfile, $destfilename, $versionstring) = @_;
1746
1747	if ( ! $installer::globals::ooouploaddir ) { installer::exiter::exit_program("ERROR: Directory for AOO upload not defined!", "create_link_tree"); }
1748	my $versiondir = $installer::globals::ooouploaddir . $installer::globals::separator . $versionstring;
1749    $installer::logger::Lang->printf("Directory for the link: %s\n", $versiondir);
1750
1751	if ( ! -d $versiondir ) { installer::systemactions::create_directory_structure($versiondir); }
1752
1753	# inside directory $versiondir all links have to be created
1754	my $linkdestination = $versiondir . $installer::globals::separator . $destfilename;
1755
1756	# If there is an older version of this file (link), it has to be removed
1757	if ( -f $linkdestination ) { unlink($linkdestination); }
1758
1759    $installer::logger::Lang->printf("Creating hard link from %s to %s\n", $sourcedownloadfile, $linkdestination);
1760	installer::systemactions::hardlink_one_file($sourcedownloadfile, $linkdestination);
1761}
1762
1763#######################################################
1764# Setting supported platform for Sun OpenOffice.org
1765# builds
1766#######################################################
1767
1768sub is_supported_platform
1769{
1770	my $is_supported = 0;
1771
1772	if (( $installer::globals::islinuxrpmbuild ) ||
1773		( $installer::globals::issolarissparcbuild ) ||
1774		( $installer::globals::issolarisx86build ) ||
1775		( $installer::globals::iswindowsbuild ))
1776	{
1777		$is_supported = 1;
1778	}
1779
1780	return $is_supported;
1781}
1782
1783####################################################
1784# Creating download installation sets
1785####################################################
1786
1787sub create_download_sets
1788{
1789	my ($installationdir, $includepatharrayref, $allvariableshashref, $downloadname, $languagestringref, $languagesarrayref) = @_;
1790
1791    $installer::logger::Info->print("\n");
1792    $installer::logger::Info->print("******************************************\n");
1793    $installer::logger::Info->print("... creating download installation set ...\n", 1);
1794    $installer::logger::Info->print("******************************************\n");
1795
1796	installer::logger::include_header_into_logfile("Creating download installation sets:");
1797
1798	# special handling for installation sets, to which english was added automatically
1799	if ( $installer::globals::added_english ) { remove_english_for_nsis_installer($languagestringref, $languagesarrayref); }
1800
1801	my $firstdir = $installationdir;
1802	installer::pathanalyzer::get_path_from_fullqualifiedname(\$firstdir);
1803
1804	my $lastdir = $installationdir;
1805	installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$lastdir);
1806
1807	if ( $lastdir =~ /\./ ) { $lastdir =~ s/\./_download_inprogress\./ }
1808	else { $lastdir = $lastdir . "_download_inprogress"; }
1809
1810	# removing existing directory "_native_packed_inprogress" and "_native_packed_witherror" and "_native_packed"
1811
1812	my $downloaddir = $firstdir . $lastdir;
1813
1814	if ( -d $downloaddir ) { installer::systemactions::remove_complete_directory($downloaddir); }
1815
1816	my $olddir = $downloaddir;
1817	$olddir =~ s/_inprogress/_witherror/;
1818	if ( -d $olddir ) { installer::systemactions::remove_complete_directory($olddir); }
1819
1820	$olddir = $downloaddir;
1821	$olddir =~ s/_inprogress//;
1822	if ( -d $olddir ) { installer::systemactions::remove_complete_directory($olddir); }
1823
1824	# creating the new directory
1825
1826	installer::systemactions::create_directory($downloaddir);
1827
1828	$installer::globals::saveinstalldir = $downloaddir;
1829
1830	# evaluating the name of the download file
1831
1832	if ( $allvariableshashref->{'AOODOWNLOADNAME'} )
1833    {
1834        $downloadname = set_download_filename($languagestringref, $allvariableshashref);
1835    }
1836	else
1837    {
1838        $downloadname = resolve_variables_in_downloadname($allvariableshashref, $downloadname, $languagestringref);
1839    }
1840
1841	if ( ! $installer::globals::iswindowsbuild )	# Unix specific part
1842	{
1843
1844		# getting the path of the getuid.so (only required for Solaris and Linux)
1845		my $getuidlibrary = "";
1846		if (( $installer::globals::issolarisbuild ) || ( $installer::globals::islinuxbuild )) {	$getuidlibrary = get_path_for_library($includepatharrayref); }
1847
1848		if ( $allvariableshashref->{'AOODOWNLOADNAME'} )
1849		{
1850			my $downloadfile = create_tar_gz_file_from_directory($installationdir, $getuidlibrary, $downloaddir, $downloadname);
1851		}
1852		else
1853		{
1854			# find and read setup script template
1855			my $scriptfilename = "downloadscript.sh";
1856
1857			my $scriptref = "";
1858
1859			if ( $installer::globals::include_pathes_read )
1860			{
1861				$scriptref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$scriptfilename, $includepatharrayref, 0);
1862			}
1863			else
1864			{
1865				$scriptref = installer::scriptitems::get_sourcepath_from_filename_and_includepath_classic(\$scriptfilename, $includepatharrayref, 0);
1866			}
1867
1868			if ($$scriptref eq "") { installer::exiter::exit_program("ERROR: Could not find script file $scriptfilename!", "create_download_sets"); }
1869			my $scriptfile = installer::files::read_file($$scriptref);
1870
1871            $installer::logger::Lang->printf("Found  script file %s: %s \n", $scriptfilename, $$scriptref);
1872
1873			# add product name into script template
1874			put_productname_into_script($scriptfile, $allvariableshashref);
1875
1876			# replace linenumber in script template
1877			put_linenumber_into_script($scriptfile);
1878
1879			# create tar file
1880			my $temporary_tarfile_name = $downloaddir . $installer::globals::separator . 'installset.tar';
1881			my $size = tar_package($installationdir, $temporary_tarfile_name, $getuidlibrary);
1882			installer::exiter::exit_program("ERROR: Could not create tar file $temporary_tarfile_name!", "create_download_sets") unless $size;
1883
1884			# calling sum to determine checksum and size of the tar file
1885			my $sumout = call_sum($temporary_tarfile_name);
1886
1887			# writing checksum and size into scriptfile
1888			put_checksum_and_size_into_script($scriptfile, $sumout);
1889
1890			# saving the script file
1891			my $newscriptfilename = determine_scriptfile_name($downloadname);
1892			$newscriptfilename = save_script_file($downloaddir, $newscriptfilename, $scriptfile);
1893
1894            $installer::logger::Info->printf("... including installation set into %s ... \n", $newscriptfilename);
1895            # Append tar file to script
1896			include_tar_into_script($newscriptfilename, $temporary_tarfile_name);
1897		}
1898	}
1899	else	# Windows specific part
1900	{
1901		my $localnsisdir = installer::systemactions::create_directories("nsis", $languagestringref);
1902		# push(@installer::globals::removedirs, $localnsisdir);
1903
1904		# find nsis in the system
1905		my $nsispath = get_path_to_nsis_sdk();
1906
1907		if ( $nsispath eq "" ) {
1908			# If nsis is not found just skip the rest of this function
1909			# and do not create the NSIS file.
1910            $installer::logger::Lang->print("\n");
1911            $installer::logger::Lang->printf("No NSIS SDK found. Skipping the generation of NSIS file.\n");
1912            $installer::logger::Info->print("... no NSIS SDK found. Skipping the generation of NSIS file ... \n");
1913			return $downloaddir;
1914		}
1915
1916		# copy language files into nsis directory and translate them
1917		copy_and_translate_nsis_language_files($nsispath, $localnsisdir, $languagesarrayref, $allvariableshashref);
1918
1919		# find and read the nsi file template
1920		my $templatefilename = "downloadtemplate.nsi";
1921
1922		my $templateref = "";
1923
1924		if ( $installer::globals::include_pathes_read )
1925		{
1926			$templateref = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$templatefilename, $includepatharrayref, 0);
1927		}
1928		else
1929		{
1930			$templateref = installer::scriptitems::get_sourcepath_from_filename_and_includepath_classic(\$templatefilename, $includepatharrayref, 0);
1931		}
1932
1933		if ($$templateref eq "") { installer::exiter::exit_program("ERROR: Could not find nsi template file $templatefilename!", "create_download_sets"); }
1934		my $templatefile = installer::files::read_file($$templateref);
1935
1936		# add product name into script template
1937		put_windows_productname_into_template($templatefile, $allvariableshashref);
1938		put_banner_bmp_into_template($templatefile, $includepatharrayref, $allvariableshashref);
1939		put_welcome_bmp_into_template($templatefile, $includepatharrayref, $allvariableshashref);
1940		put_setup_ico_into_template($templatefile, $includepatharrayref, $allvariableshashref);
1941		put_publisher_into_template($templatefile, $allvariableshashref);
1942		put_website_into_template($templatefile, $allvariableshashref);
1943		put_javafilename_into_template($templatefile, $allvariableshashref);
1944		put_windows_productversion_into_template($templatefile, $allvariableshashref);
1945		put_windows_productpath_into_template($templatefile, $allvariableshashref, $languagestringref, $localnsisdir);
1946		put_outputfilename_into_template($templatefile, $downloadname);
1947		put_filelist_into_template($templatefile, $installationdir);
1948		put_language_list_into_template($templatefile, $languagesarrayref);
1949		put_nsis_path_into_template($templatefile, $localnsisdir);
1950		put_output_path_into_template($templatefile, $downloaddir);
1951
1952		my $nsifilename = save_script_file($localnsisdir, $templatefilename, $templatefile);
1953
1954        $installer::logger::Info->printf("... created NSIS file %s ... \n", $nsifilename);
1955
1956		# starting the NSIS SDK to create the download file
1957		call_nsis($nsispath, $nsifilename);
1958	}
1959
1960	return $downloaddir;
1961}
1962
1963####################################################
1964# Creating AOO upload tree
1965####################################################
1966
1967sub create_download_link_tree
1968{
1969	my ($downloaddir, $languagestringref, $allvariableshashref) = @_;
1970
1971    $installer::logger::Info->print("\n");
1972    $installer::logger::Info->print("******************************************\n"); #
1973    $installer::logger::Info->print("... creating download hard link ...\n");
1974    $installer::logger::Info->print("******************************************\n");
1975
1976	installer::logger::include_header_into_logfile("Creating download hard link:");
1977	$installer::logger::Lang->print("\n");
1978	$installer::logger::Lang->add_timestamp("Performance Info: Creating hard link, start");
1979
1980	if ( is_supported_platform() )
1981	{
1982		my $versionstring = "";
1983		# Already defined $installer::globals::oooversionstring and $installer::globals::ooodownloadfilename ?
1984
1985		if ( ! $installer::globals::oooversionstring ) { $versionstring = get_current_version(); }
1986		else { $versionstring = $installer::globals::oooversionstring; }
1987
1988		# Is $versionstring empty? If yes, there is nothing to do now.
1989
1990        $installer::logger::Lang->printf("Version string is set to: %s\n", $versionstring);
1991
1992		if ( $versionstring )
1993		{
1994			# Now the downloadfilename has to be set (if not already done)
1995			my $destdownloadfilename = "";
1996			if ( ! $installer::globals::ooodownloadfilename ) { $destdownloadfilename = set_download_filename($languagestringref, $versionstring, $allvariableshashref); }
1997			else { $destdownloadfilename = $installer::globals::ooodownloadfilename; }
1998
1999			if ( $destdownloadfilename )
2000			{
2001				$destdownloadfilename = $destdownloadfilename . $installer::globals::downloadfileextension;
2002
2003                $installer::logger::Lang->printf("Setting destination download file name: %s\n", $destdownloadfilename);
2004
2005				my $sourcedownloadfile = $downloaddir . $installer::globals::separator . $installer::globals::downloadfilename;
2006
2007                $installer::logger::Lang->printf("Setting source download file name: %s\n", $sourcedownloadfile);
2008
2009				create_link_tree($sourcedownloadfile, $destdownloadfilename, $versionstring);
2010				# my $md5sumoutput = call_md5sum($downloadfile);
2011				# my $md5sum = get_md5sum($md5sumoutput);
2012
2013			}
2014		}
2015		else
2016		{
2017            $installer::logger::Lang->printf("Version string is empty. Nothing to do!\n");
2018        }
2019	}
2020	else
2021	{
2022        $installer::logger::Lang->printf("Platform not used for hard linking. Nothing to do!\n");
2023	}
2024
2025	$installer::logger::Lang->add_timestamp("Performance Info: Creating hard link, stop");
2026}
2027
20281;
2029