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::substfilenamefiles;
25cdf0e10cSrcweir
26cdf0e10cSrcweiruse installer::exiter;
27cdf0e10cSrcweiruse installer::globals;
28cdf0e10cSrcweiruse installer::logger;
29cdf0e10cSrcweiruse installer::pathanalyzer;
30cdf0e10cSrcweiruse installer::systemactions;
31cdf0e10cSrcweir
32cdf0e10cSrcweir#########################################################
33cdf0e10cSrcweir# Analyzing files with flag SUBST_FILENAME
34cdf0e10cSrcweir#########################################################
35cdf0e10cSrcweir
36cdf0e10cSrcweirsub resolving_subst_filename_flag
37cdf0e10cSrcweir{
38cdf0e10cSrcweir	my ($filesarrayref, $variableshashref, $languagestringref) = @_;
39cdf0e10cSrcweir
40cdf0e10cSrcweir	my $replacedirbase = installer::systemactions::create_directories("change_filename", $languagestringref);
41cdf0e10cSrcweir
42cdf0e10cSrcweir	installer::logger::include_header_into_logfile("Files with flag SUBST_FILENAME:");
43cdf0e10cSrcweir
44cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
45cdf0e10cSrcweir	{
46cdf0e10cSrcweir		my $onefile = ${$filesarrayref}[$i];
47cdf0e10cSrcweir		my $styles = "";
48cdf0e10cSrcweir
49cdf0e10cSrcweir		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
50cdf0e10cSrcweir
51cdf0e10cSrcweir		if ( $styles =~ /\bSUBST_FILENAME\b/ )
52cdf0e10cSrcweir		{
53cdf0e10cSrcweir			# Files with flag SUBST_FILENAME must have a "Substitute" key
54cdf0e10cSrcweir			if (( ! $onefile->{'Substitute'} ) && ( ! $onefile->{'InstallName'} ))
55cdf0e10cSrcweir			{
56cdf0e10cSrcweir				installer::exiter::exit_program("ERROR: SUBST_FILENAME is set, but no Substitute and no InstallName defined at file $onefile->{'gid'}!", "resolving_subst_filename_flag");
57cdf0e10cSrcweir			}
58cdf0e10cSrcweir
59cdf0e10cSrcweir			# Language specific subdirectory
60cdf0e10cSrcweir			my $onelanguage = $onefile->{'specificlanguage'};
61cdf0e10cSrcweir
62cdf0e10cSrcweir			if ($onelanguage eq "")
63cdf0e10cSrcweir			{
64cdf0e10cSrcweir				$onelanguage = "00";	# files without language into directory "00"
65cdf0e10cSrcweir			}
66cdf0e10cSrcweir
67cdf0e10cSrcweir			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
68cdf0e10cSrcweir			installer::systemactions::create_directory($replacedir);	# creating language specific directories
69cdf0e10cSrcweir
70cdf0e10cSrcweir			# copy files and edit them with the variables defined in the zip.lst
71cdf0e10cSrcweir
72cdf0e10cSrcweir			my $longfilename = 0;
73cdf0e10cSrcweir
74cdf0e10cSrcweir			my $onefilename = $onefile->{'Name'};
75cdf0e10cSrcweir
76cdf0e10cSrcweir			my $sourcepath = $onefile->{'sourcepath'};
77cdf0e10cSrcweir
78cdf0e10cSrcweir			# if ( $onefilename =~ /^\s*\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
79cdf0e10cSrcweir			if ( $onefilename =~ /\Q$installer::globals::separator\E/ )	# filename begins with a slash, for instance /registry/schema/org/openoffice/VCL.xcs
80cdf0e10cSrcweir			{
81cdf0e10cSrcweir				$onefilename =~ s/^\s*\Q$installer::globals::separator\E//;
82cdf0e10cSrcweir				$longfilename = 1;
83cdf0e10cSrcweir			}
84cdf0e10cSrcweir
85cdf0e10cSrcweir			my $destinationpath = $replacedir . $onefilename;
86cdf0e10cSrcweir			my $movepath = $destinationpath . ".orig";
87cdf0e10cSrcweir			my $destdir = $replacedir;
88cdf0e10cSrcweir
89cdf0e10cSrcweir			if ( $longfilename )	# the destination directory has to be created before copying
90cdf0e10cSrcweir			{
91cdf0e10cSrcweir				$destdir = $movepath;
92cdf0e10cSrcweir				installer::pathanalyzer::get_path_from_fullqualifiedname(\$destdir);
93cdf0e10cSrcweir				installer::systemactions::create_directory_structure($destdir);
94cdf0e10cSrcweir			}
95cdf0e10cSrcweir
96cdf0e10cSrcweir			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
97cdf0e10cSrcweir
98cdf0e10cSrcweir			if ( $copysuccess )
99cdf0e10cSrcweir			{
100cdf0e10cSrcweir				if ( $onefile->{'Substitute'} )
101cdf0e10cSrcweir				{
102cdf0e10cSrcweir					my $substitute = $onefile->{'Substitute'};
103cdf0e10cSrcweir
104cdf0e10cSrcweir					my $newfilename = $destinationpath;
105cdf0e10cSrcweir					installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$newfilename);
106cdf0e10cSrcweir					eval '$newfilename =~ ' . "$substitute";
107cdf0e10cSrcweir
108cdf0e10cSrcweir					my $longnewfilename = $destdir . $newfilename;
109cdf0e10cSrcweir
110cdf0e10cSrcweir					$copysuccess = installer::systemactions::copy_one_file($movepath, $longnewfilename);
111cdf0e10cSrcweir
112cdf0e10cSrcweir					# Saving the new file name
113cdf0e10cSrcweir					$onefile->{'Name'} = $newfilename;
114cdf0e10cSrcweir
115cdf0e10cSrcweir					# Saving the new destination
116cdf0e10cSrcweir					my $newdest = $onefile->{'destination'};
117cdf0e10cSrcweir					installer::pathanalyzer::get_path_from_fullqualifiedname(\$newdest);
118cdf0e10cSrcweir					$onefile->{'destination'} = $newdest . $newfilename;
119cdf0e10cSrcweir
120cdf0e10cSrcweir					# Saving the original source, where the file was found
121cdf0e10cSrcweir					$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
122cdf0e10cSrcweir
123cdf0e10cSrcweir					# Writing the new sourcepath into the hashref, even if it was not copied
124cdf0e10cSrcweir					$onefile->{'sourcepath'} = $longnewfilename;
125cdf0e10cSrcweir				}
126cdf0e10cSrcweir				else
127cdf0e10cSrcweir				{
128cdf0e10cSrcweir					if ( $onefile->{'InstallName'} )
129cdf0e10cSrcweir					{
130cdf0e10cSrcweir						my $installname = $onefile->{'InstallName'};
131cdf0e10cSrcweir
132cdf0e10cSrcweir						my $newfilename = $destinationpath;
133cdf0e10cSrcweir						installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$newfilename);
134cdf0e10cSrcweir
135cdf0e10cSrcweir						my $longnewfilename = $destdir . $installname;
136cdf0e10cSrcweir
137cdf0e10cSrcweir						$copysuccess = installer::systemactions::copy_one_file($movepath, $longnewfilename);
138cdf0e10cSrcweir
139cdf0e10cSrcweir						# Saving the new file name
140cdf0e10cSrcweir						$onefile->{'Name'} = $installname;
141cdf0e10cSrcweir
142cdf0e10cSrcweir						# Saving the new destination
143cdf0e10cSrcweir						my $newdest = $onefile->{'destination'};
144cdf0e10cSrcweir						installer::pathanalyzer::get_path_from_fullqualifiedname(\$newdest);
145cdf0e10cSrcweir						$onefile->{'destination'} = $newdest . $installname;
146cdf0e10cSrcweir
147cdf0e10cSrcweir						# Saving the original source, where the file was found
148cdf0e10cSrcweir						$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
149cdf0e10cSrcweir
150cdf0e10cSrcweir						# Writing the new sourcepath into the hashref, even if it was not copied
151cdf0e10cSrcweir						$onefile->{'sourcepath'} = $longnewfilename;
152cdf0e10cSrcweir					}
153cdf0e10cSrcweir				}
154cdf0e10cSrcweir			}
155cdf0e10cSrcweir		}
156cdf0e10cSrcweir	}
157cdf0e10cSrcweir
158*b274bc22SAndre Fischer    $installer::logger::Lang->printf("\n");
159cdf0e10cSrcweir}
160cdf0e10cSrcweir
161cdf0e10cSrcweir1;
162