1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28package installer::scpzipfiles;
29
30use installer::files;
31use installer::globals;
32use installer::logger;
33use installer::pathanalyzer;
34use installer::systemactions;
35
36########################################################################################
37# Replacing all zip list variables in setup script and files with flag scpzip_replace
38########################################################################################
39
40sub replace_all_ziplistvariables_in_file
41{
42	my ( $fileref, $variableshashref ) = @_;
43
44	for ( my $i = 0; $i <= $#{$fileref}; $i++ )
45	{
46		my $line = ${$fileref}[$i];
47
48		if ( $line =~ /^.*\$\{\w+\}.*$/ )	# only occurence of ${abc}
49		{
50			my $key;
51
52			foreach $key (keys %{$variableshashref})
53			{
54				my $value = $variableshashref->{$key};
55				$key = '${' . $key . '}';
56				$line =~ s/\Q$key\E/$value/g;
57				${$fileref}[$i] = $line;
58			}
59		}
60	}
61}
62
63########################################################################################
64# Replacing all zip list variables in rtf files. In rtf files
65# the brackets are masked.
66########################################################################################
67
68sub replace_all_ziplistvariables_in_rtffile
69{
70	my ( $fileref, $variablesref, $onelanguage, $loggingdir ) = @_;
71
72	# installer::files::save_file($loggingdir . "license_" . $onelanguage . "_before.rtf", $fileref);
73
74	for ( my $i = 0; $i <= $#{$fileref}; $i++ )
75	{
76		my $line = ${$fileref}[$i];
77
78		if ( $line =~ /^.*\$\\\{\w+\\\}.*$/ )	# only occurence of $\{abc\}
79		{
80			for ( my $j = 0; $j <= $#{$variablesref}; $j++ )
81			{
82				my $variableline = ${$variablesref}[$j];
83
84				my ($key, $value);
85
86				if ( $variableline =~ /^\s*([\w-]+?)\s+(.*?)\s*$/ )
87				{
88					$key = $1;
89					$value = $2;
90					$key = '$\{' . $key . '\}';
91				}
92
93				$line =~ s/\Q$key\E/$value/g;
94
95				${$fileref}[$i] = $line;
96			}
97		}
98	}
99
100	# installer::files::save_file($loggingdir . "license_" . $onelanguage . "_after.rtf", $fileref);
101}
102
103#########################################################
104# Analyzing files with flag SCPZIP_REPLACE
105# $item can be "File" or "ScpAction"
106#########################################################
107
108sub resolving_scpzip_replace_flag
109{
110	my ($filesarrayref, $variableshashref, $item, $languagestringref) = @_;
111
112	my $diritem = lc($item);
113
114	my $replacedirbase = installer::systemactions::create_directories("replace_$diritem", $languagestringref);
115
116	installer::logger::include_header_into_logfile("$item with flag SCPZIP:");
117
118	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
119	{
120		my $onefile = ${$filesarrayref}[$i];
121		my $styles = "";
122
123		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
124
125		if ( $styles =~ /\bSCPZIP_REPLACE\b/ )
126		{
127			# Language specific subdirectory
128
129			my $onelanguage = $onefile->{'specificlanguage'};
130
131			if ($onelanguage eq "")
132			{
133				$onelanguage = "00";	# files without language into directory "00"
134			}
135
136			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
137			installer::systemactions::create_directory($replacedir);	# creating language specific directories
138
139			# copy files and edit them with the variables defined in the zip.lst
140
141			my $longfilename = 0;
142
143			my $onefilename = $onefile->{'Name'};
144			my $sourcepath = $onefile->{'sourcepath'};
145
146			if ( $onefilename =~ /^\s*\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
147			{
148				$onefilename =~ s/^\s*\Q$installer::globals::separator\E//;
149				$longfilename = 1;
150			}
151
152			my $destinationpath = $replacedir . $onefilename;
153			my $movepath = $destinationpath . ".orig";
154
155			if ( $longfilename )	# the destination directory has to be created before copying
156			{
157				my $destdir = $movepath;
158				installer::pathanalyzer::get_path_from_fullqualifiedname(\$destdir);
159				installer::systemactions::create_directory_structure($destdir);
160			}
161
162			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
163
164			if ( $copysuccess )
165			{
166				# Now the file can be edited
167				# ToDo: How about binary patching?
168
169				my $onefileref = installer::files::read_file($movepath);
170				replace_all_ziplistvariables_in_file($onefileref, $variableshashref);
171				installer::files::save_file($destinationpath ,$onefileref);
172			}
173
174			# Saving the original source, where the file was found
175			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
176
177			# Writing the new sourcepath into the hashref, even if it was no copied
178
179			$onefile->{'sourcepath'} = $destinationpath;
180		}
181	}
182
183	my $infoline = "\n";
184	push( @installer::globals::logfileinfo, $infoline);
185}
186
1871;
188