#!/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) 2022 by Allen Winter <winter@kde.org>                         #
#                                                                             #
# 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 source for an acceptable copyright according to REUSE compliance

# 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-reuse";
my($Version) = "1.01";

my($ThisYear) = (localtime)[5] + 1900;

&parseArgs();

&Help() if &helpArg();
&Version() if &versionArg();
&Explain() if &explainArg();
if ($#ARGV != 0){ &Help(); Exit 0; }

my($f) = $ARGV[0];
#if ($f =~ m+/3rdparty/+ ||
#    $f =~ m/CMakeLists\.txt$/ ||
#    $f =~ m/ConfigureChecks\.cmake$/ ||
#    $f =~ m/config-.*\.h\.cmake$/ ||
#    $f =~ m/config\.h\.cmake$/ ||
#    $f =~ m/CTest.*\.cmake$/) {
#  print "okay\n" if (!&quietArg());
#  Exit 0;
#}

open(F, "$f") || die "Couldn't open $f";
my($tags) = "";
my($ytag) = "";
my($etag) = "";
my($lcnt) = 0;
my($found) = 0;
my($spdxfound) = 0;
my($line);
my($cnt) = 0;

#max lines to search. filetype dependent
my($MAXLINES)=30;
if (&fileType($f) eq "cmake") {
  $MAXLINES=100;
}

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 == $MAXLINES);
  $lcnt++;

  if ($line =~ m/SPDX-FileCopyrightText:/) {
    $spdxfound = 1;
    last;
  }

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

# SPDX Specification (https://reuse.software/spec)
if (! $found && $spdxfound) {
  $found = 1;
  #Businesses and legal entities are allowed to not have email addresses.
  if (!&isBusinessEntity($line)) {
    #check email is specified in angle brackets
    #TODO: Note that we check the first SPDX line encountered only.  not good.
    my($foo, $em);
    ($foo, $em) = split("<", $line);
    if (!defined($em) || $em !~ m/(\@| at |\(at\)| \(at\) |#| # | \@ )[[:alnum:]]/i) {
      $tags .= "email address line\#$lcnt ";
      $tags .= "($line) " if (&verboseArg());
      $cnt++;
    }
  }
}

if (! $found) {
  $tags = "REUSE copyright not found" . $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 according to REUSE guidelines\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 reuse copyright line\n";
  Exit 0 if &explainArg();
}

sub isBusinessEntity() {
  my($line) = @_;

  return 1 if ($line =~ m/Datakonsult/i);
  return 1 if ($line =~ m/Kitware, inc/i);
  return 1 if ($line =~ m/Apple Computer/i);
  return 1 if ($line =~ m/Apple\s*,?\s*Inc/i);
  return 1 if ($line =~ m/Free Software Foundation/i);
  return 1 if ($line =~ m/(World Wide Web Consortium|W3C)/i);
  return 1 if ($line =~ m/Aladdin Enterprises/i);
  return 1 if ($line =~ m/SUSE Linux/i);
  return 1 if ($line =~ m/Open Group/i);
  return 1 if ($line =~ m/Netscape/i);
  return 1 if ($line =~ m/NLNet Labs/i);
  return 1 if ($line =~ m/Nokia/i);
  return 1 if ($line =~ m/Novell, Inc/i);
  return 1 if ($line =~ m/Tridia Corporation/i);
  return 1 if ($line =~ m/AT\&T Laboratories/i);
  return 1 if ($line =~ m/X Consortium/i);
  return 1 if ($line =~ m/heXoNet Support/i);
  return 1 if ($line =~ m/Trolltech AS/i);
  return 1 if ($line =~ m/Lucent Tech/i);
    return 1 if ($line =~ m/University of California/i);
  return 1 if ($line =~ m/Sendmail\, INC/i);
  return 1 if ($line =~ m/Sun Microsystems/i);
  return 1 if ($line =~ m/Network Computing Devices/i);
  return 1 if ($line =~ m/MP3tunes, LLC/i);
  return 1 if ($line =~ m/4Front Technologies/i);
  return 1 if ($line =~ m/Critical Path/i);
  return 1 if ($line =~ m/Ximian/i);
  return 1 if ($line =~ m/Intevation GmbH/i);
  return 1 if ($line =~ m/Bundesamt für Sicherheit in der Informationstechnik/i);
  return 1 if ($line =~ m/ScalingWeb/i);
  return 1 if ($line =~ m/Red Hat/i);
  return 1 if ($line =~ m/Digia Plc/i);
  return 1 if ($line =~ m/Canonical/i);
  return 1 if ($line =~ m/Fastmail Pty/i);

  return 0;
}
