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 ($lines, $variablesref) = @_;
67
68    my $line_count = scalar @$lines;
69	for (my $i=0; $i<=$line_count; ++$i)
70	{
71		my $line = $lines->[$i];
72
73		if ($line =~ /\$\\\{/) # early rejection of lines without variable references
74		{
75            while (my ($key, $value) = each (%$variables))
76			{
77                my $pattern = '$\{' . $key . '\}';
78				$line =~ s/\Q$key\E/$value/g;
79
80			}
81            $lines->[$i] = $line;
82		}
83	}
84}
85
86#########################################################
87# Analyzing files with flag SCPZIP_REPLACE
88# $item can be "File" or "ScpAction"
89#########################################################
90
91sub resolving_scpzip_replace_flag
92{
93	my ($filesarrayref, $variableshashref, $item, $languagestringref) = @_;
94
95	my $diritem = lc($item);
96
97	my $replacedirbase = installer::systemactions::create_directories("replace_$diritem", $languagestringref);
98
99	installer::logger::include_header_into_logfile("$item with flag SCPZIP:");
100
101	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
102	{
103		my $onefile = ${$filesarrayref}[$i];
104		my $styles = "";
105
106		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
107
108		if ( $styles =~ /\bSCPZIP_REPLACE\b/ )
109		{
110			# Language specific subdirectory
111
112			my $onelanguage = $onefile->{'specificlanguage'};
113
114			if ($onelanguage eq "")
115			{
116				$onelanguage = "00";	# files without language into directory "00"
117			}
118
119			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
120			installer::systemactions::create_directory($replacedir);	# creating language specific directories
121
122			# copy files and edit them with the variables defined in the zip.lst
123
124			my $longfilename = 0;
125
126			my $onefilename = $onefile->{'Name'};
127			my $sourcepath = $onefile->{'sourcepath'};
128
129			if ( $onefilename =~ /^\s*\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
130			{
131				$onefilename =~ s/^\s*\Q$installer::globals::separator\E//;
132				$longfilename = 1;
133			}
134
135			my $destinationpath = $replacedir . $onefilename;
136			my $movepath = $destinationpath . ".orig";
137
138			if ( $longfilename )	# the destination directory has to be created before copying
139			{
140				my $destdir = $movepath;
141				installer::pathanalyzer::get_path_from_fullqualifiedname(\$destdir);
142				installer::systemactions::create_directory_structure($destdir);
143			}
144
145			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
146
147			if ( $copysuccess )
148			{
149				# Now the file can be edited
150				# ToDo: How about binary patching?
151
152				my $onefileref = installer::files::read_file($movepath);
153				replace_all_ziplistvariables_in_file($onefileref, $variableshashref);
154				installer::files::save_file($destinationpath ,$onefileref);
155			}
156
157			# Saving the original source, where the file was found
158			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
159
160			# Writing the new sourcepath into the hashref, even if it was no copied
161
162			$onefile->{'sourcepath'} = $destinationpath;
163		}
164	}
165
166    $installer::logger::Lang->printf("\n");
167}
168
1691;
170