• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1eval '(exit $?0)' && eval 'exec perl -wS -i "$0" ${1+"$@"}'
2  & eval 'exec perl -wS -i "$0" $argv:q'
3    if 0;
4
5# Copyright (C) 2015-2022 by
6# Werner Lemberg.
7#
8# This file is part of the FreeType project, and may only be used, modified,
9# and distributed under the terms of the FreeType project license,
10# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
11# indicate that you have read the license and understand and accept it
12# fully.
13
14# [Note: This script is expected to be called by the shell, which in turn
15#  calls perl automatically.  The nifty start-up code above is based on
16#  gnulib's `update-copyright' script; it is a more portable replacement for
17#  the shebang, using the first `perl' program in the shell's path instead.]
18
19# Usage:
20#
21#   update-copyright-year file1 [file2 ...]
22
23
24# This script handles copyright entries like
25#
26#   Copyright  2000   by
27#   foobar
28#
29# or
30#
31#   /* Copyright (c) 2000,  2001, 2004-2007 by    */
32#   /* foobar                                     */
33#
34# and replaces them uniformly with
35#
36#   Copyright (C) 2000-2021
37#   foobar
38#
39# and
40#
41#   /* Copyright (C) 2000-2021 by                 */
42#   /* foobar                                     */
43#
44# (assuming that the current year is 2021).  As can be seen, the line length
45# is retained if there is non-whitespace after the word `by' on the same
46# line.
47
48use strict;
49
50
51my (undef, undef, undef,
52    undef, undef, $year,
53    undef, undef, undef) = localtime(time);
54$year += 1900;
55
56my $replaced = 0;
57
58
59# Loop over all input files; option `-i' (issued at the very beginning of
60# this script) makes perl edit them in-place.
61while (<>)
62{
63  # Only handle the first copyright notice in a file.
64  if (!$replaced)
65  {
66    # First try: Search multiple copyright years.
67    s {
68        (?<begin>.*)
69        Copyright
70        (?<space1>(\ +
71                   | \ +\(C\)\ +))
72        (?<first>[12][0-9][0-9][0-9])
73        (?<middle>.+)
74        (?<last>[12][0-9][0-9][0-9])
75        (?<space2>\ +)
76        by
77        (?<space3>\ *)
78        (?<end>.*)
79      }
80      {
81        # Fill line to the same length (if appropriate); we skip the middle
82        # part but insert `(C)', three spaces, and `-'.
83        my $space = length($+{space1})
84                    + length($+{middle})
85                    + length($+{space2})
86                    + length($+{space3})
87                    - (length("(C)") + 3 + 1);
88
89        print "$+{begin}";
90        print "Copyright\ (C)\ $+{first}-$year\ by";
91        print ' ' x $space if length($+{end});
92        print "$+{end}\n";
93        $replaced = 1;
94      }ex
95    ||
96    # Second try: Search a single copyright year.
97    s {
98        (?<begin>.*)
99        Copyright
100        (?<space1>(\ +
101                   | \ +\(C\)\ +))
102        (?<first>[12][0-9][0-9][0-9])
103        (?<space2>\ +)
104        by
105        (?<space3>\ *)
106        (?<end>.*)
107      }
108      {
109        if ($+{first} < $year)
110        {
111          # Fill line to the same length (if appropriate); we insert three
112          # spaces, the string `(C)', a `-', and the current year.
113          my $space = length($+{space1})
114                      + length($+{space2})
115                      + length($+{space3})
116                      - (length($year) + length("(C)") + 3 + 1);
117
118          print "$+{begin}";
119          print "Copyright\ (C)\ $+{first}-$year\ by";
120          # If $space is negative this inserts nothing.
121          print ' ' x $space if length($+{end});
122          print "$+{end}\n";
123          $replaced = 1;
124        }
125        else
126        {
127          # Fill line to the same length (if appropriate); we insert three
128          # spaces and the string `(C)'.
129          my $space = length($+{space1})
130                      + length($+{space2})
131                      + length($+{space3})
132                      - (length("(C)") + 3);
133
134          print "$+{begin}";
135          print "Copyright\ (C)\ $+{first}\ by";
136          # If $space is negative this inserts nothing.
137          print ' ' x $space if length($+{end});
138          print "$+{end}\n";
139          $replaced = 1;
140        }
141      }ex
142    ||
143    # Otherwise print line unaltered.
144    print;
145  }
146  else
147  {
148    print;
149  }
150}
151continue
152{
153  # Reset $replaced before processing the next file.
154  $replaced = 0 if eof;
155}
156
157# EOF
158