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::scpzipfiles;
25
26use installer::files;
27use installer::globals;
28use installer::logger;
29use installer::pathanalyzer;
30use installer::systemactions;
31
32########################################################################################
33# Replacing all zip list variables in setup script and files with flag scpzip_replace
34########################################################################################
35
36sub replace_all_ziplistvariables_in_file
37{
38	my ( $fileref, $variableshashref ) = @_;
39
40	for ( my $i = 0; $i <= $#{$fileref}; $i++ )
41	{
42		my $line = ${$fileref}[$i];
43
44		if ( $line =~ /^.*\$\{\w+\}.*$/ )	# only occurence of ${abc}
45		{
46			my $key;
47
48			foreach $key (keys %{$variableshashref})
49			{
50				my $value = $variableshashref->{$key};
51				$key = '${' . $key . '}';
52				$line =~ s/\Q$key\E/$value/g;
53				${$fileref}[$i] = $line;
54			}
55		}
56	}
57}
58
59########################################################################################
60# Replacing all zip list variables in rtf files. In rtf files
61# the brackets are masked.
62########################################################################################
63
64sub replace_all_ziplistvariables_in_rtffile
65{
66	my ( $fileref, $variablesref, $onelanguage, $loggingdir ) = @_;
67
68	# installer::files::save_file($loggingdir . "license_" . $onelanguage . "_before.rtf", $fileref);
69
70	for ( my $i = 0; $i <= $#{$fileref}; $i++ )
71	{
72		my $line = ${$fileref}[$i];
73
74		if ( $line =~ /^.*\$\\\{\w+\\\}.*$/ )	# only occurence of $\{abc\}
75		{
76			for ( my $j = 0; $j <= $#{$variablesref}; $j++ )
77			{
78				my $variableline = ${$variablesref}[$j];
79
80				my ($key, $value);
81
82				if ( $variableline =~ /^\s*([\w-]+?)\s+(.*?)\s*$/ )
83				{
84					$key = $1;
85					$value = $2;
86					$key = '$\{' . $key . '\}';
87				}
88
89				$line =~ s/\Q$key\E/$value/g;
90
91				${$fileref}[$i] = $line;
92			}
93		}
94	}
95
96	# installer::files::save_file($loggingdir . "license_" . $onelanguage . "_after.rtf", $fileref);
97}
98
99#########################################################
100# Analyzing files with flag SCPZIP_REPLACE
101# $item can be "File" or "ScpAction"
102#########################################################
103
104sub resolving_scpzip_replace_flag
105{
106	my ($filesarrayref, $variableshashref, $item, $languagestringref) = @_;
107
108	my $diritem = lc($item);
109
110	my $replacedirbase = installer::systemactions::create_directories("replace_$diritem", $languagestringref);
111
112	installer::logger::include_header_into_logfile("$item with flag SCPZIP:");
113
114	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
115	{
116		my $onefile = ${$filesarrayref}[$i];
117		my $styles = "";
118
119		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
120
121		if ( $styles =~ /\bSCPZIP_REPLACE\b/ )
122		{
123			# Language specific subdirectory
124
125			my $onelanguage = $onefile->{'specificlanguage'};
126
127			if ($onelanguage eq "")
128			{
129				$onelanguage = "00";	# files without language into directory "00"
130			}
131
132			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
133			installer::systemactions::create_directory($replacedir);	# creating language specific directories
134
135			# copy files and edit them with the variables defined in the zip.lst
136
137			my $longfilename = 0;
138
139			my $onefilename = $onefile->{'Name'};
140			my $sourcepath = $onefile->{'sourcepath'};
141
142			if ( $onefilename =~ /^\s*\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
143			{
144				$onefilename =~ s/^\s*\Q$installer::globals::separator\E//;
145				$longfilename = 1;
146			}
147
148			my $destinationpath = $replacedir . $onefilename;
149			my $movepath = $destinationpath . ".orig";
150
151			if ( $longfilename )	# the destination directory has to be created before copying
152			{
153				my $destdir = $movepath;
154				installer::pathanalyzer::get_path_from_fullqualifiedname(\$destdir);
155				installer::systemactions::create_directory_structure($destdir);
156			}
157
158			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
159
160			if ( $copysuccess )
161			{
162				# Now the file can be edited
163				# ToDo: How about binary patching?
164
165				my $onefileref = installer::files::read_file($movepath);
166				replace_all_ziplistvariables_in_file($onefileref, $variableshashref);
167				installer::files::save_file($destinationpath ,$onefileref);
168			}
169
170			# Saving the original source, where the file was found
171			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
172
173			# Writing the new sourcepath into the hashref, even if it was no copied
174
175			$onefile->{'sourcepath'} = $destinationpath;
176		}
177	}
178
179    $installer::logger::Lang->printf("\n");
180}
181
1821;
183