#!/usr/bin/perl
#
#  Licensed to the Apache Software Foundation (ASF) under one or more
#  contributor license agreements.  The ASF licenses this file to You
#  under the Apache License, Version 2.0 (the "License"); you may not
#  use this file except in compliance with the License.
#  You may obtain a copy of the License at
# 
#       http://www.apache.org/licenses/LICENSE-2.0
# 
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
#===================================================================
# Change the old license header to remove the copyright ASF line
# and replace it with the new "Licensed under the ..." text.
# Note that this should not be applied to the collective copyright 
# in, for example, a README file that describes the work as a whole.
#===================================================================
#
# Usage: copy2license.pl -j *.java
#    or
#        find . -name \*.java -print0 | xargs -0 copy2license.pl -j
#
#===================================================================

$pre   = ' *';
$years = '1999-2005';

# ensure proper usage
if (scalar @ARGV < 1) {
  ShowUsage();
  exit;
}

if ($ARGV[0] eq '-y') {
    shift;
    $years = shift;
}

if ($ARGV[0] eq '-c') {
    shift;
    $pre = ' *';
    $oldtext = <<"EOTOLD";
 Copyright $years The Apache Software Foundation or its licensors, as
 * applicable.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
EOTOLD
}
elsif ($ARGV[0] eq '-j') {
    shift;
    $pre = ' *';
    $oldtext = <<"EOTOLD";
 Copyright $years The Apache Software Foundation or its licensors,
 *                     as applicable.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
EOTOLD
}
elsif ($ARGV[0] eq '-x') {
    shift;
    $pre = '  ';
    $oldtext = <<"EOTOLD";
 Copyright $years The Apache Software Foundation or its licensors,
                       as applicable.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
EOTOLD
}
elsif ($ARGV[0] eq '-p') {
    shift;
    $pre = '# ';
    $oldtext = <<"EOTOLD";
 Copyright $years The Apache Software Foundation or its licensors,
#                      as applicable
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
EOTOLD
}
elsif ($ARGV[0] eq '-w') {
    shift;
    $pre = ' *';
    $oldtext = <<"EOTOLD";
 Copyright $years The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
EOTOLD
}
else {
  ShowUsage();
  exit;
}


$newtext = <<"EOTEXT";
 Licensed to the Apache Software Foundation (ASF) under one or more
$pre contributor license agreements.  The ASF licenses this file to You
$pre under the Apache License, Version 2.0 (the "License"); you may not
$pre use this file except in compliance with the License.
EOTEXT

undef $/; # slurp the whole file

# process each filename that was supplied
while ($filename = shift) {
  open(INFILE, $filename)  || die ("Cannot read from $filename, $!\n");
  $wholething = <INFILE>;
  close INFILE;

  if ($wholething =~ s#\Q$oldtext\E#$newtext#osm) {
      open(OUTFILE, ">$filename") || die ("Cannot write to $filename, $!\n");
      print OUTFILE $wholething;
      close OUTFILE;
  }
}

$/ = "\n"; # reset the input record separator

#==================================================
# ShowUsage
#==================================================
                                                                                
sub ShowUsage {
  print qq!
Usage:  $0 [ -y ] ( -c | -j | -x | -p | -w ) file [ file ... ]
                                                                                
  where:

    -y = match copyright years (e.g., '1999-2005' or '2004')
    -c = match ' *' prefix with line break between as .. applicable.
    -j = match ' *' prefix with line break between licensors, .. as
    -x = match '  ' prefix with line break between licensors, .. as
    -p = match '# ' prefix with line break between licensors, .. as
    -w = match ' *' prefix without 'or its licensors, as applicable'

  file = a file in which to substitute license text

!;
}
