#!/usr/local/bin/perl -w
#
# Copyright (C) 2004-2007, 2009, 2010  Internet Systems Consortium, Inc. ("ISC")
# Copyright (C) 1998-2001, 2003  Internet Software Consortium.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

# $Id: merge_copyrights,v 1.38 2010/12/08 23:47:25 marka Exp $

%file_types = ();
%file_years = ();

open(COPYRIGHTS, "<util/copyrights") || die "can't open ./util/copyrights: $!";
while (<COPYRIGHTS>) {
    chomp;
    ($file, $type, $years) = split;
    $file_types{$file} = $type;
    $file_years{$file} = $years;
}
close(COPYRIGHTS);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
$year += 1900;

$find = "find . -type f -print";

open(FILES, "$find | sort |") || die "can't start \"$find\": $!";
while (<FILES>) {
    chomp;

    next if (m%/\.\# |		# CVS old conflict file
               /CVS/ |		# CVS directory
               util/newcopyrights | # our output
               \.bak$ |		# created by update_copyrights
               /(dnssafe|openssl)/.*\.[ch]$ |	# imported
               doc/(draft|expired|rfc)/		# imported
             %x);

    if (!$file_types{$_}) {
	# Strip any .in extension to find out the file's real type.
	# .in files are processed by configure to produce the target file.
	($base = $_) =~ s/\.in$//;

	# Contributed code should maintain its own copyright.
	if ($base =~ /\.\/contrib\//) {
	     $file_types{$_} = "X";
	} elsif ($base =~ /\/openssl-[a-z0-9.]*-patch$/) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\.(c|h|css)$/) {
	    $file_types{$_} = "C";
        } elsif ($base =~ /\.y$/) {
	    $file_types{$_} = "YACC";
        } elsif ($base =~ /\.pl$/i) {
	    $file_types{$_} = "PERL";
        } elsif ($base =~ /\.sh$/) {
	    $file_types{$_} = "SH";
        } elsif ($base =~ /\.docbook$/ ||
		 $base =~ /.xsl$/ ||
		 $base =~ /.xml$/) {
	    $file_types{$_} = "SGML";
        } elsif ($base =~ /\.html$/) {
	    $file_types{$_} = "HTML";
        } elsif ($base =~ /\.(man|[0-9])$/) {
	    $file_types{$_} = "MAN";
	} elsif ($base =~ /\/Makefile$/) {
	    $file_types{$_} = "MAKE";
	} elsif ($base =~ /\/(named|rndc|good|bad).{0,2}\.conf$/) {
	    $file_types{$_} = "CONF-C";
	} elsif ($base =~ /\/resolv.?\.conf$/) {
	    $file_types{$_} = "CONF-SH";
	} elsif ($base =~ /\.(db|hint)$/) {
	    $file_types{$_} = "ZONE";
	} elsif ($base =~ /(\/\.cvsignore|\.gif|\.jpg|\.dsp|\.dsw|\.mak)$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\.rnc$/i) {
	    $file_types{$_} = "RNC";
	} elsif ($base =~ /^\.\/EXCLUDED$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\.bat$/i) {
	    $file_types{$_} = "BAT";
	} else {
	    $file_types{$_} = "?";
	}
        ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
	 $blksize,$blocks)
	    = stat($_);
	($sec,$min,$hour,$mday,$mon,$c_year,$wday,$yday,$isdst) =
	    localtime($ctime);
	($sec,$min,$hour,$mday,$mon,$m_year,$wday,$yday,$isdst) =
	    localtime($mtime);
	$c_year += 1900;
	$m_year += 1900;
	if ($m_year != $year || $c_year != $year) {
	    print "$_: must set copyright year(s) manually\n";
	    $file_years{$_} = "????";
	} else {
	    $file_years{$_} = "$year";
	}
	# keep perl from issuing warnings about "used only once"
	$dev = $ino = $mode = $nlink = $uid = $gid = $rdev = $size = 0;
	$atime = $blksize = $blocks = 0;
    } else {
        if (! defined($file_years{$_}) || $file_years{$_} eq "????") {
            print "$_: must set copyright year(s) manually\n";
	    $file_years{$_} = "????";
            next;
        }

	# track the modification years even if we are not going to be
	# updating the copyrights.
	# next if $file_types{$_} eq "X";
	next if ($file_years{$_} =~ /^PARENT:/);
	next if ($file_years{$_} eq "DOCBOOK");

	@years = split(/,/, $file_years{$_});
	$has_current = 0;
	foreach $fyear (@years) {
	    if ($fyear == $year) {
		$has_current = 1;
	    }
	}
	if (!$has_current) {
	    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
	     $blksize,$blocks)
		= stat($_);
	    ($sec,$min,$hour,$mday,$mon,$m_year,$wday,$yday,$isdst) =
		localtime($mtime);
	    $m_year += 1900;
	    if ($m_year == $year) {
		$file_years{$_} .= ",$year";
	    }
	}
    }
}
close(FILES);

open(NEWCOPYRIGHTS, ">util/newcopyrights") ||
    die "can't open newcopyrights: $!";
foreach $file (sort(keys(%file_types))) {
    print NEWCOPYRIGHTS "$file";
    $len = length($file);
    if ($len >= 48) {
	$tabs = 1;
    } else {
	$needed = int (48 - $len);
	$tabs = int ($needed / 8);
	if ($needed % 8 != 0) {
	    $tabs++;
	}
    }
    for ($i = 0; $i < $tabs; $i++) {
	printf NEWCOPYRIGHTS "\t";
    }
    printf NEWCOPYRIGHTS "%s\t%s\n", $file_types{$file}, $file_years{$file};
}
close(NEWCOPYRIGHTS);
