1*7e90fac2SAndrew Rist#**************************************************************
2*7e90fac2SAndrew Rist#
3*7e90fac2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*7e90fac2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*7e90fac2SAndrew Rist#  distributed with this work for additional information
6*7e90fac2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*7e90fac2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*7e90fac2SAndrew Rist#  "License"); you may not use this file except in compliance
9*7e90fac2SAndrew Rist#  with the License.  You may obtain a copy of the License at
10*7e90fac2SAndrew Rist#
11*7e90fac2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*7e90fac2SAndrew Rist#
13*7e90fac2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*7e90fac2SAndrew Rist#  software distributed under the License is distributed on an
15*7e90fac2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7e90fac2SAndrew Rist#  KIND, either express or implied.  See the License for the
17*7e90fac2SAndrew Rist#  specific language governing permissions and limitations
18*7e90fac2SAndrew Rist#  under the License.
19*7e90fac2SAndrew Rist#
20*7e90fac2SAndrew Rist#**************************************************************
21*7e90fac2SAndrew Rist
22*7e90fac2SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweiruse warnings;
25cdf0e10cSrcweiruse strict;
26cdf0e10cSrcweiruse diagnostics;
27cdf0e10cSrcweir
28cdf0e10cSrcweirsub trim;
29cdf0e10cSrcweirsub readRedirectionValues($);
30cdf0e10cSrcweir
31cdf0e10cSrcweirmy $usage =
32cdf0e10cSrcweir   "Usage is: \n subst_template.pl configTemplate redirections policyConfig
33cdf0e10cSrcweir
34cdf0e10cSrcweir   configTemplate: The config file which is used for the policy assembly. It
35cdf0e10cSrcweir   contains place holders for the binding redirection.
36cdf0e10cSrcweir
37cdf0e10cSrcweir   redirections: file containing the values for oldVersion and newVersion tags
38cdf0e10cSrcweir   which are used in the BindingRedirect element of the config files.
39cdf0e10cSrcweir
40cdf0e10cSrcweir   policyConfig: Name of the file in which we want to write the config file.
41cdf0e10cSrcweir";
42cdf0e10cSrcweir
43cdf0e10cSrcweir
44cdf0e10cSrcweirif (scalar @ARGV < 3) {
45cdf0e10cSrcweir   print $usage;
46cdf0e10cSrcweir   exit -1;
47cdf0e10cSrcweir}
48cdf0e10cSrcweir
49cdf0e10cSrcweir
50cdf0e10cSrcweirmy %redirectionValue = readRedirectionValues($ARGV[1]);
51cdf0e10cSrcweir#print "|$_|  |$redirectionValue{$_}|\n",  for keys %redirectionValue;
52cdf0e10cSrcweir
53cdf0e10cSrcweir
54cdf0e10cSrcweir#Read config file in which we will replace the versions
55cdf0e10cSrcweir$/ = undef;
56cdf0e10cSrcweiropen TEMPLATE, $ARGV[0] or die $!;
57cdf0e10cSrcweirmy $templ = <TEMPLATE>;
58cdf0e10cSrcweir
59cdf0e10cSrcweir#Open the config file we are goint to write to
60cdf0e10cSrcweiropen CONFIG, "> $ARGV[2]" or die "Cannot write to $ARGV[2] $!";
61cdf0e10cSrcweir
62cdf0e10cSrcweir#No substitute the place holders for oldVersion and new Version in the config template with
63cdf0e10cSrcweir#the values obtained from the redirections file
64cdf0e10cSrcweirfor (keys %redirectionValue) {
65cdf0e10cSrcweir    $templ=~ s/\b$_\b/$redirectionValue{$_}/;
66cdf0e10cSrcweir}
67cdf0e10cSrcweir#Write the config file
68cdf0e10cSrcweirprint CONFIG $templ;
69cdf0e10cSrcweir
70cdf0e10cSrcweir#Reads the key value pairs from the files, which name must be passed in
71cdf0e10cSrcweir#the parameter. The file contains lines of the form name=value, for example
72cdf0e10cSrcweir#CLI_TYPES_OLD_VERSION=1.1.0.0-1.1.1.0
73cdf0e10cSrcweirsub readRedirectionValues($)
74cdf0e10cSrcweir{
75cdf0e10cSrcweir    #Read in the values for the version redirection
76cdf0e10cSrcweir    open REDIR, $_[0] or die $!;
77cdf0e10cSrcweir
78cdf0e10cSrcweir    my %redirectionValues;
79cdf0e10cSrcweir
80cdf0e10cSrcweir    while (<REDIR>)
81cdf0e10cSrcweir    {
82cdf0e10cSrcweir        chomp;
83cdf0e10cSrcweir	my $trimmed;
84cdf0e10cSrcweir        #Skip empty lines
85cdf0e10cSrcweir        if (length($trimmed = trim($_)) == 0) {
86cdf0e10cSrcweir            next;
87cdf0e10cSrcweir        }
88cdf0e10cSrcweir
89cdf0e10cSrcweir	#Skip comment symbol: #
90cdf0e10cSrcweir	if ($trimmed =~ /^#/) {
91cdf0e10cSrcweir	    next;
92cdf0e10cSrcweir	}
93cdf0e10cSrcweir
94cdf0e10cSrcweir        my @lineParts = split /=/,$_;
95cdf0e10cSrcweir
96cdf0e10cSrcweir        #Check if we have valid name value pairs.
97cdf0e10cSrcweir        if (scalar @lineParts != 2) {
98cdf0e10cSrcweir            print "Error: Values in $ARGV[1] are not correct (Entries must have the form name=value). Invalid line: \n$_\n";
99cdf0e10cSrcweir            exit -1;
100cdf0e10cSrcweir        }
101cdf0e10cSrcweir
102cdf0e10cSrcweir        #Trim the strings and check if they still contain characters
103cdf0e10cSrcweir        my $name = trim($lineParts[0]);
104cdf0e10cSrcweir        my $value = trim($lineParts[1]);
105cdf0e10cSrcweir        if (length($name) == 0 || length($value) == 0) {
106cdf0e10cSrcweir            print "Error: Values in $ARGV[1] are not correct. Invalid line: \n$_\n";
107cdf0e10cSrcweir            exit -1;
108cdf0e10cSrcweir        }
109cdf0e10cSrcweir
110cdf0e10cSrcweir        #Check if we have duplicate key names
111cdf0e10cSrcweir        for (keys %redirectionValues) {
112cdf0e10cSrcweir            if ( $name eq $_) {
113cdf0e10cSrcweir                print "Error: Values in $ARGV[1] are not correct. The name $_ is not unique.\n";
114cdf0e10cSrcweir                exit -1;
115cdf0e10cSrcweir            }
116cdf0e10cSrcweir        }
117cdf0e10cSrcweir
118cdf0e10cSrcweir        $redirectionValues{$name} = $value;
119cdf0e10cSrcweir    }
120cdf0e10cSrcweir    return %redirectionValues;
121cdf0e10cSrcweir}
122cdf0e10cSrcweir
123cdf0e10cSrcweirsub trim($)
124cdf0e10cSrcweir{
125cdf0e10cSrcweir	my $string = shift;
126cdf0e10cSrcweir	$string =~ s/^\s+//;
127cdf0e10cSrcweir	$string =~ s/\s+$//;
128cdf0e10cSrcweir	return $string;
129cdf0e10cSrcweir}
130