1*9780544fSAndrew Rist#**************************************************************
2*9780544fSAndrew Rist#
3*9780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*9780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*9780544fSAndrew Rist#  distributed with this work for additional information
6*9780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*9780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*9780544fSAndrew Rist#  "License"); you may not use this file except in compliance
9*9780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
10*9780544fSAndrew Rist#
11*9780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*9780544fSAndrew Rist#
13*9780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*9780544fSAndrew Rist#  software distributed under the License is distributed on an
15*9780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
17*9780544fSAndrew Rist#  specific language governing permissions and limitations
18*9780544fSAndrew Rist#  under the License.
19*9780544fSAndrew Rist#
20*9780544fSAndrew Rist#**************************************************************
21*9780544fSAndrew Rist
22*9780544fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweirpackage installer::scpzipfiles;
25cdf0e10cSrcweir
26cdf0e10cSrcweiruse installer::files;
27cdf0e10cSrcweiruse installer::globals;
28cdf0e10cSrcweiruse installer::logger;
29cdf0e10cSrcweiruse installer::pathanalyzer;
30cdf0e10cSrcweiruse installer::systemactions;
31cdf0e10cSrcweir
32cdf0e10cSrcweir########################################################################################
33cdf0e10cSrcweir# Replacing all zip list variables in setup script and files with flag scpzip_replace
34cdf0e10cSrcweir########################################################################################
35cdf0e10cSrcweir
36cdf0e10cSrcweirsub replace_all_ziplistvariables_in_file
37cdf0e10cSrcweir{
38cdf0e10cSrcweir	my ( $fileref, $variableshashref ) = @_;
39cdf0e10cSrcweir
40cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$fileref}; $i++ )
41cdf0e10cSrcweir	{
42cdf0e10cSrcweir		my $line = ${$fileref}[$i];
43cdf0e10cSrcweir
44cdf0e10cSrcweir		if ( $line =~ /^.*\$\{\w+\}.*$/ )	# only occurence of ${abc}
45cdf0e10cSrcweir		{
46cdf0e10cSrcweir			my $key;
47cdf0e10cSrcweir
48cdf0e10cSrcweir			foreach $key (keys %{$variableshashref})
49cdf0e10cSrcweir			{
50cdf0e10cSrcweir				my $value = $variableshashref->{$key};
51cdf0e10cSrcweir				$key = '${' . $key . '}';
52cdf0e10cSrcweir				$line =~ s/\Q$key\E/$value/g;
53cdf0e10cSrcweir				${$fileref}[$i] = $line;
54cdf0e10cSrcweir			}
55cdf0e10cSrcweir		}
56cdf0e10cSrcweir	}
57cdf0e10cSrcweir}
58cdf0e10cSrcweir
59cdf0e10cSrcweir########################################################################################
60cdf0e10cSrcweir# Replacing all zip list variables in rtf files. In rtf files
61cdf0e10cSrcweir# the brackets are masked.
62cdf0e10cSrcweir########################################################################################
63cdf0e10cSrcweir
64cdf0e10cSrcweirsub replace_all_ziplistvariables_in_rtffile
65cdf0e10cSrcweir{
66cdf0e10cSrcweir	my ( $fileref, $variablesref, $onelanguage, $loggingdir ) = @_;
67cdf0e10cSrcweir
68cdf0e10cSrcweir	# installer::files::save_file($loggingdir . "license_" . $onelanguage . "_before.rtf", $fileref);
69cdf0e10cSrcweir
70cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$fileref}; $i++ )
71cdf0e10cSrcweir	{
72cdf0e10cSrcweir		my $line = ${$fileref}[$i];
73cdf0e10cSrcweir
74cdf0e10cSrcweir		if ( $line =~ /^.*\$\\\{\w+\\\}.*$/ )	# only occurence of $\{abc\}
75cdf0e10cSrcweir		{
76cdf0e10cSrcweir			for ( my $j = 0; $j <= $#{$variablesref}; $j++ )
77cdf0e10cSrcweir			{
78cdf0e10cSrcweir				my $variableline = ${$variablesref}[$j];
79cdf0e10cSrcweir
80cdf0e10cSrcweir				my ($key, $value);
81cdf0e10cSrcweir
82cdf0e10cSrcweir				if ( $variableline =~ /^\s*([\w-]+?)\s+(.*?)\s*$/ )
83cdf0e10cSrcweir				{
84cdf0e10cSrcweir					$key = $1;
85cdf0e10cSrcweir					$value = $2;
86cdf0e10cSrcweir					$key = '$\{' . $key . '\}';
87cdf0e10cSrcweir				}
88cdf0e10cSrcweir
89cdf0e10cSrcweir				$line =~ s/\Q$key\E/$value/g;
90cdf0e10cSrcweir
91cdf0e10cSrcweir				${$fileref}[$i] = $line;
92cdf0e10cSrcweir			}
93cdf0e10cSrcweir		}
94cdf0e10cSrcweir	}
95cdf0e10cSrcweir
96cdf0e10cSrcweir	# installer::files::save_file($loggingdir . "license_" . $onelanguage . "_after.rtf", $fileref);
97cdf0e10cSrcweir}
98cdf0e10cSrcweir
99cdf0e10cSrcweir#########################################################
100cdf0e10cSrcweir# Analyzing files with flag SCPZIP_REPLACE
101cdf0e10cSrcweir# $item can be "File" or "ScpAction"
102cdf0e10cSrcweir#########################################################
103cdf0e10cSrcweir
104cdf0e10cSrcweirsub resolving_scpzip_replace_flag
105cdf0e10cSrcweir{
106cdf0e10cSrcweir	my ($filesarrayref, $variableshashref, $item, $languagestringref) = @_;
107cdf0e10cSrcweir
108cdf0e10cSrcweir	my $diritem = lc($item);
109cdf0e10cSrcweir
110cdf0e10cSrcweir	my $replacedirbase = installer::systemactions::create_directories("replace_$diritem", $languagestringref);
111cdf0e10cSrcweir
112cdf0e10cSrcweir	installer::logger::include_header_into_logfile("$item with flag SCPZIP:");
113cdf0e10cSrcweir
114cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
115cdf0e10cSrcweir	{
116cdf0e10cSrcweir		my $onefile = ${$filesarrayref}[$i];
117cdf0e10cSrcweir		my $styles = "";
118cdf0e10cSrcweir
119cdf0e10cSrcweir		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
120cdf0e10cSrcweir
121cdf0e10cSrcweir		if ( $styles =~ /\bSCPZIP_REPLACE\b/ )
122cdf0e10cSrcweir		{
123cdf0e10cSrcweir			# Language specific subdirectory
124cdf0e10cSrcweir
125cdf0e10cSrcweir			my $onelanguage = $onefile->{'specificlanguage'};
126cdf0e10cSrcweir
127cdf0e10cSrcweir			if ($onelanguage eq "")
128cdf0e10cSrcweir			{
129cdf0e10cSrcweir				$onelanguage = "00";	# files without language into directory "00"
130cdf0e10cSrcweir			}
131cdf0e10cSrcweir
132cdf0e10cSrcweir			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
133cdf0e10cSrcweir			installer::systemactions::create_directory($replacedir);	# creating language specific directories
134cdf0e10cSrcweir
135cdf0e10cSrcweir			# copy files and edit them with the variables defined in the zip.lst
136cdf0e10cSrcweir
137cdf0e10cSrcweir			my $longfilename = 0;
138cdf0e10cSrcweir
139cdf0e10cSrcweir			my $onefilename = $onefile->{'Name'};
140cdf0e10cSrcweir			my $sourcepath = $onefile->{'sourcepath'};
141cdf0e10cSrcweir
142cdf0e10cSrcweir			if ( $onefilename =~ /^\s*\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
143cdf0e10cSrcweir			{
144cdf0e10cSrcweir				$onefilename =~ s/^\s*\Q$installer::globals::separator\E//;
145cdf0e10cSrcweir				$longfilename = 1;
146cdf0e10cSrcweir			}
147cdf0e10cSrcweir
148cdf0e10cSrcweir			my $destinationpath = $replacedir . $onefilename;
149cdf0e10cSrcweir			my $movepath = $destinationpath . ".orig";
150cdf0e10cSrcweir
151cdf0e10cSrcweir			if ( $longfilename )	# the destination directory has to be created before copying
152cdf0e10cSrcweir			{
153cdf0e10cSrcweir				my $destdir = $movepath;
154cdf0e10cSrcweir				installer::pathanalyzer::get_path_from_fullqualifiedname(\$destdir);
155cdf0e10cSrcweir				installer::systemactions::create_directory_structure($destdir);
156cdf0e10cSrcweir			}
157cdf0e10cSrcweir
158cdf0e10cSrcweir			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
159cdf0e10cSrcweir
160cdf0e10cSrcweir			if ( $copysuccess )
161cdf0e10cSrcweir			{
162cdf0e10cSrcweir				# Now the file can be edited
163cdf0e10cSrcweir				# ToDo: How about binary patching?
164cdf0e10cSrcweir
165cdf0e10cSrcweir				my $onefileref = installer::files::read_file($movepath);
166cdf0e10cSrcweir				replace_all_ziplistvariables_in_file($onefileref, $variableshashref);
167cdf0e10cSrcweir				installer::files::save_file($destinationpath ,$onefileref);
168cdf0e10cSrcweir			}
169cdf0e10cSrcweir
170cdf0e10cSrcweir			# Saving the original source, where the file was found
171cdf0e10cSrcweir			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
172cdf0e10cSrcweir
173cdf0e10cSrcweir			# Writing the new sourcepath into the hashref, even if it was no copied
174cdf0e10cSrcweir
175cdf0e10cSrcweir			$onefile->{'sourcepath'} = $destinationpath;
176cdf0e10cSrcweir		}
177cdf0e10cSrcweir	}
178cdf0e10cSrcweir
179cdf0e10cSrcweir	my $infoline = "\n";
180cdf0e10cSrcweir	push( @installer::globals::logfileinfo, $infoline);
181cdf0e10cSrcweir}
182cdf0e10cSrcweir
183cdf0e10cSrcweir1;
184