1 | #!/usr/bin/env perl
|
---|
2 |
|
---|
3 | use File::Basename;
|
---|
4 | use File::Find;
|
---|
5 |
|
---|
6 | # This script will attempt to add a license header at the top of all source
|
---|
7 | # files, unless it finds a comment already at the top of the source file
|
---|
8 |
|
---|
9 | # make sure we're in the right directory
|
---|
10 | chdir(dirname($0));
|
---|
11 |
|
---|
12 | # iterate over files in the source tree
|
---|
13 | find(\&addHeader, "./src");
|
---|
14 | sub addHeader {
|
---|
15 | # check that the file is a java source file
|
---|
16 | (-f and /\.java$/s) or return;
|
---|
17 | my $file = $_;
|
---|
18 |
|
---|
19 | # open the file for reading
|
---|
20 | open my $i, '<', $file or die "Can't read file: $!";
|
---|
21 |
|
---|
22 | # find the first non-whitespace line and ensure it is not already a header
|
---|
23 | while (<$i>) {
|
---|
24 | unless (/^\s*$/) { # finds lines with only whitespace
|
---|
25 | if (/^\/\*\*/) { # finds lines with a comment header
|
---|
26 | print "Found an existing header in '$file'\n";
|
---|
27 | return;
|
---|
28 | }
|
---|
29 | print "Adding header to '$file'\n";
|
---|
30 | last;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | # open a temporary file for writing
|
---|
35 | open my $o, '>', "$file.HEADER_WORKING" or die "Can't write file: $!";
|
---|
36 |
|
---|
37 | # print the header to the new file
|
---|
38 | print $o "/**\n * $file";
|
---|
39 | print $o '
|
---|
40 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
41 | *
|
---|
42 | * This program is free software: you can redistribute it and/or modify
|
---|
43 | * it under the terms of the GNU General Public License as published by
|
---|
44 | * the Free Software Foundation, either version 3 of the License, or
|
---|
45 | * (at your option) any later version.
|
---|
46 | *
|
---|
47 | * This program is distributed in the hope that it will be useful,
|
---|
48 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
49 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
50 | * GNU General Public License for more details.
|
---|
51 | *
|
---|
52 | * You should have received a copy of the GNU General Public License
|
---|
53 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
54 | */
|
---|
55 |
|
---|
56 | ';
|
---|
57 |
|
---|
58 | # rewind the source file
|
---|
59 | seek $i, 0, 0;
|
---|
60 |
|
---|
61 | # copy the source file into the new file
|
---|
62 | while (<$i>) {
|
---|
63 | print $o $_;
|
---|
64 | }
|
---|
65 |
|
---|
66 | close $o;
|
---|
67 | close $i;
|
---|
68 |
|
---|
69 | # replace the file with the new file
|
---|
70 | rename("$file.HEADER_WORKING", $file);
|
---|
71 | }
|
---|