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