#!/usr/bin/perl

use strict;
use warnings;

use IPC::Run qw/ run /;
use Data::Dumper;

my %authors;
my %unreleased = (
	'2.4' => 'HEAD',
);

sub versions {
    my ($start, $end, $step) = @_; 

    return map {
    	my $k = sprintf('%.1f', ($_ * $step));
	$k => sprintf('%.1f', ($k + 0.1));
    } ($start / $step .. $end / $step);
}

my %v_map = versions(0.8, 2.4, 0.1);

foreach my $ver (sort keys %v_map) {
	my $g_out;
	my $to = $v_map{$ver};

	if (exists($unreleased{$to})) {
		$to = $unreleased{$to};
	}

	my $cmd = [
		'git', 'log',
		$ver .  '..' . $to,
		'--format=%B'
	];
	run($cmd, \$g_out, \$g_out, undef) or die $!;
	my @lines = split("\n", $g_out);

	foreach my $line (@lines) {
		if ($line =~ m/(?:from\s+|by\s+)([A-Z][A-Za-z0-9 ]*)\.$/) {
			$authors{$to}{$1}++;
		}
	}
}

my $html_out = "<html><body><h1>Tmux Authors</h1>";

foreach my $k (sort {$a cmp $b } keys %authors) {
	$html_out .= "<h2>$k</h2>\n";
	$html_out .= "<ul>\n";
	
	foreach my $name (sort keys %{ $authors{$k} }) {
		$html_out .= "<li><b>${name}</b>: " .
		$authors{$k}->{$name} . "</li>\n";
	}
	$html_out .= "</ul><hr />\n";
}
$html_out .= "</body></html>\n";

open my $f_out, ">", "./authors.html" or die $!;
print $f_out $html_out, "\n";
close $f_out;
