#!/usr/bin/env perl

%licenses = 
    ( 
      "BSD (with advertise clause, not GPL conform)" => 
      [ 
	'3. All advertising materials mentioning features or use of this software must display the following acknowledgement:',
	'(3) all advertising materials mentioning features or use of this software display the following acknowledgement:',
      ],

      "BSD (no advertise clause)" => 
      [ 
	'All rights reserved. Redistribution and use in source and binary forms are permitted provided that: (1) source distributions retain this entire copyright notice and comment, and (2) distributions including binaries display the following acknowledgement: ``This product includes software developed by the University of California, Berkeley and its contributors\'\' in the documentation or other materials provided with the distribution and in all advertising materials mentioning features or use of this software. Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS\'\' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.',
	'All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the Author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.',
	'Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.',
	'Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS',
      ],

      "GPL (no version mentioned)" => 
      [
       'This software may be freely redistributed under the terms of the GNU public license.',
       '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,',
       'Can be freely distributed and used under the terms of the GNU GPL.',

      ],

      "GPLv2" =>
      [
       '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 version 2 of the License.',
       'is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.',
       'This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU General Public License v.2.',
       'is free software; you may redistribute and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; Version 2 with the clarifications and exceptions',
       'is free software; you may redistribute and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; Version 2.',
       'GNU Public License 2 applies',
       'Licensed under the terms of the GNU GPL License version 2.',
      ],

      "GPLv2+" => 
      [ 
	'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.',
	'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, or (at your option) any later version.',
	'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.',
      ],

      "LGPLv2+" => 
      [ 'is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.',
	'is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.',
	'is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.',
      ],

      "LGPLv2.1" => 
      [ 
	'is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software Foundation.',
      ],

       "LGPLv2.1+" => 
      [ 'is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1, or (at your option) any later version.',
	'is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.',
       'is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2.1, or (at your option) any later version.',
      ],
    );

sub collectsourcefiles {
        my @file = `find . -type f -a \\( -name \\*.c -o -name \\*.cc -o -name \\*.cpp -o -name \\*.h -o -name \\*.py -o -name \\*.y -o -name \\*.pl -o -name \\*.java \\)`;
        chomp @file;
        return @file;
}

%licenses_found = ();

sub checklicense
{
    $file = shift;
    $text = shift;
    for $l (keys %licenses) {
	@patterns = @{ $licenses{$l} };
	for $pat (@patterns) {
	    if (index($text, $pat) > -1) {
		$licenses_found{$l} = 1;
		return;
	    }
	}
    }
    print "=================== $file ======================================\n";
    print `(file -bi $file|fgrep -q text/)&&head -20 $file`;
    print "\n\n";
}

if ($#ARGV < 0) {
    @ARGV = collectsourcefiles();
}

if ($#ARGV+1 == 0) {
    print "No files to check, waiting on stdin...";
}

print "Checking ".($#ARGV+1)." files.\n";


 LINE: while (<>) {
     if ($ARGV ne $oldargv) {
	 if ($oldargv) {
	     checklicense($oldargv, $lines);
	 }
	 $oldargv = $ARGV;
	 $lines = "";
     }
     chomp;
     s/\r//g;
     s/^\s*#+//g;
     s/^\s*\/*\**\s*(.*)/$1/g;
     s/\s*[*]\s*/ /g;
     $lines .= " $_";
     $lines =~ s/\s+/ /g;
}
select(STDOUT);
if ($oldargv) {
    checklicense($oldargv, $lines);
}

print "\n";

print "************************************************************\n";
print "Found Licenses: ";
foreach $key (keys %licenses_found) {
    print "$key, ";
}
print "\n************************************************************\n";
