#!/usr/bin/perl
# cvsstat 1.1 http://www.davidc.net/cvsstat/

# Configuration variables:

# CVS module (used for title)
$module = '99dogs devel';

# CVS log output file (do 'cvs log > cvs-log')
$logfile = 'cvs-log-20020731';

use CGI;
$cgi = new CGI;
use CGI::Carp qw/fatalsToBrowser/;
print $cgi->header();

print <<EOF;
<html>
<head>
<title>$module CVS statistics</title>
<style type="text/css">
<!--
.stats { background-color: white; border-collapse: collapse; }
.stats TD { border: 1px solid black; }
.stats TH { font-weight: bold; padding: 2px 5px 2px 5px; background-color: #0000AA; color: white; }
-->
</style>
</head>
<body bgcolor="#FFFFFF">
<font face="Arial, Helvetica, Sans-Serif">

<table class="stats" cellspacing="0" cellpadding="2">
<tr>
<td align="center" colspan="5">
<strong>$module statistics</strong>
</td>
</tr>
<tr>
<th>Committer</th>
<th>\# of Commits</th>
<th>Lines Changed</th>
<th>Lines per Commit</th>
<th>\% of Changed Lines</th>
</tr>
EOF

$global = 0; # Global total number of added lines

open(DATA, '<'.$logfile) || die "$!";
while (<DATA>) {
    if (/^date: /) {
        $author = "$_";
        $author =~ s/date: .* author: (.*) state: .*/$1/i;
        $author =~ s/\;//g;
        $thedate = "$_";
	$thedate =~ s/date: ([0-9]{4}\/[0-9]{2}\/[0-9]{2})/$1/;
	$thedate = $1;
#	print "$thedate<br>\n";
        if ($lines{"$author"} !~ /./) {
            $lines{"$author"} = 0;
        }
        if ($commits{"$author"} !~ /./) {
            $commits{"$author"} = 0;
        }
        if ($datelines{"$thedate"} !~ /./) {
            $datelines{"$thedate"} = 0;
        }
        if ($datecommits{"$thedate"} !~ /./) {
            $datecommits{"$thedate"} = 0;
        }
        $commits{"$author"} ++;
        $datecommits{"$thedate"} ++;

        if (/lines:/) {
            $changes = "$_";
            $changes =~ s/.*lines: \+([0-9]*) \-([0-9]*)/$1+$2/;
            $lines{"$author"} += eval "$changes";
            $global += eval "$changes";
	    $datelines{"$thedate"} += eval "$changes";
        }
    }
}
close(DATA);

sub sorter {
    $commits{$b} <=> $commits{$a};
}

foreach $committer (sort sorter (keys %commits)) {
    $number = $commits{"$committer"};
    $changes = $lines{"$committer"};
    $percent = ($changes / $global) * 100;
    $average = $changes / $number;
    foreach $_ ($percent, $average) {
        s/\..*//g;
    }
    print <<EOF;
<tr>
<td>$committer</td>
<td>$number</td>
<td>$changes</td>
<td>$average</td>
<td>$percent\%</td>
</tr>
EOF
}

print <<EOF;
<tr>
<th>Date</th>
<th>\# of Commits</th>
<th>Lines Changed</th>
<th>Lines per Commit</th>
<th>\% of Changed Lines</th>
</tr>
EOF

foreach $thedate (sort (keys(%datelines))) {
    $number = $datecommits{"$thedate"};
    $changes = $datelines{"$thedate"};
    $percent = ($changes / $global) * 100;
    $average = $changes / $number;
    foreach $_ ($percent, $average) {
        s/\..*//g;
    }


    print <<EOF;
<tr>
<td>$thedate</td>
<td>$number</td>
<td>$changes</td>
<td>$average</td>
<td>$percent\%</td>
</tr>
EOF
}

print <<EOF;
<tr>
<td align="center" colspan="5">
<strong>$module CVS statistics generated by <a href="http://www.davidc.net/cvsstat/">cvsstat</a></strong>
</td>
</tr>
</table>
</font>
</body>
</html>
<!-- Originally based off cvstat 1.0
   - Modified and updated by David Croft <david@davidc.net>
-->
EOF
