#!/usr/bin/perl -w

#  Licensed to the Apache Software Foundation (ASF) under one or more
#  contributor license agreements.  See the NOTICE file distributed with
#  this work for additional information regarding copyright ownership.
#  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.

#########################################################################
# Description:
# Scan an SVN working copy and report any NOTICE files that need the
# copyright year to be updated because the code below it has changed.
#
# Procedure:
#  cd to the directory of interest.
#  svn update
#  Run this script.
#  Finds all NOTICE files and uses 'svn info" to determine if code has changed.
#  Review the output for "MODIFY" notes.
#  Edit the relevant files manually and commit.
#
#########################################################################

use strict;
use File::Basename;

my @inputList = `find . -iname "*NOTICE*" -print`;
my ($totalCount, $yearMissingCount, $yearCurrentCount, $yearChangedCount) = (0, 0, 0, 0);
my $currentYear = 1900 + (localtime)[5];
foreach my $inputFn (@inputList) {
  next if $inputFn =~ /\.svn/;
  next if $inputFn =~ /\/build\//;
  next if $inputFn =~ /\.swp$/;
  next if $inputFn =~ /~$/;
  next if $inputFn =~ /\/lib\//; # probably notice files of supporting products
  chomp($inputFn);
  $totalCount++;
  my $existingYear = "";
  open(INPUT, "<$inputFn") or die "Could not open input file '$inputFn': $!";
  while (<INPUT>) {
    if (/Copyright [0-9]*-?([0-9]{4}) The Apache/) {
      $existingYear = $1;
      last;
    }
  }
  close INPUT;
  
  # get the last changed date via 'svn info'
  my $dirname = dirname($inputFn);
  my $changed = `svn info $dirname | grep "Last Changed Date"`;
  $changed =~ /([0-9]{4})-([0-9]{2}-[0-9]{2})/;
  my $changedYear = $1;

  print "$inputFn : existingYear=$existingYear svnChangedDate=$1-$2";
  if ($existingYear eq "") { $yearMissingCount++; }
  elsif ($changedYear > $existingYear) {
    print " MODIFY";
    $yearChangedCount++;
  }
  elsif ($existingYear == $currentYear) {
    $yearCurrentCount++;
  }
  print "\n";
}

print qq!
There was a total of $totalCount NOTICE files.
$yearMissingCount were missing the copyright year line.
$yearCurrentCount have current copyright year.
$yearChangedCount need to be changed to the current year $currentYear.
!;
