#!/usr/bin/env perl

%licenses = 
    ( 
      "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',
	
      ],
      "GPLv2.1+" => 
      [
       '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.',
      ],
      "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.',
      ],
      "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.',
      ],
    );

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

%licenses_found = ();
@check_files = [];

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;
	    }
	}
    }
    push(@check_files, $file);
}

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

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*\/*\**\s*(.*)/$1/g;
     s/\s*[*]\s*/ /g;
     s/^\s+//g;
     s/\s+$//g;
     s/\s+/ /g;
     $lines .= " $_";
}
select(STDOUT);
if ($oldargv) {
    checklicense($oldargv, $lines);
}

print "Check these files:\n";
foreach $key (@check_files) {
    print "***************** $key ************************************ \n";
    print `(file -bi $key|fgrep -q text/)&&head -20 $key`;
    print "\n\n";
}

print "\n";

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