1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28use warnings;
29use strict;
30use diagnostics;
31
32sub trim;
33sub readRedirectionValues($);
34
35my $usage =
36   "Usage is: \n subst_template.pl configTemplate redirections policyConfig
37
38   configTemplate: The config file which is used for the policy assembly. It
39   contains place holders for the binding redirection.
40
41   redirections: file containing the values for oldVersion and newVersion tags
42   which are used in the BindingRedirect element of the config files.
43
44   policyConfig: Name of the file in which we want to write the config file.
45";
46
47
48if (scalar @ARGV < 3) {
49   print $usage;
50   exit -1;
51}
52
53
54my %redirectionValue = readRedirectionValues($ARGV[1]);
55#print "|$_|  |$redirectionValue{$_}|\n",  for keys %redirectionValue;
56
57
58#Read config file in which we will replace the versions
59$/ = undef;
60open TEMPLATE, $ARGV[0] or die $!;
61my $templ = <TEMPLATE>;
62
63#Open the config file we are goint to write to
64open CONFIG, "> $ARGV[2]" or die "Cannot write to $ARGV[2] $!";
65
66#No substitute the place holders for oldVersion and new Version in the config template with
67#the values obtained from the redirections file
68for (keys %redirectionValue) {
69    $templ=~ s/\b$_\b/$redirectionValue{$_}/;
70}
71#Write the config file
72print CONFIG $templ;
73
74#Reads the key value pairs from the files, which name must be passed in
75#the parameter. The file contains lines of the form name=value, for example
76#CLI_TYPES_OLD_VERSION=1.1.0.0-1.1.1.0
77sub readRedirectionValues($)
78{
79    #Read in the values for the version redirection
80    open REDIR, $_[0] or die $!;
81
82    my %redirectionValues;
83
84    while (<REDIR>)
85    {
86        chomp;
87	my $trimmed;
88        #Skip empty lines
89        if (length($trimmed = trim($_)) == 0) {
90            next;
91        }
92
93	#Skip comment symbol: #
94	if ($trimmed =~ /^#/) {
95	    next;
96	}
97
98        my @lineParts = split /=/,$_;
99
100        #Check if we have valid name value pairs.
101        if (scalar @lineParts != 2) {
102            print "Error: Values in $ARGV[1] are not correct (Entries must have the form name=value). Invalid line: \n$_\n";
103            exit -1;
104        }
105
106        #Trim the strings and check if they still contain characters
107        my $name = trim($lineParts[0]);
108        my $value = trim($lineParts[1]);
109        if (length($name) == 0 || length($value) == 0) {
110            print "Error: Values in $ARGV[1] are not correct. Invalid line: \n$_\n";
111            exit -1;
112        }
113
114        #Check if we have duplicate key names
115        for (keys %redirectionValues) {
116            if ( $name eq $_) {
117                print "Error: Values in $ARGV[1] are not correct. The name $_ is not unique.\n";
118                exit -1;
119            }
120        }
121
122        $redirectionValues{$name} = $value;
123    }
124    return %redirectionValues;
125}
126
127sub trim($)
128{
129	my $string = shift;
130	$string =~ s/^\s+//;
131	$string =~ s/\s+$//;
132	return $string;
133}
134