add_copyright_notice.pl 5.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#!/usr/bin/perl -w
#
# add_copyright_notice.pl
# Copyright (C) 2006-2007 Catalyst IT Ltd (http://www.catalyst.net.nz)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This file incorporates work covered by the following copyright and
# permission notice:
#  
#   Copyright (C) 2006 Serious Hack Inc. (http://www.serioushack.com)
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA
#

use strict;

use File::Copy;
use File::Find;
use File::Temp;

# Directories which contain files to be "licensified"
my @SRC_DIRS = ('htdocs', 'test');

# When changing the copyright notice, one must also change the appropriate
# strings in:
#    - COPYING
#    - debian/copyright
53 54
my $copyright_years = '2006-2009';
my $copyright_last_year = '2009';
55 56 57
my $copyright_text = <<END_OF_COPYRIGHT_TEXT;
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
58 59
 * Copyright (C) $copyright_years Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
END_OF_COPYRIGHT_TEXT

sub process_file
{
77 78
    my ($filename, $fullpath) = @_;
    print "Processing $File::Find::name... ";
79 80 81 82 83 84 85 86 87 88 89 90 91 92
    open INPUT, "$filename" or die "Cannot open $filename as input.";

    my $in_copyright = 0; # 0 = before the header
    my $copyright_holder = 'unknown';
    my $product = 'unknown';
    my @lines;
    my @after_license_lines;
    while (<INPUT>) {
        if (0 == $in_copyright and m|^/\*\*|) {
            $in_copyright = 1; # 1 = inside the header
        }
        elsif (1 == $in_copyright and m|^ \* This program is part of (.*)$|) {
            $product = $1;
        }
93 94 95
        elsif (1 == $in_copyright and m|^ \* Mahara:|) {
            $product = 'Mahara';
        }
96
        elsif (2 == $in_copyright and 
97
               m|^ \* \@copyright.*\([cC]\) (\d+([-,]\d+)?) Catalyst IT|) {
98 99 100 101
            $copyright_holder = 'Catalyst IT';
            push @after_license_lines, $_;
        }
        elsif (1 == $in_copyright and 
102
               m|^ \* along with this program\.  If not, see|) {
103 104 105 106 107
            $in_copyright = 2; # 2 = after the license text
        }
        elsif (m|\*/| and (1 == $in_copyright or 2 == $in_copyright)) {
            $in_copyright = 3; # 3 = after the header
            push @lines, $copyright_text;
108 109
            # Correct any catalyst IT copyright lines
            for my $line (@after_license_lines) {
110 111 112 113 114 115 116
                if ($line =~ m|^ \* \@copyright.*\([cC]\) ((\d{4})(?:[,-](\d{4}))?) Catalyst IT|) {
                    my $years = $1;
                    my $firstyear = $2;
                    my $lastyear = $3 ? $3 : $2;
                    if ($lastyear lt $copyright_last_year) {
                        $line =~ s/$years/$firstyear-$copyright_last_year/;
                    }
117 118
                }
            }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            push @lines, @after_license_lines;
            push @lines, " */\n";
        } 
        elsif (0 == $in_copyright or 3 == $in_copyright) {
            push @lines, $_;
        }
        elsif (2 == $in_copyright) {
            push @after_license_lines, $_;
        }
    }
    close INPUT;

    if ($in_copyright == 0) {
        # No header was found, insert copyright text at the front?
        #unshift @lines, $copyright_text;
    }

    die "$filename: Bad header." if $in_copyright == 1;

    if ($copyright_holder eq 'Catalyst IT') {
        if ($product eq 'Mahara') {
            open OUTPUT, ">$filename" or die "Cannot open $filename as output.";
            print OUTPUT foreach @lines;
            close OUTPUT;
            print "done.\n";
        } else {
            print "skipped (product is $product).\n";
        }
    } else {
        print "skipped (copyright holder is $copyright_holder).\n";
    }

}

sub process_standard_files
{
    # Find all php and javascript files in the standard directories
    File::Find::find sub { /^.*\.(php|js)$/s && process_file($_) }, @SRC_DIRS;
}

sub main
{
    if (@ARGV > 0) {
        while (@ARGV > 0) {
            process_file(shift @ARGV);
        }
    } else {
        &process_standard_files;
    }
}

&main;