update-copyright: generalize comment handling
[gnulib.git] / build-aux / update-copyright
1 #!/usr/bin/perl -0777 -pi
2 # Update an FSF copyright year list to include the current year.
3
4 my $VERSION = '2009-07-30.13:24'; # UTC
5
6 # Copyright (C) 2009 Free Software Foundation
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 # Written by Jim Meyering
22
23 # In the copyright statement in each file, "Copyright (C)" must appear
24 # at the beginning of the line except that it may be preceded by any
25 # sequence (e.g., a comment) of no more than 5 characters.  Iff that
26 # prefix is present, the same prefix should appear at the beginning
27 # of each remaining line within the copyright statement so that it
28 # can be parsed correctly.
29 #
30 # For example, these are fine:
31 #
32 #   # Copyright (C) 1990-2005, 2007-2009 Free Software
33 #   # Foundation, Inc.
34 #
35 #   /*
36 #    * Copyright (C) 1990-2005, 2007-2009 Free Software
37 #    * Foundation, Inc.
38 #    */
39 #
40 # The following format is not recognized:
41 #
42 #   /* Copyright (C) 1990-2005, 2007-2009 Free Software
43 #    * Foundation, Inc.  */
44 #
45 # A warning is printed for every file for which the copyright format is
46 # not recognized.  The culprit may be that the above preconditions are
47 # not obeyed as in the previous example, or it may simply be that the
48 # stated copyright holder is not the Free Software Foundation.
49 #
50 # You may wish to place a target like the following in your top-level
51 # makefile in your project:
52 #
53 #   .PHONY: update-copyright
54 #   update-copyright:
55 #       if test -d .git; then                                   \
56 #         git grep -l -w Copyright                              \
57 #           | grep -v -E '(^|/)(COPYING|ChangeLog)'             \
58 #           | xargs $(srcdir)/build-aux/$@;                     \
59 #       fi
60 #
61 # You can build a list of files to skip in the second grep.
62
63 use strict;
64 use warnings;
65
66 my ($sec, $min, $hour, $mday, $month, $year) = localtime (time());
67 my $this_year = $year + 1900;
68 my $holder = 'Free Software Foundation';
69
70 my $prefix = '';
71 if (/(?:^|\n)(.{0,5})Copyright \([cC]\)/) {
72   $prefix = quotemeta $1;
73 }
74 $holder = " $holder";
75 $holder =~ s/\s/\\s*(?:\\s|\\n$prefix)\\s*/g;
76
77 if (/([- ])((?:\d\d)?\d\d)($holder)/s)
78   {
79     my ($sep, $last_c_year, $rest) = ($1, $2, $3);
80
81     # Handle two-digit year numbers like "98" and "99".
82     $last_c_year <= 99
83       and $last_c_year += 1900;
84
85     if ($last_c_year != $this_year)
86       {
87         if ($sep eq '-' && $last_c_year + 1 == $this_year)
88           {
89             s//-$this_year$rest/;
90           }
91         elsif ($sep eq ' ' && $last_c_year + 1 == $this_year)
92           {
93             s// $last_c_year-$this_year$rest/;
94           }
95         else
96           {
97             s//$sep$last_c_year, $this_year$rest/;
98           }
99       }
100   }
101 else
102   {
103     print STDERR
104       "$ARGV: warning: external copyright holder or parse failure\n";
105   }
106
107 # Local variables:
108 # indent-tabs-mode: nil
109 # eval: (add-hook 'write-file-hooks 'time-stamp)
110 # time-stamp-start: "my $VERSION = '"
111 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
112 # time-stamp-time-zone: "UTC"
113 # time-stamp-end: "'; # UTC"
114 # End: