#!/usr/local/bin/perl -w
#
# Copyright (C) 1998, 1999, 2000  Internet Software Consortium.
# 
# Permission to use, copy, modify, and 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
# ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
# CONSORTIUM 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.

if (@ARGV == 0) {
    die "usage: update_copyrights <copyright_text>";
}
@copyright_text = ();
open(COPYRIGHT, "<$ARGV[0]") || die "can't open $ARGV[0]: $!";
@copyright_text = <>;
close(COPYRIGHT);

while (<>) {
    ($file, $type, $years_list) = split(/\s+/);
    @years = split(/,/, $years_list);

    if ( ! -f $file ) {
	print "$file: missing\n";
	next;
    }
    if ($type eq "X") {
	if ( ! -f $file ) {
	    print "$file: type X, but missing\n";
	}
	next;
    }

    $before_copyright = "";
    $c_comment = 0;
    $shell_comment = 0;
    $m4_comment = 0;
    $first = "";
    if ($type eq "C") {
	$c_comment = 1;
	$prefix = " * ";
    } elsif ($type eq "SH" || $type eq "PERL" || $type eq "MAKE") {
	$shell_comment = 1;
	$prefix = "# ";
    } elsif ($type eq "M4") {
	$m4_comment = 1;
	$prefix = "dnl ";
    } else {
	print "$file: type '$type' not supported yet; skipping\n";
	next;
    }
    open(SOURCE, "<$file") || die "can't open $file: $!";
    $_ = <SOURCE>;
    if ($c_comment && /^\/\*/) {
	$_ = <SOURCE>;
	if ($_ !~ /[Cc]opyright/) {
	    print "$file: non-copyright comment\n";
	    close(SOURCE);
	    next;
	}
	if ($_ !~ /\*\//) {
	    while (<SOURCE>) {
		if ($_ =~ /\*\//) {
		    last;
		}
	    }
	}
    } elsif ($shell_comment) {
	if (/^\#\!/) {
	    $before_copyright = "$_#\n";
	    $_ = <SOURCE>;
	    if ($_ eq "#\n") {
		$_ = <SOURCE>;
	    }
	}
	if (/^\#/) {
	    if ($_ !~ /[Cc]opyright/) {
		print "$file: non-copyright comment\n";
		close(SOURCE);
		next;
	    }
	    while (<SOURCE>) {
		if ($_ !~ /^\#/) {
		    $first = $_;
		    last;
		}
	    }
	} else {
	    $first = $_;
	}
    } elsif ($m4_comment && /^dnl /) {
	if ($_ !~ /[Cc]opyright/) {
	    print "$file: non-copyright comment\n";
	    close(SOURCE);
	    next;
	}
	while (<SOURCE>) {
	    if ($_ !~ /^dnl /) {
		$first = $_;
		last;
	    }
	}
    } else {
	$first = $_;
    }
    open(TARGET, ">$file.new") || die "can't open $file.new: $!";
    if ($before_copyright ne "") {
	print TARGET $before_copyright;
    }
    if ($c_comment) {
	print TARGET "/*\n";
    }
    print TARGET "${prefix}Copyright (C) ";
    $first_year = 1;
    foreach $year (@years) {
	if (! $first_year) {
	    print TARGET ", ";
	}
	print TARGET "$year";
	$first_year = 0;
    }
    print TARGET "  Internet Software Consortium.\n";
    print TARGET "$prefix\n";
    foreach $_ (@copyright_text) {
	print TARGET "${prefix}$_";
    }
    if ($c_comment) {
	print TARGET " */\n";
    }
    if ($first eq "") {
	$first = <SOURCE>;
    }
    if ($first ne "") {
	if ($first !~ /^\s*$/) {
	    print TARGET "\n";
	}
	print TARGET $first;
	while (<SOURCE>) {
	    print TARGET $_;
	}
    }
    close(TARGET);
    close(SOURCE);
    $mode = (stat $file)[2]&511;
    chmod $mode, "$file.new";
    rename("$file", "$file.bak") || die "rename($file, $file.bak): $!";
    rename("$file.new", "$file") || die "rename($file.new, $file): $!";
}
