#!/usr/bin/ruby1.9.1

#$KCODE = 'UTF8'

require 'active_support'
require 'set'
require 'pp'


dir_list = ['src', 'work']

file_list = {}

# parse copyright information
dir_list.each do |dir|
  IO.popen("rgrep -Ei '(copyright|credit)' #{dir}") do |io|
    io.each_line do |line|
      begin
        line =~ /^([^:]+):.*(?:CREDIT:|Copyright) (.*)$/
      rescue
        STDERR.puts "Problem with line (#{$!}): " + line
      end
      file = $1
      copyright = $2

      next unless file =~ /\.rb$/
      next if copyright.blank?
      # ignore copyright by the main author, taken into account
      # in a specific wildcard entry
      next if copyright =~ /(Sawyer|Trans|\?)/

      file_list[file] ||= Set.new
      copyright.split(', ').each do |copyright_part|
        file_list[file] << copyright_part.strip
      end
    end
  end
end

# merge identical copyright entries in the same paragraph
file_list.values.uniq.each do |copyright|
  # list of files having the exact same copyright information
  files = Hash[file_list.select{|k, v|  v == copyright }].keys

  # print DEP-5 copyright information
  puts
  puts "Files: " + files.join(" ")
  header = "Copyright: "
  copyright.each do |copyright_part|
    puts header + copyright_part
    header = " " * header.size
  end
  puts "License: Ruby or GPL-2"
end
