1*b1cdbd2cSJim Jagielski#**************************************************************
2*b1cdbd2cSJim Jagielski#
3*b1cdbd2cSJim Jagielski#  Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski#  or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski#  distributed with this work for additional information
6*b1cdbd2cSJim Jagielski#  regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski#  to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski#  "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski#  with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski#
11*b1cdbd2cSJim Jagielski#    http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski#
13*b1cdbd2cSJim Jagielski#  Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski#  software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski#  KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski#  specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski#  under the License.
19*b1cdbd2cSJim Jagielski#
20*b1cdbd2cSJim Jagielski#**************************************************************
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielskipackage installer::substfilenamefiles;
25*b1cdbd2cSJim Jagielski
26*b1cdbd2cSJim Jagielskiuse installer::exiter;
27*b1cdbd2cSJim Jagielskiuse installer::globals;
28*b1cdbd2cSJim Jagielskiuse installer::logger;
29*b1cdbd2cSJim Jagielskiuse installer::pathanalyzer;
30*b1cdbd2cSJim Jagielskiuse installer::systemactions;
31*b1cdbd2cSJim Jagielski
32*b1cdbd2cSJim Jagielski#########################################################
33*b1cdbd2cSJim Jagielski# Analyzing files with flag SUBST_FILENAME
34*b1cdbd2cSJim Jagielski#########################################################
35*b1cdbd2cSJim Jagielski
36*b1cdbd2cSJim Jagielskisub resolving_subst_filename_flag
37*b1cdbd2cSJim Jagielski{
38*b1cdbd2cSJim Jagielski	my ($filesarrayref, $variableshashref, $languagestringref) = @_;
39*b1cdbd2cSJim Jagielski
40*b1cdbd2cSJim Jagielski	my $replacedirbase = installer::systemactions::create_directories("change_filename", $languagestringref);
41*b1cdbd2cSJim Jagielski
42*b1cdbd2cSJim Jagielski	installer::logger::include_header_into_logfile("Files with flag SUBST_FILENAME:");
43*b1cdbd2cSJim Jagielski
44*b1cdbd2cSJim Jagielski	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
45*b1cdbd2cSJim Jagielski	{
46*b1cdbd2cSJim Jagielski		my $onefile = ${$filesarrayref}[$i];
47*b1cdbd2cSJim Jagielski		my $styles = "";
48*b1cdbd2cSJim Jagielski
49*b1cdbd2cSJim Jagielski		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
50*b1cdbd2cSJim Jagielski
51*b1cdbd2cSJim Jagielski		if ( $styles =~ /\bSUBST_FILENAME\b/ )
52*b1cdbd2cSJim Jagielski		{
53*b1cdbd2cSJim Jagielski			# Files with flag SUBST_FILENAME must have a "Substitute" key
54*b1cdbd2cSJim Jagielski			if (( ! $onefile->{'Substitute'} ) && ( ! $onefile->{'InstallName'} ))
55*b1cdbd2cSJim Jagielski			{
56*b1cdbd2cSJim Jagielski				installer::exiter::exit_program("ERROR: SUBST_FILENAME is set, but no Substitute and no InstallName defined at file $onefile->{'gid'}!", "resolving_subst_filename_flag");
57*b1cdbd2cSJim Jagielski			}
58*b1cdbd2cSJim Jagielski
59*b1cdbd2cSJim Jagielski			# Language specific subdirectory
60*b1cdbd2cSJim Jagielski			my $onelanguage = $onefile->{'specificlanguage'};
61*b1cdbd2cSJim Jagielski
62*b1cdbd2cSJim Jagielski			if ($onelanguage eq "")
63*b1cdbd2cSJim Jagielski			{
64*b1cdbd2cSJim Jagielski				$onelanguage = "00";	# files without language into directory "00"
65*b1cdbd2cSJim Jagielski			}
66*b1cdbd2cSJim Jagielski
67*b1cdbd2cSJim Jagielski			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
68*b1cdbd2cSJim Jagielski			installer::systemactions::create_directory($replacedir);	# creating language specific directories
69*b1cdbd2cSJim Jagielski
70*b1cdbd2cSJim Jagielski			# copy files and edit them with the variables defined in the zip.lst
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski			my $longfilename = 0;
73*b1cdbd2cSJim Jagielski
74*b1cdbd2cSJim Jagielski			my $onefilename = $onefile->{'Name'};
75*b1cdbd2cSJim Jagielski
76*b1cdbd2cSJim Jagielski			my $sourcepath = $onefile->{'sourcepath'};
77*b1cdbd2cSJim Jagielski
78*b1cdbd2cSJim Jagielski			# if ( $onefilename =~ /^\s*\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
79*b1cdbd2cSJim Jagielski			if ( $onefilename =~ /\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
80*b1cdbd2cSJim Jagielski			{
81*b1cdbd2cSJim Jagielski				$onefilename =~ s/^\s*\Q$installer::globals::separator\E//;
82*b1cdbd2cSJim Jagielski				$longfilename = 1;
83*b1cdbd2cSJim Jagielski			}
84*b1cdbd2cSJim Jagielski
85*b1cdbd2cSJim Jagielski			my $destinationpath = $replacedir . $onefilename;
86*b1cdbd2cSJim Jagielski			my $movepath = $destinationpath . ".orig";
87*b1cdbd2cSJim Jagielski			my $destdir = $replacedir;
88*b1cdbd2cSJim Jagielski
89*b1cdbd2cSJim Jagielski			if ( $longfilename )	# the destination directory has to be created before copying
90*b1cdbd2cSJim Jagielski			{
91*b1cdbd2cSJim Jagielski				$destdir = $movepath;
92*b1cdbd2cSJim Jagielski				installer::pathanalyzer::get_path_from_fullqualifiedname(\$destdir);
93*b1cdbd2cSJim Jagielski				installer::systemactions::create_directory_structure($destdir);
94*b1cdbd2cSJim Jagielski			}
95*b1cdbd2cSJim Jagielski
96*b1cdbd2cSJim Jagielski			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
97*b1cdbd2cSJim Jagielski
98*b1cdbd2cSJim Jagielski			if ( $copysuccess )
99*b1cdbd2cSJim Jagielski			{
100*b1cdbd2cSJim Jagielski				if ( $onefile->{'Substitute'} )
101*b1cdbd2cSJim Jagielski				{
102*b1cdbd2cSJim Jagielski					my $substitute = $onefile->{'Substitute'};
103*b1cdbd2cSJim Jagielski
104*b1cdbd2cSJim Jagielski					my $newfilename = $destinationpath;
105*b1cdbd2cSJim Jagielski					installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$newfilename);
106*b1cdbd2cSJim Jagielski					eval '$newfilename =~ ' . "$substitute";
107*b1cdbd2cSJim Jagielski
108*b1cdbd2cSJim Jagielski					my $longnewfilename = $destdir . $newfilename;
109*b1cdbd2cSJim Jagielski
110*b1cdbd2cSJim Jagielski					$copysuccess = installer::systemactions::copy_one_file($movepath, $longnewfilename);
111*b1cdbd2cSJim Jagielski
112*b1cdbd2cSJim Jagielski					# Saving the new file name
113*b1cdbd2cSJim Jagielski					$onefile->{'Name'} = $newfilename;
114*b1cdbd2cSJim Jagielski
115*b1cdbd2cSJim Jagielski					# Saving the new destination
116*b1cdbd2cSJim Jagielski					my $newdest = $onefile->{'destination'};
117*b1cdbd2cSJim Jagielski					installer::pathanalyzer::get_path_from_fullqualifiedname(\$newdest);
118*b1cdbd2cSJim Jagielski					$onefile->{'destination'} = $newdest . $newfilename;
119*b1cdbd2cSJim Jagielski
120*b1cdbd2cSJim Jagielski					# Saving the original source, where the file was found
121*b1cdbd2cSJim Jagielski					$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
122*b1cdbd2cSJim Jagielski
123*b1cdbd2cSJim Jagielski					# Writing the new sourcepath into the hashref, even if it was not copied
124*b1cdbd2cSJim Jagielski					$onefile->{'sourcepath'} = $longnewfilename;
125*b1cdbd2cSJim Jagielski				}
126*b1cdbd2cSJim Jagielski				else
127*b1cdbd2cSJim Jagielski				{
128*b1cdbd2cSJim Jagielski					if ( $onefile->{'InstallName'} )
129*b1cdbd2cSJim Jagielski					{
130*b1cdbd2cSJim Jagielski						my $installname = $onefile->{'InstallName'};
131*b1cdbd2cSJim Jagielski
132*b1cdbd2cSJim Jagielski						my $newfilename = $destinationpath;
133*b1cdbd2cSJim Jagielski						installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$newfilename);
134*b1cdbd2cSJim Jagielski
135*b1cdbd2cSJim Jagielski						my $longnewfilename = $destdir . $installname;
136*b1cdbd2cSJim Jagielski
137*b1cdbd2cSJim Jagielski						$copysuccess = installer::systemactions::copy_one_file($movepath, $longnewfilename);
138*b1cdbd2cSJim Jagielski
139*b1cdbd2cSJim Jagielski						# Saving the new file name
140*b1cdbd2cSJim Jagielski						$onefile->{'Name'} = $installname;
141*b1cdbd2cSJim Jagielski
142*b1cdbd2cSJim Jagielski						# Saving the new destination
143*b1cdbd2cSJim Jagielski						my $newdest = $onefile->{'destination'};
144*b1cdbd2cSJim Jagielski						installer::pathanalyzer::get_path_from_fullqualifiedname(\$newdest);
145*b1cdbd2cSJim Jagielski						$onefile->{'destination'} = $newdest . $installname;
146*b1cdbd2cSJim Jagielski
147*b1cdbd2cSJim Jagielski						# Saving the original source, where the file was found
148*b1cdbd2cSJim Jagielski						$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
149*b1cdbd2cSJim Jagielski
150*b1cdbd2cSJim Jagielski						# Writing the new sourcepath into the hashref, even if it was not copied
151*b1cdbd2cSJim Jagielski						$onefile->{'sourcepath'} = $longnewfilename;
152*b1cdbd2cSJim Jagielski					}
153*b1cdbd2cSJim Jagielski				}
154*b1cdbd2cSJim Jagielski			}
155*b1cdbd2cSJim Jagielski		}
156*b1cdbd2cSJim Jagielski	}
157*b1cdbd2cSJim Jagielski
158*b1cdbd2cSJim Jagielski    $installer::logger::Lang->printf("\n");
159*b1cdbd2cSJim Jagielski}
160*b1cdbd2cSJim Jagielski
161*b1cdbd2cSJim Jagielski1;
162