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