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::scppatchsoname;
25
26use installer::files;
27use installer::globals;
28use installer::logger;
29use installer::setupscript;
30use installer::systemactions;
31
32########################################################################################
33# The length of the new string must be identical with the length of the old string
34########################################################################################
35
36sub change_length_of_string
37{
38	my ($newstringref, $oldstring) = @_;
39
40	while ( length($$newstringref) < length($oldstring) )
41	{
42		$$newstringref = $$newstringref . chr(0);
43	}
44}
45
46########################################################################################
47# The length of the new string must be identical with the length of the old string
48########################################################################################
49
50sub change_length_of_string_with_letter
51{
52	my ($newstringref, $oldstring, $onestring) = @_;
53
54	while ( length($$newstringref) < length($oldstring) )
55	{
56		$$newstringref = $$newstringref . $onestring;
57	}
58}
59
60########################################################################################
61# Converting a string to a unicode string
62########################################################################################
63
64sub convert_to_unicode
65{
66	my ($string) = @_;
67
68	my $unicodestring = "";
69
70	my $stringlength = length($string);
71
72	for ( my $i = 0; $i < $stringlength; $i++ )
73	{
74		$unicodestring = $unicodestring . substr($string, $i, 1);
75		$unicodestring = $unicodestring . chr(0);
76	}
77
78	return $unicodestring;
79}
80
81########################################################################################
82# Replacing the so name in all files with flag PATCH_SO_NAME
83########################################################################################
84
85sub replace_productname_in_file
86{
87	my ($sourcepath, $destpath, $variableshashref, $onefilehash, $styles) = @_;
88
89	my $onefile = installer::files::read_binary_file($sourcepath);
90
91	# searching for "x"
92
93	my $onestring = "x" . chr(0);
94	my $replacestring = "";
95	for ( my $i = 1; $i <= 80; $i++ ) { $replacestring .= $onestring; }
96
97	my $productname = $variableshashref->{'PRODUCTNAME'} . " " . $variableshashref->{'PRODUCTVERSION'};
98	if ( exists($onefilehash->{'FileDescription'}) ) { $productname = $onefilehash->{'FileDescription'}; }
99	my $unicode_productname = convert_to_unicode($productname);
100
101	change_length_of_string(\$unicode_productname, $replacestring);
102
103	my $found = $onefile =~ s/$replacestring/$unicode_productname/sg;
104
105	installer::files::save_binary_file($onefile, $destpath);
106
107	return $found;
108}
109
110#########################################################
111# Analyzing files with flag PATCH_SO_NAME
112#########################################################
113
114sub resolving_patchsoname_flag
115{
116	my ($filesarrayref, $variableshashref, $item, $languagestringref) = @_;
117
118	my $diritem = lc($item);
119
120	my $replacedirbase = installer::systemactions::create_directories("patchsoname_$diritem", $languagestringref);
121
122	installer::logger::include_header_into_logfile("$item with flag PATCH_SO_NAME:");
123
124	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
125	{
126		my $onefile = ${$filesarrayref}[$i];
127		my $styles = "";
128
129		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
130
131		if ( $styles =~ /\bPATCH_SO_NAME\b/ )
132		{
133			# Language specific subdirectory
134
135			my $onelanguage = $onefile->{'specificlanguage'};
136            my $filedescription = "";
137
138			if ($onelanguage eq "")
139			{
140				$onelanguage = "00";	# files without language into directory "00"
141			}
142
143			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
144			installer::systemactions::create_directory($replacedir);	# creating language specific directories
145
146			# copy files and edit them with the variables defined in the zip.lst
147
148			my $onefilename = $onefile->{'Name'};
149			my $sourcepath = $onefile->{'sourcepath'};
150			my $destinationpath = $replacedir . $onefilename;
151			my $movepath = $destinationpath . ".orig";
152
153			# if (!(-f $destinationpath))	# do nothing if the file already exists
154			# {
155
156			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
157
158			if ( $copysuccess )
159			{
160				# Now the file can be patch (binary!)
161				my $found = replace_productname_in_file($movepath, $destinationpath, $variableshashref, $onefile, $styles);
162
163				if ($found == 0)
164				{
165					$installer::logger::Lang->printf("Did not patch the file %s\n", $destinationpath);
166				}
167				else
168				{
169					$installer::logger::Lang->printf("Successfully patched %s, Count: %s\n",
170                        $destinationpath, $found);
171				}
172			}
173
174			# }
175
176			# Saving the original source, where the file was found
177			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
178
179			# Saving the original source, where the file was found
180			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
181
182			# Writing the new sourcepath into the hashref, even if it was no copied
183
184			$onefile->{'sourcepath'} = $destinationpath;
185		}
186	}
187
188	$installer::logger::Lang->print("\n");
189}
190
1911;
192