mysql-copyright-2 3.67 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/perl -i

# Add the header to all given files
# This program asumes that after the copyright there is a empty line
#

$opt_v= 0;
require "getopts.pl";
Getopts("v") || die "Aborted";

@copyright=
(
 "Copyright (C) 2000 MySQL AB & MySQL Finland AB",
 "",
 "This software is distributed with NO WARRANTY OF ANY KIND.  No author or",
 "distributor accepts any responsibility for the consequences of using it, or",
 "for whether it serves any particular purpose or works at all, unless he or",
18
 "she says so in writing.  Refer to the MySQLEULA.txt file for details.",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
19 20
 "",
 "Every copy of this file must include a copy of the License, normally in a",
21
 "plain ASCII text file named MySQLEULA.txt. The License grants you the right to",
bk@work.mysql.com's avatar
bk@work.mysql.com committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 "copy, modify and redistribute this file, but only under certain conditions",
 "described in the License.  Among other things, the License requires that",
 "the copyright notice and this notice be preserved on all copies"
);

while (<>)
{
  if (!$first++)
  {
    add_copyright($_);
  }
  if ($in_copyright)
  {
    $in_copyright=check_in_copyright($_);
  }
  print $_ if (!$in_copyright);
  if (eof)
  {
    $first=0; $in_copyright=1;
  }
}

exit 0;

sub add_copyright
{
  my ($line)=@_;
  my ($row);

  $in_copyright= $line =~ /copyright/i;
  $found_end_copyright=$skip_this_line=0;
	
  if (!($line =~ /Monty/ || $line =~ /MySQL AB/))
  {
    $in_copyright=0;
    print STDERR "File with unknown copyright ", $ARGV,"\n" if ($opt_v);
    return;
  }
  else
  {
    print STDERR "To be Changed: ", $ARGV, "\n" if ($opt_v);
  }
  if ($ARGV =~ /Makefile/ ||
      $ARGV =~ /makefile/)
  {				# Makefile
    $start_copyright="# ";
    $line_copyright= "# ";
    $end_copyright=  "";
  }
  elsif ($line =~ "^#!")
  {				# Shell script
    $start_copyright="# ";
    $line_copyright= "# ";
    $end_copyright=  "";
    $skip_this_line=1;
    print $line;
    while ($line=<>)		# Copy all until new line or copyright
    {
      if ($line =~ /copyright/i)
      {
	last;
      }
      print $line;
      last if ($line =~ /^(\s|\n)*$/);
    }
    $in_copyright=1;
  }
  elsif ($ARGV =~ /\.c$/ ||
	 $ARGV =~ /\.cc$/ ||
	 $ARGV =~ /\.h$/ ||
patg@krsna.patg.net's avatar
patg@krsna.patg.net committed
92
	 $ARGV =~ /\.cpp$/ ||
93
	 $ARGV =~ /\.txt$/ ||
94
	 $ARGV =~ /\.yy$/)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
95 96 97
  {
    $start_copyright="/* ";
    $line_copyright= "   ";
98
    $end_copyright=  "*/";
bk@work.mysql.com's avatar
bk@work.mysql.com committed
99
  }
100 101 102 103 104 105
	elsif ($ARGV =~ /-x86\.s$/)
	{
		$start_copyright="# ";
		$line_copyright= "# ";
		$end_copyright=  "";
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  elsif ($ARGV =~ /\.s$/)
  {
    $start_copyright="! ";
    $line_copyright= "! ";
    $end_copyright=  "";
  }
  elsif ($ARGV =~ /\.asm$/)
  {
    $start_copyright="; ";
    $line_copyright= "; ";
    $end_copyright=  "";
  }
  else				# Unknown file
  {
    $in_copyright=0;
    print STDERR "Unknown file type ", $ARGV,"\n" if ($opt_v);
    return;
  }
  $data=\@copyright;

  for ($row=0 ; $row <= $#$data ; $row++)
  {
    print $row == 0 ? $start_copyright : $line_copyright;
    print $data->[$row];
    print $row != $#$data ? "\n" : $end_copyright . "\n";
  }
  print "\n";
  $end_copyright =~ /\s*([^\s]+)\s*(([^\s].*)|)$/; # Remove pre and post spaces
}

#
# Return 1 if in copyright
#

sub check_in_copyright
{
  my ($line)=@_;
  $line =~ /^(.*[^\s])(\s|\n|\r)*$/; # Remove end space and newline
  $line=$1;
  if (!$line)
  {
    $found_end_copyright=1 if (!length($end_copyright));
    return 1;			# Skip empty lines
  }
  return 0 if ($found_end_copyright);
  if ($end_copyright)
  {
    if (index($line,$end_copyright) != -1)
    {
      $found_end_copyright=1;
    }
    return 1;
  }
  if ($line =~ /copyright/i || index($line . " ",$line_copyright) == 0)
  {
    return 1;
  }
  if ($skip_this_line)
  {
    $skip_this_line=0;
    return 1;
  }
  return 0;			# Can't trust the empty copyright line yet 
}