aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/update-copyright
blob: ad5eb231034082959f04eac6b3b23d85ad22d2f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl
use strict;
use File::stat;
sub update_maybe($%);

my $year =  (split ' ', ~~localtime)[4];
my %dates = ();
my %files = ();
my $cvs;
open $cvs, '-|', '/usr/bin/cvs', 'update', @ARGV or die "cvs update failed - $!\n";
while (<$cvs>) {
    /^M (.*)$/o and $files{$1}{$year} = 1;
}
close $cvs;

open $cvs, '-|', '/usr/bin/cvs', 'log', '-N', '-b', @ARGV or die "cvs log failed - $!\n";
my $file;
while (<$cvs>) {
    if (/^Working file: (.*)$/o) {
	$file = $1;
    } elsif (/^date: (\d+)/o) {
	$files{$file}{$1} = 1;
    } elsif (/^=+$/o) {
	my $rec = delete $files{$file};
	update_maybe($file, %{$rec}) if -e $file;
    }
}
close $cvs;

exit 0;

sub addwrap($$) {
    my $indent = shift;
    my $copyright = shift;
    $copyright =~ s/Red Hat\n/Red Hat, Inc.\n/so;
    return $copyright if length($copyright) <= 80;
    my @lines;
    while (length($copyright) > 80) {
	my $i = index($copyright, ' ', 80 - 6);
	push @lines, substr($copyright, 0, $i) . "\n";
	substr($copyright, 0, $i + 1) = $indent;
    }
    push @lines, $copyright unless $copyright =~ /^\s*$/o;
    return join('', @lines);
}

sub update_maybe($%) {
    my $f = shift;
    local @ARGV = $f;
    my %dates = @_;
    my @file = ();
    my $copyright = '';
    my $modified = 0;
    while (<>) {
	if ($copyright) {
	    push @file, $_;
	} elsif (/^(?:dnl\s|[#\s]*)Copyright/o) {
	    $copyright = $_;
	    $copyright .= scalar <> while $copyright =~ /,\s*$/o;
	    if ($copyright !~ /Red Hat, Inc\.\n/o) {
		push @file, $copyright;
		next;
	    }
	    for my $date ($copyright =~ /(\d+)/g) {
		$dates{$date} = 1;
	    }
	    my $indent = ($copyright =~ /\A(dnl\s+|[#\s]*)/o)[0];
	    my $newcopyright = addwrap $indent,
				       $indent . 'Copyright ' .
				       (join ', ', sort {$a <=> $b} sort keys %dates) .
				       " Red Hat, Inc.\n";
	    push @file, $newcopyright;
	    $modified = $newcopyright ne $copyright;
	} else {
	    push @file, $_;
	}
    }
    if ($modified) {
	print "updating $f\n";
	my $fcopy = "$f.copyright";
	rename $f, $fcopy or die "$0: couldn't rename $f -> $fcopy - $!\n";
	my $st = stat($fcopy);
	open my $fd, '>', $f;
	chmod $st->mode & 07777, $f;
	print $fd @file;
	close $fd;
    }
}