#!/usr/local/bin/perl -w
#
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.

use strict;

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

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

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

open(CHANGES, "git ls-files | sed 's;^;./;' | sort |") || die "git ls-files: $!";
while (<CHANGES>) {
    chomp;

    next if (m%/\.\# |		# CVS old conflict file
	       /CVS/ |		# CVS directory
	       /\.git/ |		# git directory
	       /\.gitignore |   # .gitignore files
	       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.
	my $base;
	($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 =~ /\.py$/i) {
	    $file_types{$_} = "PYTHON";
	    if (open(PYTHON, $_)) {
	      my $line = <PYTHON>;
	      $file_types{$_} = "PYTHON-BIN" if ($line && $line =~ /^#!/);
	      close(PYTHON);
	    }
	} elsif ($base =~ /\.sh$/) {
	    $file_types{$_} = "SH";
	} elsif ($base =~ /\.docbook$/ ||
		 $base =~ /.xsl$/ ||
		 $base =~ /.xml$/) {
	    $file_types{$_} = "SGML";
	} elsif ($base =~ /doc\/arm\/.*\.html$/) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\.(sty|pdf|eps)$/) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\.html$/) {
	    $file_types{$_} = "HTML";
	    $base =~ s/\.html$//;
	    $base = $base . ".docbook";
	} elsif ($base =~ /\.(man|[0-9])$/) {
	    $file_types{$_} = "MAN";
	    $base =~ s/\.[0-9]$//;
	    $base = $base . ".docbook";
	} elsif ($base =~ /\/Makefile$/) {
	    $file_types{$_} = "MAKE";
	} elsif ($base =~ /\/(named|rndc|good|bad).{0,2}\.conf$/) {
	    $file_types{$_} = "CONF-C";
	} elsif ($base =~ /\/checkconf\/(good|bad)-.*\.conf$/) {
	    $file_types{$_} = "CONF-C";
	} elsif ($base =~ /\/resolv.?\.conf$/) {
	    $file_types{$_} = "CONF-SH";
	} elsif ($base =~ /\.(db|hint)$/) {
	    $file_types{$_} = "ZONE";
	} elsif ($base =~ /\.md$/) {
	    $file_types{$_} = "MKD";
	} elsif ($base =~ /(\/\.(gitignore|gitattributes)|Kyuafile|\.(gif|jpg))$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\.(def|dep|dsp|dsw|mak|sln)$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\.(vcxproj(|\.(user|filters)))$/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";
	} elsif ($base =~ /\.(key|private)$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\/named\d{0,2}\.args$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\/named\.port$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\/named\.dropedns$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\/named\.notcp$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\/doc\/misc\/[-a-z]*\.zoneopt$/i) {
	    $file_types{$_} = "X";
	} elsif ($base =~ /\/README$/i) {
	    $file_types{$_} = "TXT.BRIEF";
	} else {
	    $file_types{$_} = "?";
	}
	if (($file_types{$_} eq "MAN" || $file_types{$_} eq "HTML") &&
	    $base =~ /\.docbook$/ && -e $base) {
	   $file_years{$_} = "DOCBOOK";
	} else {
	    $file_years{$_} = "$year";
	}
    } 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");

	my @years = split(/,/, $file_years{$_});
	my $has_current = 0;
	foreach my $fyear (@years) {
	    if ($fyear == $year) {
		$has_current = 1;
	    }
	}
	if (!$has_current) {
	    $file_years{$_} .= ",$year";
	}
    }
}
close(CHANGES);

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

    if (($file_years{$file} eq "????") || ($file_types{$file} eq "?")) {
	print "Unknown file type or year: $file\n";
    }
}
close(NEWCOPYRIGHTS);
