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 readIncVersions($);
34sub processLine($$);
35sub checkName($);
36sub incrementNewVersion($);
37sub incrementOldVersion($);
38sub incrementPolicyVersion($);
39my $usage =
40"The tool increments the minor version of assemblies and the major version of ".
41"the respective policy files. This is only done if new uno types have been added since".
42"the last product upate. This information is obtained from the file which is passed as ".
43"argument changedTypes. The names in the version file must have a particular form. ".
44"They must end on one of folling terms: NEW_VERSION, OLD_VERSION, POLICY_VERSION\n".
45"If no new published types habe been added then no output, argument  newVersions, is written".
46"Usage is: \n increment_version.pl oldVersions  incVersions newVersions changedTypes\n\n".
47"oldVersion: Contains name value pairs, which are used for forming the config files of ".
48"the policy assemblies, for building the assemblies. \n\n".
49"incVersions: File containing the names of which the versions are to be incremented. ".
50"Every line may only contain one name. The names must exactly match those from the ".
51"oldVersion file.\n\n".
52"newVersions: Contains all entries from oldVersions, but the values of the names,".
53"which occur in selection, have been incremented.\n\n".
54"changedTypes: File that contains the information if new published types have been added ".
55"since the last product update.\n\n" ;
56
57my $sNameForm =
58"The names must end on one of these names: NEW_VERSION, OLD_VERSION, POLICY_VERSION\n".
59"For example, valid names are: \n".
60"CLI_URETYPES_NEW_VERSION\nCLI_URETYPES_OLD_VERSION\nCLI_URETYPES_POLICY_VERSION\n";
61
62if (scalar @ARGV < 4) {
63   print $usage;
64   exit -1;
65}
66
67-e "$ARGV[0]" or die "Error: wrong arguments. \n".$usage;
68-e "$ARGV[1]" or die "Error: wrong arguments. \n".$usage;
69-e "$ARGV[3]" or die "Error: wrong arguments. \n".$usage;
70
71#check if new types have been added since last release.
72#If not, then there is nothing to be done.
73#read in oldVersions line by line and apply the increment operation
74open TYPES, "$ARGV[3]" or die "Cannot open to $ARGV[3] $!";
75
76my $newTypes;
77
78#We look for the line that contains the number of new types
79while(<TYPES>)
80{
81    if (/New and published types/i)
82    {
83	$_ =~ /=\s*(\d+)/;
84	if ( ! defined $1)
85	{
86	    print "\n###$ARGV[3] contains an invalid entry for 'New and published types'.  \n\n";
87	    exit -1;
88	}
89	$newTypes = $1;
90    }
91}
92
93#Check if changeTypes contained the line we are looking for
94if (! defined $newTypes)
95{
96    print "\n###$ARGV[3] does not contain entry about the new types ".
97	"or we are looking for the wrong string! \n\n";
98    exit -1;
99}
100
101if ( $newTypes == 0)
102{
103    print "\nNo new UNO types since las product update.\n";
104    exit 0;
105}
106else
107{
108    print "\nNew UNO types were addes since last release. The version will be increased.\n\n";
109}
110
111#read in incVersions in a list
112my @incVersions = readIncVersions($ARGV[1]);
113#print "@incVersions";
114
115#read in oldVersions line by line and apply the increment operation
116open OLDVERSION, "$ARGV[0]" or die "Cannot open to $ARGV[0] $!";
117
118#open file we want to write to
119open NEWVERSION, "> $ARGV[2]" or die "Cannot write to $ARGV[2] $!";
120
121print NEWVERSION processLine($_, @incVersions) while(<OLDVERSION>);
122
123close NEWVERSION;
124close OLDVERSION;
125
126exit 0;
127
128sub processLine($$)
129{
130    my $line = $_[0];
131    #skip empty lines
132    my $trimmed;
133    return $line if (length($trimmed = trim($line)) == 0);
134    #Skip comment symbol: #
135    return $line if ($trimmed =~ /^#/);
136
137    #Get the left part of '='
138    my $i = index($line, "=");
139    if( $i == -1)
140    {
141	print "Error: No '=' found in line:,: \n $line \n";
142	exit -1;
143    }
144    my $name = substr($line, 0, $i);
145    $name = trim($name);
146    #We do not check the names here because the file can contain
147    #other names, e.g. CLI_URETYPES_POLICY_ASSEMBLY
148    if (length($name) == 0) {
149	print "Wrong line in $ARGV[0]\n", $sNameForm;
150	exit -1;
151    }
152    my $value = substr($line, $i + 1);
153    $value = trim($value);
154
155    #Check if the entry shall be incremented, this information is in the second
156    #argument
157    my $found;
158    for(@incVersions) {
159	if ($_ eq $name) {
160	    $found = 1;
161	    last;
162	}
163    }
164    if ( ! defined($found)) {
165	 return $line;
166    }
167
168    #Check if the name represents a version we need to change
169    if ($name =~ /NEW_VERSION$/)
170    {
171	$value = incrementNewVersion($value);
172    }
173    elsif ($name =~ /OLD_VERSION$/)
174    {
175	$value = incrementOldVersion($value);
176    }
177    elsif ($name =~ /POLICY_VERSION$/)
178    {
179	$value = incrementPolicyVersion($value);
180    }
181    else
182    {
183	#other name which we ignore
184	return $line;
185    }
186    return "${name}=${value}\n";
187}
188
189#The value of a new version has the form x.x.x.x
190#We increment the third position from the left.
191#Te argument must already be trimmed.
192sub incrementNewVersion($)
193{
194   my @parts = split /\./,$_[0];
195   if (scalar @parts != 4)
196   {
197       print "Error, no valid version given in $ARGV[0]\n. A 'new version' has four parts.";
198       exit -1;
199   }
200   $parts[2]++;
201   #build the version string and return
202   return "$parts[0].$parts[1].$parts[2].$parts[3]";
203}
204
205#The value of a new version has the form x.x.x.x-x.x.x.x
206#We increment the third position of the second part.
207#Te argument must already be trimmed.
208sub incrementOldVersion($)
209{
210    my @parts = split /[\.-]/,$_[0];
211    if (scalar @parts != 8)
212    {
213	print "Error, no valid version given in $ARGV[0]\n. A 'old version' has the form
214x.x.x.x-x.x.x.x\n.";
215	exit -1;
216    }
217    $parts[6]++;
218    return "$parts[0].$parts[1].$parts[2].$parts[3]-$parts[4].$parts[5].$parts[6].$parts[7]";
219    return $_[0];
220}
221
222sub incrementPolicyVersion($)
223{
224   my @parts = split /\./,$_[0];
225   if (scalar @parts != 4)
226   {
227       print "Error, no valid version given in $ARGV[0]\n. A 'policy version' has four parts.";
228       exit -1;
229   }
230   $parts[0]++;
231   #build the version string and return
232   return "$parts[0].$parts[1].$parts[2].$parts[3]";
233}
234
235
236sub readIncVersions($)
237{
238    open INC, $_[0] or die "Could not open $_[0] $!";
239    my $arg = $_[0];
240    my @names;
241
242    while(<INC>)
243    {
244	chomp;
245	#Skip empty lines
246	my $line;
247	if (length($line = trim($_)) == 0) {
248	    next;
249	}
250	#Skip comment symbol: #
251	if ($line =~ /^#/) {
252	    next;
253	}
254	if (!checkName($line)) {
255	    print "Wrong entry in file $_[0]\n", $sNameForm;
256	    exit -1;
257	}
258	push @names, $line;
259    }
260    print "No entries found in $arg\n" if(scalar @names == 0);
261    return @names;
262}
263
264#The argument must already be trimmed
265#returns 1 if ok
266sub checkName($)
267{
268    my $name = $_[0];
269    if ( $name !~/NEW_VERSION$|OLD_VERSION$|POLICY_VERSION$/) {
270	return 0;
271    }
272    return 1;
273}
274
275sub trim($)
276{
277	my $string = shift;
278	$string =~ s/^\s+//;
279	$string =~ s/\s+$//;
280	return $string;
281}
282