#!/usr/bin/perl -w
# $Id: mkcopyright.pl,v 1.1 1997/11/20 01:15:59 ray Exp ray $
#use strict;

($C = $0) =~ s%.*/%%;

my $In;
my $Out;
my $Mode = 1;

my $Help = 0;
my $OptErr = "";
my $verbose = 0;
my $debug = 0;
my $p = 0;


while ( $#ARGV >= $[ && ($_ = shift, /^-/ || (unshift(@ARGV,$_) && 0)) ) {
  last if /^--$/;
  (/^--mode=(\d+)$/)			&& ($Mode = $1, next);
  (/^--verbose$/ || /^-v$/)		&& ($verbose ++, next);
  (/^--debug$/ || /^-d$/)		&& ($debug ++, $verbose ++, next);
  (/^--?help/ || /^-h/)			&& ($Help = 1, next);
  (/^-/)				&& ($OptErr .= "$_ ", $_ = "", next);
}


$OptErr = sprintf( "$C: unkown option: %s\n", $OptErr) if ( $OptErr );

if ( $#ARGV < $[ + 1 ) {
  $OptErr .= sprintf( "$C: missing parameter(s)\n");
} elsif ( $#ARGV > $[ + 1 ) {
  $OptErr .= sprintf( "$C: superfluous parameter(s)\n");
} else {
  $In = shift;
  $Out = shift;
}

if ( $OptErr || $Help ) {
  print( STDERR $OptErr) if ( $OptErr );
  die( "Usage: $C [options] in out\n");
}

open( IN, "<$In") || die("open($In): $!\n");
open( OUT, ">$Out") || die("open($Out): $!\n");
select( OUT);

if ( $Mode == 1 ) {
  print( " *\n");

  while ( <IN> ) {
    $p = 1 if ( /^ \* Copy/ );
    $p = 1 if ( /^ \* THIS/ );
    last if ( /^ \*\// );
    print if ( $p );
    $last = $_;
  }

  print( " *\n") unless ( $last =~ /^ \*\s*$/ );
} elsif ( $Mode == 2 ) {
  print( " *\n");

  while ( <IN> ) {
    $p = 1 if ( /^ \* THIS/ );
    last if ( /^ \*\// );
    print if ( $p );
    $last = $_;
  }

  print( " *\n") unless ( $last =~ /^ \*\s*$/ );
} else {
  unlink $Out;
  print( STDERR "$C: mode $Mode not implemented!\n");
  exit( 1);
}

close( OUT);
close( IN);
cpmod( $In, $Out) unless ( $Out eq "-" );
exit( 0);

sub cpmod($$) {
  my( $Org, $Dup) = @_;
  my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
      $atime,$mtime,$ctime,$blksize,$blocks) = stat($Org);
  $mode &= 007777; $mode |= 000200;
  chown( $uid, $gid, $Dup) || die( "$CMD: cannot chown( $uid, $gid, $Dup).\n");
  chmod( $mode, $Dup) || die( "$CMD: cannot chmod( $mode, $Dup).\n");
  utime( $atime, $mtime, $Dup) || die( "$CMD: cannot utime( $Dup).\n");
}

