Copyright consolidation script
[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 }
35
36 # Look for leading copyright blocks and process (print/swallow) them.
37 while ( <> ) {
38     if ($. == 1) {
39         my $DATE;
40         # Look for special copyright EAY line at line one.
41         if ( /Copyright.*(199.)-.*Eric Young/ ) {
42             $DATE = $1;
43         } else {
44             # Nope, use when it first existed in git.
45             $DATE=`git log '--pretty=format:%cI' $ARGV | tail -1`;
46             $DATE =~ s/-.*//;
47         }
48         my $YEAR = $DATE ? $DATE : 1995;
49         my $SPAN = $YEAR == 2016 ? "2016" : "${YEAR}-2016";
50         print <<EOF;
51 /*
52  * Copyright ${SPAN} The OpenSSL Project Authors. All Rights Reserved.
53  *
54  * Licensed under the OpenSSL license (the "License").  You may not use
55  * this file except in compliance with the License.  You can obtain a copy
56  * in the file LICENSE in the source distribution or at
57  * https://www.openssl.org/source/license.html
58  */
59
60 EOF
61     }
62     next if m@^$@;
63     last if not m@/\*@;
64     &check_comment($_);
65 }
66
67 if (defined($_)) {
68     print unless m@\*/@;
69
70     # Print rest of file.
71     print while ( <> );
72 }