#!/usr/bin/perl -w
#
# License checker for source files

# This should kept in sync with kde-common/svn/hooks/post-commit.pl
sub checklicense($)
{
   my ($text) = @_;

   $text =~ y/\t\n\r/   /;
   $text =~ y/ A-Za-z.,@1-9\(\)//cd;
   $text =~ s/\s+/ /g;

   my ($gl, $qte, $license, $massave);

   $gl = "";
   $qte = "";
   $license = "";
   $massave = "";

   $gl = " (v2)" if ($text =~ /version 2 as published by the Free Software Foundation/);
   $gl = " (v2+)" if ($text =~ /either version 2 of the License, or .at your option. any later version/);
   $gl = " (v2.1)" if ($text =~ /version 2\.1 as published by the Free Software Foundation/);
   $gl = " (v2.1+)" if ($text =~ /either version 2\.1 of the License, or .at your option. any later version/);
   $qte = " (+Qt exception)" if ($text =~ /([Pp]ermission is given|[pP]ermission is also granted|[pP]ermission) to link (the code of )?this program with (any edition of )?(Qt|the Qt library)/);
   $massave = " (wrong address)" if ($text =~ /(?:675 Mass Ave|59 Temple Place|51 Franklin Steet|02139|02110-1301)/i); # "51 Franklin Street, 02110-1301" is the right FSF address

   $license="GENERATED FILE" if ($text =~ /(All changes made in this file will be lost|DO NOT EDIT|DO NOT delete this file|[Gg]enerated by)/);
   $license="LGPL$gl$massave $license" if ($text =~ /is free software.? you can redistribute it and.?or modify it under the terms of the GNU (Library|Lesser) General Public License/);
   $license="GPL$gl$qte$massave $license" if ($text =~ /is free software.? you can redistribute it and.?or modify it under the terms of the GNU General Public License/);
   ### FIXME if the license matches the next regexp, it will probably match the following one too.
   $license="QPL (part of Qt) $license" if ($text =~ /This file is part of the .*Qt GUI Toolkit. This file may be distributed under the terms of the Q Public License as defined/);
   $license="QPL $license" if ($text =~ /may be distributed under the terms of the Q Public License as defined by Trolltech AS/);
   $license="X11 (BSD like) $license" if($text =~ /Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files .the Software., to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and.?or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED AS IS/);
   $license="BSD $license" if ($text =~ /THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE/);
   $license="MPL 1.1 $license" if ($text =~ /subject to the Mozilla Public License Version 1.1/);
   $license="no copyright $license" if ($text !~ /copyright/i);
   $license="UNKNOWN" if(!length($license));

   $license =~ s/ $//;

   return "$license";
}

my $verbose = 0;
foreach my $arg (@ARGV) {
    if ($arg eq "-v") {
	$verbose = 1;
    } else {
	# For each file
	if ($verbose) {
	    print "----- header -----\n";
	    print `head -n 30 $arg| sed 's/	/ /g;s/\$/ /g;s#//##g' | tr -d -c ' A-Za-z.,/\@1-9()'`."\n";
	    print "--- end header ---\n";
	}
	open(F, "<$arg") || die "Couldn't open $arg";
	my @c = <F>;
	my $htxt = join '', @c[0.. ($#c > 29 ? 29 : $#c)];
	my $license = &checklicense($htxt);
	close(F);
	print "$arg: $license\n";
    }
}
