add non-functional c1 Hatchery
[openc2e.git] / tools / newcopyright.pl
blob371e4a2aaa2aa95869b9e84e3ad7facb5e1f5596
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
5 # Note: This is for C/C++ files only.
7 sub min {
8 my $n = shift @_;
9 for (@_) {
10 if ($_ < $n) {
11 $n = $_;
14 return $n;
17 sub max {
18 my $n = shift @_;
19 for (@_) {
20 if ($_ > $n) {
21 $n = $_;
24 return $n;
27 open AUTHORS, "<AUTHORS" or die "Can't open AUTHORS file: $!";
28 my @AUTHORS = <AUTHORS>;
29 close AUTHORS;
31 my %authors;
33 for (@AUTHORS) {
34 if (m{^([^-].*) <([^>]+)> \((.*)\)$}) {
35 $authors{$3} = $1;
39 for my $file (@ARGV) {
40 sleep 2;
41 print "Processing $file...\n";
42 my @log = `svn log -q "$file"`;
43 @log = grep { /^r/ } @log;
44 if (!@log) {
45 print STDERR "No svn log information found for $file! Skipping...\n";
46 next;
49 my %fauthors;
51 my ($orig_author, $orig_date);
53 for my $entry (@log) {
54 # print $entry;
55 unless ($entry =~ /^r\d+ \| (.+) \| (\d{4}).* \((.*)\)/) {
56 printf STDERR "Malformed svn output: $entry\n";
57 exit 1;
59 my ($author, $year) = ($1, $2);
62 my $aname = $authors{$author};
63 if (!defined $aname) {
64 next;
65 #print STDERR "Author $author not found! Add to AUTHORS.\n";
66 #exit 1;
69 $orig_author = $aname;
70 $orig_date = $3;
71 $orig_date =~ s/,//;
73 $author = $aname;
75 if (!exists $fauthors{$author}) {
76 $fauthors{$author} = [$year, $year];
77 } else {
78 my ($syear, $eyear) = @{$fauthors{$author}};
79 $fauthors{$author} = [min($year, $syear), max($year, $eyear)];
83 if (!defined $orig_author) {
84 print STDERR "No recognized authorship data found for $file.\n";
85 next;
88 open FIN, "<$file" or die "Can't open file $file";
89 my @lines = <FIN>;
90 close FIN;
92 my @clines;
93 for my $author (sort keys %fauthors) {
94 my ($syear, $eyear) = @{$fauthors{$author}};
95 my $df = "$syear-$eyear";
96 if ($syear == $eyear) {
97 $df = $syear;
100 my $cline = " * Copyright (c) $df $author. All rights reserved.\n";
101 push @clines, $cline;
103 @clines = (
104 <<END,
106 * $file
107 * openc2e
109 * Created by $orig_author on $orig_date.
111 @clines,
112 <<END,
114 * This library is free software; you can redistribute it and/or
115 * modify it under the terms of the GNU Lesser General Public
116 * License as published by the Free Software Foundation; either
117 * version 2 of the License, or (at your option) any later version.
119 * This library is distributed in the hope that it will be useful,
120 * but WITHOUT ANY WARRANTY; without even the implied warranty of
121 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
122 * Lesser General Public License for more details.
128 splice @lines, 0, 0, @clines;
129 rename $file, "$file.bak" or die "Can't rename $file to $file.bak: $!";
130 open FILE, ">$file" or die "Can't open $file for writing: $!";
131 print FILE @lines or die "Can't write to $file: $!";
132 close FILE;