#!/bin/sh -- # A comment mentioning perl to prevent looping.
eval 'exec perl -i -s -S $0 ${1+"$@"}'
    if 0;

#
#  FIRST: make clean
#  Usage: add_copyright `find . -type f -print -name CVS -prune`
#  Desc : Replace/Insert our copyrights with new version. 
#
#
#  WARNING: This script may damage the original sources.
#	It has at least the following problems:
#    	* depend on blank line just after copyright, otherwise
#	    everything up until a blank line will be replaced by
#    	    the copyright notice
#    	* can't handle any lines before the copyright notice
#
#     There are probably others.  So we recommend running this
#     to find out any changes that aren't related to copyrights:
#		
#      cvs diff | egrep -v "Copyright|University|COPYRIGHT|Reserved|RCS|retrieving|diff|------|======"
#
#


$/ = "\n\n";	# input record separator
$* = 1;		# multi-line search

$cpysrc = '/* --------------------------------------------------------------- */
/* -- Copyright (c) 1994, 1995 Computer Sciences Department,    -- */
/* -- University of Wisconsin-Madison, subject to the terms     -- */
/* -- and conditions given in the file COPYRIGHT.  All Rights   -- */
/* -- Reserved.                                                 -- */
/* --------------------------------------------------------------- */

';

$cpyother = '# --------------------------------------------------------------- #
# -- Copyright (c) 1994, 1995 Computer Sciences Department,    -- #
# -- University of Wisconsin-Madison, subject to the terms     -- #
# -- and conditions given in the file COPYRIGHT.  All Rights   -- #
# -- Reserved.                                                 -- #
# --------------------------------------------------------------- #

';

if($d) {
    # open(STDERR, ">/tmp/add_copyright.debug") || die (" cannot open debug");
}

sub dofile {
    if($d) { print STDERR "PROCESSING...\n"; }
    while (<>)  {
	if ($. == 1 || $skip_first) {
	    if($d) { print STDERR " $ARGV\n"; }
	    $skip_first = 0 if $skip_first;
	    if ($ARGV=~/.*\.[SCchx]/) {
		$cpy = $cpysrc;
	    } elsif ($ARGV=~/.*\.tmpl/) {
		$cpy = $cpysrc;
	    } elsif ($ARGV=~/.*\.rules/) {
		$cpy = $cpysrc;
	    } elsif ($ARGV=~/Imakefile/) {
		$cpy = $cpysrc;
	    } elsif ($ARGV=~/Platform\.select/) {
		$cpy = $cpysrc;
	    } elsif ($ARGV=~/shore\.def\.example/) {
		$cpy = $cpysrc;
	    } else {
		$cpy = $cpyother;
	    }
	    if (/\([Cc]\)/ || /[Cc][Oo][Pp][Yy][Rr][Ii][Gg][Hh][Tt]/) {
		if (/University of Wisconsin[\s-]{1,4}Madison/)  {
		    print $cpy
		}
	    } elsif (/^\/\//)  {
		# skip lines beginning with // 
		$skip_first = 1;
		print;
	    } elsif (/^\#\!/)  {
		# skip lines beginning with #! 
		$skip_first = 1;
		print;
	    } else {
		print $cpy;
		print;
	    }
	} else {
	    print;
	}
	close(ARGV) if eof;		# reset line number
    }
}

$i = 0;
$j = 0;
if($d) { print STDERR " SKIPPING....\n"; }
foreach $FILE (@ARGV) {
    $skip = 0;
    if(! -f $FILE) {
	if($d) { print STDERR  $FILE."Cannot handle non-files! use find to skip them\n";
	}
	exit(1);
    } elsif($FILE =~ /.*\.[ly]$/) {
	# skip lex & yacc files
	$skip = 1;
    } elsif($FILE =~ /.*\.patch$/) {
	# skip patch files
	$skip = 1;
    } elsif ($FILE =~ /^\.[^\/].*/) {
	# skip dotfiles
	$skip = 1;
    } elsif ($FILE =~ /.*\/CVS\/.*/) {
	# skip CVS files
	$skip = 1;
    } elsif ($FILE =~ /.cvsignore/) {
	# skip .cvsignore files
	$skip = 1;
    } elsif ($FILE =~ /.*sed\.script.*/) {
	# skip sed scripts, by convention here
	# called sed.script.<xxxx>
	$skip = 1;
    } else {
	$skip = 0;
    }

    if($skip) {
	if($d) { print STDERR " $FILE\n"; }
	$j++;
    } else {
	@AVLIST[$i++] = @ARGV[$j++];
    }
}
@ARGV = @AVLIST;
&dofile;
if ($d) { print STDERR "skipped $j-$i files\n"; }
