#! /usr/bin/perl

use strict;
use warnings;

# This gets fed in as a regular expression...
my $cpyheader = '\$LicenseInfo:firstyear=\d{4}&license=(.*)\$';

my %cpystatement = (
'viewergpl' => [
	'',
	'Copyright \(c\) (20\d{2}-)?2007, Linden Research, Inc\.',
	'',
	'Second Life Viewer Source Code',
	'The source code in this file \("Source Code"\) is provided by Linden Lab',
	'to you under the terms of the GNU General Public License, version 2.0',
	'\("GPL"\), unless you have obtained a separate licensing agreement',
	'\("Other License"\), formally executed by you and Linden Lab.  Terms of',
	'the GPL can be found in doc/GPL-license.txt in this distribution, or',
	'online at http://secondlife.com/developers/opensource/gplv2',
	'',
	'There are special exceptions to the terms and conditions of the GPL as',
	'it is applied to this Source Code. View the full text of the exception',
	'in the file doc/FLOSS-exception.txt in this software distribution, or',
	'online at http://secondlife.com/developers/opensource/flossexception',
	'',
	'By copying, modifying or distributing this software, you acknowledge',
	'that you have read and understood your obligations described above,',
	'and agree to abide by those obligations.',
	'',
	'ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO',
	'WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,',
	'COMPLETENESS OR PERFORMANCE.',
	'\$/LicenseInfo\$',
	],
'mit' => [
	'',
	'Copyright \(c\) (20\d{2}-)?2007, Linden Research, Inc\.',
	'',
	'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", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR',
	'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,',
	'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE',
	'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER',
	'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,',
	'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN',
	'THE SOFTWARE.',
	'\$/LicenseInfo\$',
	],
);

my $headerstart = -1;
my $fileskip = 0;
my $dosending = 0;
my $license = undef;

while ( <> )
{
	# We have our answers for this file
	next if $fileskip and !eof;

	# Trim off newlines
	chomp;
	if ( substr( $_, -1) eq '' ) {
		$dosending = 1 if $headerstart >= 0;
		chop;
	}

	if( defined( $license ) ) {
		# Check for the next line of the header
		if ( $headerstart >= 0 and $_ =~ /${cpystatement{ $license }->[ $headerstart ] }$/ ) {
			$headerstart++;
			if ( $headerstart == scalar( @{$cpystatement{ $license }} ) - 1 ) {
				$fileskip = 1;
			}
		}
	}

	# Report on this file
	if ( eof ) {
		print "$ARGV has DOS line-endings in the license header\n" if $dosending;
		if ( not $fileskip ) {
			if ( $headerstart > 0 ) {
				print "$ARGV: ($cpystatement{ $license }[ $headerstart ])\n";
			} else {
				print "$ARGV\n";
			}
		}
		$headerstart = -1;
		$fileskip = 0;
		$dosending = 0;
		$license = undef;
	}

	# Check for the copyright notice
	my $basename = $ARGV;

	$basename =~ s#.*/([^/]+)#$1#;

	if ( m/ * \@file +(.*)/ and $1 ne $basename ) {
		print "$ARGV claims to be $1\n";
	}

	if ( m/$cpyheader/ and $headerstart == -1 ) {
		$license = $1;
		if ( defined( $cpystatement{ $license } ) ) {
			$headerstart = 0;
		} else {
			print "$ARGV is under '$license' license\n";
			$fileskip = 1;
		}
	}

}
