summaryrefslogtreecommitdiff
path: root/build-aux/update-b4-copyright
blob: c0f5a133d598afeb72a7bfc925a168cffbd1682d (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/perl -0777 -pi

# Update b4_copyright invocations or b4_copyright_years definitions to
# include the current year.

# Copyright (C) 2009-2012 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;

my $margin = 72;

my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
if (!$this_year || $this_year !~ m/^\d{4}$/)
  {
    my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
    $this_year = $year + 1900;
  }
my $old_re = <<'EOF'
  (
    (?:^|\n)
    #BEFORE
    (?:
      b4_copyright\(\[[^][]*]
      | m4_(?:push|pop)def\(\[b4_copyright_years]
    )
    #AFTER
  )
  (?:
    ,\s*
    (
      \[\s* (?:\d{4}(?:,\s*|-))* (\d{4}) \s*]
    )
  )?
  \)
EOF
  ;

while (/($old_re)/gx)
  {
    my $start = pos() - length ($1);
    my $b4_copyright_line = $2;
    my $year_lines = $3;
    my $final_year = $4;
    $year_lines .= ')';

    # If there was a second argument, it contains years, so update them.
    if ($final_year)
      {
        $b4_copyright_line .= ',';
        if ($final_year != $this_year)
          {
            # Update the year.
            $year_lines =~ s/$final_year/$final_year, $this_year/;
          }

        # Normalize all whitespace.
        $year_lines =~ s/\s+/ /g;

        # Put spaces after commas.
        $year_lines =~ s/, ?/, /g;

        # Compress to intervals.
        $year_lines =~
          s/
            (\d{4})
            (?:
              (,\ |-)
              ((??{
                if    ($2 eq '-') { '\d{4}'; }
                elsif (!$3)       { $1 + 1;  }
                else              { $3 + 1;  }
              }))
            )+
          /$1-$3/gx;

        # Format within margin.
        my $year_lines_new;
        my $indent = index ($b4_copyright_line, '[');
        --$indent if ($b4_copyright_line =~ m/^\n/);
        while (length $year_lines)
          {
            my $text_margin = $margin - $indent;
            if (($year_lines =~ s/^(.{1,$text_margin})(?: |$)//)
                || ($year_lines =~ s/^([\S]+)(?: |$)//))
              {
                my $line = "\n" . (' 'x$indent) . $1;
                ++$indent if (!$year_lines_new);
                $year_lines_new .= $line;
              }
            else
              {
                # Should be unreachable, but we don't want an infinite
                # loop if it can be reached.
                die;
              }
          }

        # Replace the old invocation.  Should never die.
        die if (!s/$old_re\G/$b4_copyright_line$year_lines_new/x);

        # Prepare for the next search.
        pos () = $start + length ("$b4_copyright_line$year_lines_new");
      }
  }

while (/(\bb4_copyright\()/g)
  {
    my $start = pos () - length ($1);
    my $end = pos ();
    my $re = $old_re;
    pos () = $start;
    $re =~ s/\#BEFORE/\\G/;
    if (!/$re/x)
      {
        my $line = (substr ($_, 0, $start) =~ s/\n/\n/g) + 1;
        print STDERR
          "$ARGV:$line: warning: failed to update a b4_copyright\n";
      }
    pos () = $end;
  }

while (/(\[b4_copyright_years])/g)
  {
    my $start = pos () - length ($1);
    my $end = pos ();
    my $re = $old_re;
    $re =~ s/\#AFTER/\\G/;
    if (!/$re/x)
      {
        # The substr operation blows away pos (), so restoring pos ()
        # at the end is necessary.
        my $line = (substr ($_, 0, $start) =~ s/\n/\n/g) + 1;
        print STDERR
          "$ARGV:$line: warning: failed to update a"
          . " b4_copyright_years\n";
      }
    pos () = $end;
  }