#!/usr/bin/env ruby

cmd = `git log --pretty='format:%ci::%an <%ae>::%s'`

list = {}
list_order = []
contributors = []
changelog_file = "CHANGELOG"
contributors_file = "Contributors"

cmd.each_line do |l|
  date, author, subject = l.chomp.split("::")
  date, _time, _zone = date.split(" ")

  id = "#{date}\t#{author}"
  unless (list[id])
    list[id] = []
    list_order << {:id => id, :value => list[id]}
  end
  list[id] << subject
  contributors << author
end

# list.each do |id, value|
file = File.new(changelog_file, "w")
list_order.each do |i|
  id = i[:id]
  value = i[:value]

  file.puts id.to_s
  file.puts value.map { |e| "\t* #{e}" }.join("\n")
  file.puts "\n"
end
file.close
file = File.new(contributors_file, "w")
file.puts "Contributors (sorted alphabetically)"
file.puts "\n"
file.puts contributors.uniq.sort.join("\n")
file.close
