--- /dev/null
+#! /usr/bin/perl
+# mergechangelogs -- Tool to merge Debian changelogs
+#
+# Copyright (C) 2006 Nokia
+#
+# This program is free software. It is distributed under the terms of
+# the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, you can find it on the World Wide
+# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
+# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+use strict;
+use warnings;
+
+my @logs = ([]);
+
+# Read changelogs.
+foreach my $file ( @ARGV ) {
+ local $/;
+ open LOG,'<',$file or die;
+ push @logs, [
+ map { s/^\s*//s; s/\s*$/\n/s; $_; }
+ split /\n\n\b/, <LOG>
+ ];
+ close LOG;
+}
+
+# Merge changelogs.
+# If there are entries for the same version in multiple changelogs,
+# take the entry from the last changelog.
+while ( @logs >= 2 ) {
+ my (@entries,@vers,$index);
+ while ( @{$logs[0]} > 0 && @{$logs[1]} > 0 ) {
+ if ( $logs[0]->[0] eq $logs[1]->[0] ) {
+ push @entries, $logs[1]->[0];
+ shift @{$logs[0]};
+ shift @{$logs[1]};
+ next;
+ }
+ ($vers[0]) = $logs[0]->[0] =~ /^[^\n()]+ [(]([^()]+)[)]/s;
+ ($vers[1]) = $logs[1]->[0] =~ /^[^\n()]+ [(]([^()]+)[)]/s;
+ if ( defined $vers[0] && defined $vers[1] ) {
+ if ( $vers[0] eq $vers[1] ) {
+ push @entries, $logs[1]->[0];
+ shift @{$logs[0]};
+ shift @{$logs[1]};
+ next;
+ }
+ my $status = system
+ 'dpkg',
+ '--compare-versions',
+ $vers[0], 'gt', $vers[1];
+ $status == 0 || $status == 1 << 8 || die;
+ $index = $status == 0 ? 0 : 1;
+ }
+ elsif ( defined $vers[0] ) {
+ $index = 0;
+ }
+ elsif ( defined $vers[1] ) {
+ $index = 1;
+ }
+ else {
+ last;
+ }
+ push @entries, shift @{$logs[$index]};
+ }
+ push @entries, @{$logs[0]}, @{$logs[1]};
+ splice @logs, 0, 2, \@entries;
+}
+
+# Print the merges changelog.
+print join "\n", @{$logs[0]};