1 #!/usr/bin/perl -w
2 # -*- mode: cperl; coding: utf-8 -*-
3 # Copyright © 2005-2012 Jonas Smedegaard <dr@jones.dk>
4 # Description: Reformat licencecheck output to copyright file format
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2, or (at
9 # your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 my $whitespace_list_delimiter = $ENV{'whitespace_list_delimiter'} || "\n ";
20 my $rfc822_list_delimiter = $ENV{'rfc822_list_delimiter'} || "\n ";
21 my $merge_same_license = $ENV{'merge_same_license'} || "";
23 print "Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\n";
24 print "Upstream-Name: FIXME\n";
25 print "Upstream-Contact: FIXME\n";
26 print "Source: FIXME\n";
27 print "Disclaimer: Autogenerated by CDBS\n\n";
28 $n=0; while (<>) {
29 if (/^([^:\s][^:]+):[\s]+(\S.*?)\s*$/) {
30 $files[$n]{name}=$1;
31 $files[$n]{license}=$2;
32 };
33 if (/^\s*\[Copyright:\s*(\S.*?)\s*\]/) {
34 $files[$n]{copyright}=$1;
35 };
36 /^$/ and $n++;
37 };
38 foreach $file (@files) {
39 $file->{name} =~ s/([*?\\])/\\$1/g;
40 $file->{license} =~ s/\s*\(unversioned\/unknown version\)//;
41 $file->{license} =~ s/\s*\(with incorrect FSF address\)//;
42 $file->{license} =~ s/\s+\(v([^)]+) or later\)/-$1+/;
43 $file->{license} =~ s/\s+\(v([^)]+)\)/-$1/;
44 $file->{license} =~ s/\s*(\*No copyright\*)\s*// and $file->{copyright} = $1;
45 $file->{license} =~ s/^\s*(GENERATED FILE)/UNKNOWN/;
46 $file->{license} =~ s/\s+(GENERATED FILE)//;
47 $file->{license} =~ s/^\s*zlib\/libpng$/Zlib/;
48 $file->{license} =~ s/^\s*MIT\/X11 \(BSD like\)$/Expat/;
49 $file->{license} =~ s/^\s*BSD \((\d) clause\)$/BSD-$1-clause/;
50 $file->{copyright} =~ s/^©\s*//;
51 $file->{copyright} =~ s/(?<=(\b\d{4}))(?{$y=$^N})\s*[,-]\s*((??{$y+1}))\b/-$2/g;
52 $file->{copyright} =~ s/(?<=\b\d{4})\s*-\s*\d{4}(?=\s*-\s*(\d{4})\b)//g;
53 $file->{copyright} =~ s/\b(\d{4}),?\s+([\S^\d])/$1, $2/g;
54 my @ownerlines = grep {/\w\w/} split /\s\/\s/, $file->{copyright};
55 my @ownerlines_clean = ();
56 my %owneryears = ();
57 my $owneryears_seem_correct = 1;
58 for $ownerline ( @ownerlines ) {
59 my ($owneryear, $owner) = $ownerline =~ /^([\d\-,\s]*)\s*+(.*)/;
60 $owneryears_seem_correct = 0 unless ($owneryear);
61 $owner =~ s/^by\s+//;
62 $owner =~ s/,?\s+All Rights Reserved\.?//gi;
63 push @ownerlines_clean, "$owneryear$owner";
64 push @{ $owneryears{"$owner"} }, $owneryear;
65 };
66 my @owners = sort keys %owneryears;
67 @owners = () if ($merge_same_license and $owneryears_seem_correct);
68 my $pattern = join ("\n", $file->{license}, @owners);
69 push @{ $patternfiles{"$pattern"} }, $file->{name};
70 push @{ $patternownerlines{"$pattern"} }, @ownerlines_clean;
71 $patternlicense{"$pattern"} = $file->{license};
72 };
73 foreach $pattern ( sort {
74 @{$patternfiles{$b}} <=> @{$patternfiles{$a}}
75 ||
76 $a cmp $b
77 } keys %patternfiles ) {
78 my $prev;
79 @ownerlines_unique = grep((!defined $prev || $_ ne $prev) && (($prev) = $_), sort @{ $patternownerlines{$pattern} });
80 print "Files: ", join($whitespace_list_delimiter, sort @{ $patternfiles{$pattern} }), "\n";
81 print "Copyright: ", join($rfc822_list_delimiter, @ownerlines_unique), "\n";
82 print "License: $patternlicense{$pattern}\n FIXME\n\n";
83 };
