19780544fSAndrew Rist#**************************************************************
29780544fSAndrew Rist#
39780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59780544fSAndrew Rist#  distributed with this work for additional information
69780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
89780544fSAndrew Rist#  "License"); you may not use this file except in compliance
99780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
109780544fSAndrew Rist#
119780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
129780544fSAndrew Rist#
139780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
149780544fSAndrew Rist#  software distributed under the License is distributed on an
159780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
179780544fSAndrew Rist#  specific language governing permissions and limitations
189780544fSAndrew Rist#  under the License.
199780544fSAndrew Rist#
209780544fSAndrew Rist#**************************************************************
219780544fSAndrew Rist
229780544fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweirpackage installer::scpzipfiles;
25cdf0e10cSrcweir
26cdf0e10cSrcweiruse installer::files;
27cdf0e10cSrcweiruse installer::globals;
28cdf0e10cSrcweiruse installer::logger;
29cdf0e10cSrcweiruse installer::pathanalyzer;
30cdf0e10cSrcweiruse installer::systemactions;
31cdf0e10cSrcweir
32*dba1a2e4SAndre Fischeruse strict;
33*dba1a2e4SAndre Fischer
34cdf0e10cSrcweir########################################################################################
35cdf0e10cSrcweir# Replacing all zip list variables in setup script and files with flag scpzip_replace
36cdf0e10cSrcweir########################################################################################
37cdf0e10cSrcweir
38*dba1a2e4SAndre Fischersub replace_all_ziplistvariables_in_file ($$)
39cdf0e10cSrcweir{
40*dba1a2e4SAndre Fischer	my ($lines, $variables) = @_;
41cdf0e10cSrcweir
42*dba1a2e4SAndre Fischer    my $count = scalar @$lines;
43*dba1a2e4SAndre Fischer	for (my $lineno=0; $lineno<$count; ++$lineno)
44cdf0e10cSrcweir	{
45*dba1a2e4SAndre Fischer        my $line = $lines->[$lineno];
46*dba1a2e4SAndre Fischer		if ($line =~ /\$\{/)  # early rejection of lines that don't need replacements
47*dba1a2e4SAndre Fischer		{
48*dba1a2e4SAndre Fischer            while (my ($key,$value) = each %$variables)
49cdf0e10cSrcweir			{
50*dba1a2e4SAndre Fischer				my $pattern = '${' . $key . '}';
51*dba1a2e4SAndre Fischer				my $replacement_count = ($line =~ s/\Q$pattern\E/$value/g);
52*dba1a2e4SAndre Fischer                if ($key eq "PRODUCTADDON" && $replacement_count>0)
53*dba1a2e4SAndre Fischer                {
54*dba1a2e4SAndre Fischer                    $installer::logger::Lang->printf(
55*dba1a2e4SAndre Fischer                        "replaced PRODUCTADDON %d times in line %d\n",
56*dba1a2e4SAndre Fischer                        $replacement_count,
57*dba1a2e4SAndre Fischer                        $lineno);
58*dba1a2e4SAndre Fischer                }
59cdf0e10cSrcweir			}
60*dba1a2e4SAndre Fischer            $lines->[$lineno] = $line;
61cdf0e10cSrcweir		}
62cdf0e10cSrcweir	}
63cdf0e10cSrcweir}
64cdf0e10cSrcweir
65cdf0e10cSrcweir########################################################################################
66cdf0e10cSrcweir# Replacing all zip list variables in rtf files. In rtf files
67cdf0e10cSrcweir# the brackets are masked.
68cdf0e10cSrcweir########################################################################################
69cdf0e10cSrcweir
70dca4887fSAndre Fischersub replace_all_ziplistvariables_in_rtffile ($$)
71cdf0e10cSrcweir{
72*dba1a2e4SAndre Fischer	my ($lines, $variables) = @_;
73cdf0e10cSrcweir
74dca4887fSAndre Fischer    my $line_count = scalar @$lines;
75dca4887fSAndre Fischer	for (my $i=0; $i<=$line_count; ++$i)
76cdf0e10cSrcweir	{
77dca4887fSAndre Fischer		my $line = $lines->[$i];
78cdf0e10cSrcweir
79dca4887fSAndre Fischer		if ($line =~ /\$\\\{/) # early rejection of lines without variable references
80dca4887fSAndre Fischer		{
81dca4887fSAndre Fischer            while (my ($key, $value) = each (%$variables))
82cdf0e10cSrcweir			{
83dca4887fSAndre Fischer                my $pattern = '$\{' . $key . '\}';
84cdf0e10cSrcweir				$line =~ s/\Q$key\E/$value/g;
85cdf0e10cSrcweir
86cdf0e10cSrcweir			}
87dca4887fSAndre Fischer            $lines->[$i] = $line;
88cdf0e10cSrcweir		}
89cdf0e10cSrcweir	}
90cdf0e10cSrcweir}
91cdf0e10cSrcweir
92cdf0e10cSrcweir#########################################################
93cdf0e10cSrcweir# Analyzing files with flag SCPZIP_REPLACE
94cdf0e10cSrcweir# $item can be "File" or "ScpAction"
95cdf0e10cSrcweir#########################################################
96cdf0e10cSrcweir
97cdf0e10cSrcweirsub resolving_scpzip_replace_flag
98cdf0e10cSrcweir{
99cdf0e10cSrcweir	my ($filesarrayref, $variableshashref, $item, $languagestringref) = @_;
100cdf0e10cSrcweir
101cdf0e10cSrcweir	my $diritem = lc($item);
102cdf0e10cSrcweir
103cdf0e10cSrcweir	my $replacedirbase = installer::systemactions::create_directories("replace_$diritem", $languagestringref);
104cdf0e10cSrcweir
105cdf0e10cSrcweir	installer::logger::include_header_into_logfile("$item with flag SCPZIP:");
106cdf0e10cSrcweir
107cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
108cdf0e10cSrcweir	{
109cdf0e10cSrcweir		my $onefile = ${$filesarrayref}[$i];
110cdf0e10cSrcweir		my $styles = "";
111cdf0e10cSrcweir
112cdf0e10cSrcweir		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
113cdf0e10cSrcweir
114cdf0e10cSrcweir		if ( $styles =~ /\bSCPZIP_REPLACE\b/ )
115cdf0e10cSrcweir		{
116cdf0e10cSrcweir			# Language specific subdirectory
117cdf0e10cSrcweir
118cdf0e10cSrcweir			my $onelanguage = $onefile->{'specificlanguage'};
119cdf0e10cSrcweir
120cdf0e10cSrcweir			if ($onelanguage eq "")
121cdf0e10cSrcweir			{
122cdf0e10cSrcweir				$onelanguage = "00";	# files without language into directory "00"
123cdf0e10cSrcweir			}
124cdf0e10cSrcweir
125cdf0e10cSrcweir			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
126cdf0e10cSrcweir			installer::systemactions::create_directory($replacedir);	# creating language specific directories
127cdf0e10cSrcweir
128cdf0e10cSrcweir			# copy files and edit them with the variables defined in the zip.lst
129cdf0e10cSrcweir
130cdf0e10cSrcweir			my $longfilename = 0;
131cdf0e10cSrcweir
132cdf0e10cSrcweir			my $onefilename = $onefile->{'Name'};
133cdf0e10cSrcweir			my $sourcepath = $onefile->{'sourcepath'};
134cdf0e10cSrcweir
135cdf0e10cSrcweir			if ( $onefilename =~ /^\s*\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
136cdf0e10cSrcweir			{
137cdf0e10cSrcweir				$onefilename =~ s/^\s*\Q$installer::globals::separator\E//;
138cdf0e10cSrcweir				$longfilename = 1;
139cdf0e10cSrcweir			}
140cdf0e10cSrcweir
141cdf0e10cSrcweir			my $destinationpath = $replacedir . $onefilename;
142cdf0e10cSrcweir			my $movepath = $destinationpath . ".orig";
143cdf0e10cSrcweir
144cdf0e10cSrcweir			if ( $longfilename )	# the destination directory has to be created before copying
145cdf0e10cSrcweir			{
146cdf0e10cSrcweir				my $destdir = $movepath;
147cdf0e10cSrcweir				installer::pathanalyzer::get_path_from_fullqualifiedname(\$destdir);
148cdf0e10cSrcweir				installer::systemactions::create_directory_structure($destdir);
149cdf0e10cSrcweir			}
150cdf0e10cSrcweir
151cdf0e10cSrcweir			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
152cdf0e10cSrcweir
153cdf0e10cSrcweir			if ( $copysuccess )
154cdf0e10cSrcweir			{
155cdf0e10cSrcweir				# Now the file can be edited
156cdf0e10cSrcweir				# ToDo: How about binary patching?
157cdf0e10cSrcweir
158cdf0e10cSrcweir				my $onefileref = installer::files::read_file($movepath);
159cdf0e10cSrcweir				replace_all_ziplistvariables_in_file($onefileref, $variableshashref);
160cdf0e10cSrcweir				installer::files::save_file($destinationpath ,$onefileref);
161cdf0e10cSrcweir			}
162cdf0e10cSrcweir
163cdf0e10cSrcweir			# Saving the original source, where the file was found
164cdf0e10cSrcweir			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
165cdf0e10cSrcweir
166cdf0e10cSrcweir			# Writing the new sourcepath into the hashref, even if it was no copied
167cdf0e10cSrcweir
168cdf0e10cSrcweir			$onefile->{'sourcepath'} = $destinationpath;
169cdf0e10cSrcweir		}
170cdf0e10cSrcweir	}
171cdf0e10cSrcweir
172b274bc22SAndre Fischer    $installer::logger::Lang->printf("\n");
173cdf0e10cSrcweir}
174cdf0e10cSrcweir
175cdf0e10cSrcweir1;
176