#!/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 valid FOSS license 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) = "license-reuse";
my($Version) = "1.00";

&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($lcnt) = 0;
my($found) = 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;
  }

  #Skip generated files
  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;
  }

  #Skip works in the public domain and other stuff
  if ($line =~ m/Public Domain/i ||
      $line =~ m/approved BSD License/i ||
      $line =~ m/(disclaims|waives) copyright/i ||
      $line =~ m/Redistribution and use is allowed/ ||
      $line =~ m/Redistribution and use in source and binary forms/) {
    $found = 1;
    last;
  }

  last if ($lcnt == $MAXLINES);

  $lcnt++;

  if ($line =~ m/SPDX-License-Identifier: / &&
      ($line =~ m/GPL/ || $line =~ m/BSD/ || $line =~ m/CISST/ || $line =~ m/MIT/)) {
    $found++;
    next;
  }

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

$tags = "";

if (! $found) {
  $tags = "REUSE FOSS license line not found";
  $cnt++;
}
if ($found > 1) {
  $tags = "More than 1 REUSE FOSS license line found";
  $cnt++;
}

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

sub Help {
  print "Check for FOSS license according to REUSE guidelines\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "Source files must contain a REUSE license line of the form \"SPDX-License-Identifer: GPL-2.0-only\"\n";
  Exit 0 if &explainArg();
}
