#!/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-2009 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
#   --priority:      report issues of the specified priority only
#   --strict:        report issues with the specified strictness level only
#   --explain:       print an explanation with solving instructions
#   --installed      file is to be installed
#   --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 FindBin qw($Bin);
use lib "$Bin/../../../../lib";
use Krazy::Utils;

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

&parseArgs();

&Help() if &helpArg();
&Version() if &versionArg();
&Explain() if &explainArg();
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+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $found = 1;
    last;
  }
  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/ ||
      $line =~ m/;$/ ) {
    $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);
  next if ($line =~ m/ copyrights/i);
  next if ($line =~ m/ AUTHORS OR COPYRIGHT/i);
  $found++;

  next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);

  #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/Apple\s*,?\s*Inc/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/Nokia/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);
  next if ($line =~ m/MP3tunes, LLC/i);
  next if ($line =~ m/4Front Technologies/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 (&verboseArg());
#    $cnt++;
#    next;
#  }

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

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

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

if (! $tags ) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  print "missing tags: $tags ($cnt)\n" if (!&quietArg());
  Exit $cnt;
}

sub Help {
  print "Check for an acceptable copyright\n";
  Exit 0 if &helpArg();
}

sub Version {
  print "$Prog, version $Version\n";
  Exit 0 if &versionArg();
}

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://techbase.kde.org/Policies/Licensing_Policy>.  A typical copyright looks like: \"Copyright 2002,2005-2006 Joe Hacker \<joe.hacker\@kde.org\>\"\n";
  Exit 0 if &explainArg();
}
