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 2015-2018 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 2000,  2001, 2004-2007 by    */
32#   /* foobar                                 */
33#
34# and replaces them uniformly with
35#
36#   Copyright 2000-2015
37#   foobar
38#
39# and
40#
41#   /* Copyright 2000-2015 by                 */
42#   /* foobar                                 */
43#
44# (assuming that the current year is 2015).  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        (?<first>[12][0-9][0-9][0-9])
72        (?<middle>.+)
73        (?<last>[12][0-9][0-9][0-9])
74        (?<space2>\ +)
75        by
76        (?<space3>\ *)
77        (?<end>.*)
78      }
79      {
80        # Fill line to the same length (if appropriate); we skip the middle
81        # part but insert two spaces and `-'.
82        my $space = length($+{space1}) - 1
83                    + length($+{middle}) - 1
84                    + length($+{space2}) - 1
85                    + length($+{space3});
86
87        print "$+{begin}";
88        print "Copyright\ $+{first}-$year\ by";
89        print ' ' x $space if length($+{end});
90        print "$+{end}\n";
91        $replaced = 1;
92      }ex
93    ||
94    # Second try: Search a single copyright year.
95    s {
96        (?<begin>.*)
97        Copyright
98        (?<space1>\ +)
99        (?<first>[12][0-9][0-9][0-9])
100        (?<space2>\ +)
101        by
102        (?<space3>\ *)
103        (?<end>.*)
104      }
105      {
106        # Fill line to the same length (if appropriate); we insert two
107        # spaces, a `-', and the current year.
108        my $space = length($+{space1}) - 1
109                    + length($+{space2}) - 1
110                    + length($+{space3})
111                    - (length($year) + 1);
112
113        print "$+{begin}";
114        print "Copyright $+{first}-$year by";
115        # If $space is negative this inserts nothing.
116        print ' ' x $space if length($+{end});
117        print "$+{end}\n";
118        $replaced = 1;
119      }ex
120    ||
121    # Otherwise print line unaltered.
122    print;
123  }
124  else
125  {
126    print;
127  }
128}
129continue
130{
131  # Reset $replaced before processing the next file.
132  $replaced = 0 if eof;
133}
134
135# EOF
136