]>
Commit | Line | Data |
---|---|---|
269e222e | 1 | #!/usr/bin/perl -0777 -pi |
0b61a8ec JD |
2 | |
3 | # Update b4_copyright invocations or b4_copyright_years definitions to | |
4 | # include the current year. | |
269e222e | 5 | |
7d424de1 PE |
6 | # Copyright (C) 2009, 2010 Free Software Foundation, Inc. |
7 | ||
269e222e JD |
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 | use strict; | |
22 | use warnings; | |
23 | ||
0b61a8ec JD |
24 | my $margin = 72; |
25 | ||
269e222e | 26 | my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR}; |
0b61a8ec | 27 | if (!$this_year || $this_year !~ m/^\d{4}$/) |
269e222e JD |
28 | { |
29 | my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ()); | |
30 | $this_year = $year + 1900; | |
31 | } | |
269e222e JD |
32 | my $old_re = <<'EOF' |
33 | ( | |
34 | (?:^|\n) | |
0b61a8ec | 35 | #BEFORE |
269e222e | 36 | (?: |
0b61a8ec | 37 | b4_copyright\(\[[^][]*] |
269e222e JD |
38 | | m4_(?:push|pop)def\(\[b4_copyright_years] |
39 | ) | |
0b61a8ec | 40 | #AFTER |
269e222e JD |
41 | ) |
42 | (?: | |
43 | ,\s* | |
44 | ( | |
585935e8 | 45 | \[\s* (?:\d{4}(?:,\s*|-))* (\d{4}) \s*] |
269e222e JD |
46 | ) |
47 | )? | |
48 | \) | |
49 | EOF | |
50 | ; | |
51 | ||
0b61a8ec | 52 | while (/($old_re)/gx) |
269e222e | 53 | { |
0b61a8ec JD |
54 | my $start = pos() - length ($1); |
55 | my $b4_copyright_line = $2; | |
56 | my $year_lines = $3; | |
585935e8 | 57 | my $final_year = $4; |
269e222e JD |
58 | $year_lines .= ')'; |
59 | ||
269e222e JD |
60 | # If there was a second argument, it contains years, so update them. |
61 | if ($final_year) | |
62 | { | |
63 | $b4_copyright_line .= ','; | |
64 | if ($final_year != $this_year) | |
65 | { | |
66 | # Update the year. | |
585935e8 | 67 | $year_lines =~ s/$final_year/$final_year, $this_year/; |
269e222e JD |
68 | } |
69 | ||
0b61a8ec JD |
70 | # Normalize all whitespace. |
71 | $year_lines =~ s/\s+/ /g; | |
269e222e | 72 | |
0b61a8ec JD |
73 | # Put spaces after commas. |
74 | $year_lines =~ s/, ?/, /g; | |
269e222e | 75 | |
7d424de1 PE |
76 | # Do not compress to intervals; for example, do not replace |
77 | # "2008, 2009, 2010" with "2008-2010". See the Copyright | |
78 | # Notices section in Information for Maintainers of GNU Software, at: | |
79 | # http://www.gnu.org/prep/maintain/maintain.html#Copyright-Notices | |
585935e8 | 80 | |
0b61a8ec JD |
81 | # Format within margin. |
82 | my $year_lines_new; | |
83 | my $indent = index ($b4_copyright_line, '['); | |
84 | --$indent if ($b4_copyright_line =~ m/^\n/); | |
85 | while (length $year_lines) | |
86 | { | |
87 | my $text_margin = $margin - $indent; | |
88 | if (($year_lines =~ s/^(.{1,$text_margin})(?: |$)//) | |
89 | || ($year_lines =~ s/^([\S]+)(?: |$)//)) | |
90 | { | |
91 | my $line = "\n" . (' 'x$indent) . $1; | |
92 | ++$indent if (!$year_lines_new); | |
93 | $year_lines_new .= $line; | |
94 | } | |
95 | else | |
96 | { | |
97 | # Should be unreachable, but we don't want an infinite | |
98 | # loop if it can be reached. | |
99 | die; | |
100 | } | |
101 | } | |
102 | ||
103 | # Replace the old invocation. Should never die. | |
104 | die if (!s/$old_re\G/$b4_copyright_line$year_lines_new/x); | |
269e222e | 105 | |
0b61a8ec JD |
106 | # Prepare for the next search. |
107 | pos () = $start + length ("$b4_copyright_line$year_lines_new"); | |
108 | } | |
269e222e JD |
109 | } |
110 | ||
0b61a8ec | 111 | while (/(\bb4_copyright\()/g) |
269e222e | 112 | { |
a1a9422d JD |
113 | my $start = pos () - length ($1); |
114 | my $end = pos (); | |
0b61a8ec | 115 | my $re = $old_re; |
a1a9422d | 116 | pos () = $start; |
0b61a8ec JD |
117 | $re =~ s/\#BEFORE/\\G/; |
118 | if (!/$re/x) | |
119 | { | |
a1a9422d | 120 | my $line = (substr ($_, 0, $start) =~ s/\n/\n/g) + 1; |
0b61a8ec | 121 | print STDERR |
a1a9422d | 122 | "$ARGV:$line: warning: failed to update a b4_copyright\n"; |
0b61a8ec | 123 | } |
a1a9422d | 124 | pos () = $end; |
269e222e | 125 | } |
0b61a8ec | 126 | |
a1a9422d | 127 | while (/(\[b4_copyright_years])/g) |
269e222e | 128 | { |
a1a9422d JD |
129 | my $start = pos () - length ($1); |
130 | my $end = pos (); | |
0b61a8ec JD |
131 | my $re = $old_re; |
132 | $re =~ s/\#AFTER/\\G/; | |
133 | if (!/$re/x) | |
134 | { | |
a1a9422d JD |
135 | # The substr operation blows away pos (), so restoring pos () |
136 | # at the end is necessary. | |
137 | my $line = (substr ($_, 0, $start) =~ s/\n/\n/g) + 1; | |
0b61a8ec | 138 | print STDERR |
a1a9422d JD |
139 | "$ARGV:$line: warning: failed to update a" |
140 | . " b4_copyright_years\n"; | |
0b61a8ec | 141 | } |
a1a9422d | 142 | pos () = $end; |
269e222e | 143 | } |