Skip blank lines if old copyright comment was removed, and only then
[openssl.git] / util / copyright.pl
1 #! /usr/bin/env perl
2 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 # Add new copyright and delete old ones.  Used as
10 #   find . -name '*.[ch]' -type f -exec perl -i.bak util/copyright.pl '{}' ';'
11 # This does not do everything that's needed for the consolidation.
12
13 use strict;
14 use warnings;
15
16 # Read a multi-line comments.  If it matches a "fingerprint" of a legacy
17 # copyright block, then just delete it.
18 sub check_comment()
19 {
20     my @lines = ( @_ );
21     my $skipit = 0;
22
23     if ($lines[$#lines] !~ m@\*/@) {
24         while ( <> ) {
25             push @lines, $_;
26             last if m@\*/@;
27             $skipit = 1 if /Copyright remains Eric Young's/i;
28             $skipit = 1 if /Copyright.*The OpenSSL Project/i;
29             $skipit = 1 if /Written by.*for the OpenSSL Project/i;
30         }
31     }
32
33     print @lines unless $skipit;
34     return $skipit;
35 }
36
37 # Look for leading copyright blocks and process (print/swallow) them.
38 while ( <> ) {
39     if ($. == 1) {
40         my $DATE;
41         # Look for special copyright EAY line at line one.
42         if ( /Copyright.*(199.)-.*Eric Young/ ) {
43             $DATE = $1;
44         } else {
45             # Nope, use when it first existed in git.
46             $DATE=`git log '--pretty=format:%cI' $ARGV | tail -1`;
47             $DATE =~ s/-.*//;
48         }
49         my $YEAR = $DATE ? $DATE : 1995;
50         my $SPAN = $YEAR == 2016 ? "2016" : "${YEAR}-2016";
51         print <<EOF;
52 /*
53  * Copyright ${SPAN} The OpenSSL Project Authors. All Rights Reserved.
54  *
55  * Licensed under the OpenSSL license (the "License").  You may not use
56  * this file except in compliance with the License.  You can obtain a copy
57  * in the file LICENSE in the source distribution or at
58  * https://www.openssl.org/source/license.html
59  */
60
61 EOF
62     }
63     next if m@^$@;
64     last if not m@/\*@;
65     last unless &check_comment($_);
66 }
67
68 if (defined($_)) {
69     print unless m@\*/@;
70
71     # Print rest of file.
72     print while ( <> );
73 }