#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
###############################################################################
# Sanity check plugin for the Krazy project.                                  #
# Copyright (C) 2005-2007 by Allen Winter <winter@kde.org>                    #
# Copyright (C) 2006 by Jaison Lee <lee.jaison@gmail.com>                     #
#                                                                             #
# 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 2 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, write to the Free Software                 #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. #
#                                                                             #
###############################################################################

# Tests KDE source for an acceptable copyright

# Program options:
#   --help:          print one-line help message and exit
#   --version:       print one-line version information and exit
#   --explain:       print an explanation with solving instructions, then exit
#   --quiet:         suppress all output messages
#   --verbose:       print the offending content

# Exits with status=0 if test condition is not present in the source;
# else exits with the number of failures encountered.

use strict;
use Getopt::Long;

my($Prog) = "copyright";
my($Version) = "1.2";

my($help) = '';
my($version) = '';
my($explain) = '';
my($quiet) = '';
my($verbose) = '';

exit 1
if (!GetOptions('help' => \$help, 'version' => \$version,
		'explain' => \$explain,
		'verbose' => \$verbose, 'quiet' => \$quiet));

&Help() if $help;
&Version() if $version;
&Explain() if $explain;
if ($#ARGV != 0){ &Help(); exit 0; }

my($f) = $ARGV[0];
open(F, "$f") || die "Couldn't open $f";
my($tags) = "";
my($lcnt) = 0;
my($found) = 0;
my($line);
my($cnt) = 0;
while ($line = <F>) {
  if ($line =~ m/generated from/ ||
      $line =~ m/generated by/ ||
      $line =~ m/uic-generated/ ||
      $line =~ m/changes made in this file will be lost/ ||
      $line =~ m/produced by gperf/ ||
      $line =~ m/This file is automatically generated/ ||
      $line =~ m/created by dcopidl2cpp/){
    $found = 1;
    last;
  }
  
  #Works in the public domain have waived copyright 
  if ($line =~ m/ Public Domain/i ||
      $line =~ m/(disclaims|waives) copyright/i ) {
    $found = 1;
    last;
  }
  
  last if ($lcnt == 30);
  
  $lcnt++;
  next if ($line !~ m/copyright/i);
  next if ($line =~ m/[[:alnum:]]copyright/i);
  next if ($line =~ m/ copyrighted/i);
  next if ($line =~ m/ copyright holder/i);
  next if ($line =~ m/ above copyright/i);
  next if ($line =~ m/ copyright notice/i);
  next if ($line =~ m/ infringement of copyright/i);
  next if ($line =~ m/ disclaims copyright/i);
  next if ($line =~ m/ the copyright/i);
  $found++;

  #Businesses and legal entities are allowed to not have email addresses.
  next if ($line =~ m/Datakonsult/i);
  next if ($line =~ m/Apple Computer/i);
  next if ($line =~ m/Free Software Foundation/i);
  next if ($line =~ m/(World Wide Web Consortium|W3C)/i);
  next if ($line =~ m/Aladdin Enterprises/i);
  next if ($line =~ m/SUSE Linux/i);
  next if ($line =~ m/Open Group/i);
  next if ($line =~ m/Netscape/i);
  next if ($line =~ m/Tridia Corporation/i);
  next if ($line =~ m/AT\&T Laboratories/i);
  next if ($line =~ m/X Consortium/i);
  next if ($line =~ m/heXoNet Support/i);
  next if ($line =~ m/Trolltech AS/i);
  next if ($line =~ m/Lucent Tech/i);
  next if ($line =~ m/University of California/i);
  next if ($line =~ m/Sendmail\, INC/i);
  next if ($line =~ m/Sun Microsystems/i);
  next if ($line =~ m/Network Computing Devices/i);

  #Non-legal entities are not allowed to hold copyright. Don't use things like
  #'The KDE Developers' or 'The KDE Authors'
# Enable this check once we determine a legally acceptable means of specifying this.
#  if ( $line =~ m/the /i ) {
#    $tags .= "non-legal group(line $lcnt) ";
#    $tags .= "($line) " if ($verbose);
#    $cnt++;
#    next;
#  }

  if ($line !~ m/copyright[[:space:]].+(\@| at )[[:alnum:]]/i ) {
    #Always check the next line in case the email address is there.
    $line = <F>;
    $lcnt++;
    if ($line !~ m/[[:print:]]+(\@| at )[[:alnum:]]/i ||
      $line =~ m/copyright/i) {

      #This line didn't yield anything useful, so we need to check it again.
      seek(F,-(length($line)),1);
      $lcnt--;

      $tags .= "email address(line $lcnt) ";
      $tags .= "($line) " if ($verbose);
      $cnt++;
    }
  }
}
close(F);

#ignore small files.
if ( !$found && $lcnt <= 5 ) {
  $found = 1;
}

if (! $found) {
  $tags = "copyright " . $tags;
  $cnt++;
}

if (! $tags ) {
  print "okay\n" if (!$quiet);
  exit 0;
} else {
  print "missing tags: $tags ($cnt)\n" if (!$quiet);
  exit $cnt;
}

sub Help {
  print "Check for an acceptable copyright\n";
  exit 0 if $help;
}

sub Version {
  print "$Prog, version $Version\n";
  exit 0 if $version;
}

sub Explain {
  print "All source files must contain a copyright header which identifies the copyright holder(s) together with a e-mail address that can be used to reach the copyright holder.  One copyright holder per line, with real email addresses please. For details regarding KDE's licensing policy please visit <http://developer.kde.org/policies/licensepolicy.html>.  A typical copyright looks like: \"Copyright 2002,2005-2006 Joe Hacker <joe.hacker\@kde.org>\"\n";
  exit 0 if $explain;
}
