#!/usr/bin/perl -w
# Jesse Yurkovich <yurkjes@iit.edu> & Ondrej Hrebicek <hrebond@iit.edu>
# adapt wim bertels

use strict;

open (HIST, '/var/lib/cvs/CVSROOT/history') or die "Can't open cvs history file.";
#exit if (time - (stat(HIST))[9] > 3600);

open (HTML, '>/var/www/cvs_stats.html') or die "Can't create cvs_stats.html.";
open(IN,'/tmp/naam.tmp') or die "/tmp/naam.tmp den boom in";

my %modules = ();
while (my $line = <HIST>)
{
	my @info = split ('\|', $line);

	# $modules { Module Name }{ User }{ CVS Type }++
	if (exists $info[3] && exists $info[1] && exists $info[0])
	{
		$modules{ (split ('\/', $info[3]))[0] }{ $info[1] }{ substr ($info[0], 0, 1) }++;
	}
}

close (HIST);

my @t = localtime (time);
my $timedate = ($t[4] + 1) . "/$t[3]/" . ($t[5] + 1900) . " at " . (($t[2] < 10) ? "0" . $t[2] : $t[2]) . ":" . (($t[1] < 10) ? "0" . $t[1] : $t[1]) . ":" . (($t[0] < 10) ? "0" . $t[0] : $t[0]);

print HTML <<"EOF";
<html>
<head>
	<title>Omega -- CVS Repository Statistics</title>
	<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body vlink="blue">
	<center>
	<span class="title"><h2>Omega -- <span class="emph">CVS Repository Statistics</span></h2></span>
	Last generated on $timedate<p>	
EOF

foreach my $module (sort keys %modules)
{
	print HTML <<"EOF";

	<p>
	<table width="90%" border="1" cellspacing="0" cellpadding="4">
	<tr>
		<td valign="middle" align="center" width="180" height="50" align="center" style="font-family: Verdana; font-size: 20px;">$module</td>
		<td valign="middle" class="emph-cvs">Total Checkouts</td>
		<td valign="middle" class="emph-cvs">Total Commits</td>
		<td valign="middle" class="emph-cvs">Added</td>
		<td valign="middle" class="emph-cvs">Modified</td>
		<td valign="middle" class="emph-cvs">Removed</td>
		<td valign="middle" class="emph-cvs">Total Updates</td>
		<td valign="middle" class="emph-cvs">Ups</td>
		<td valign="middle" class="emph-cvs">Merges</td>
		<td valign="middle" class="emph-cvs">Conflicts</td>
		<td valign="middle" class="emph-cvs">Deletes</td>
	</tr>
EOF


	my $i = 0;
	foreach my $user (sort keys %{ $modules{$module} })
	{
		my ($bgcolor, $textcolor) = ($i++ % 2) ? ("gray", "#d8d8d8") : ("#d8d8d8", "black");
		my %mu = %{$modules{ $module }{ $user }};

		system("ldapsearch -x uid=$user cn | grep cn: >> naam.tmp");
		
		while(<IN>){
			print HTML "<tr style=\"color: $textcolor;\"><td align=\"center\" bgcolor=\"$bgcolor\">$user $_</td>";
			}

		foreach my $type ( ("O", "A", "M", "R", "U", "G", "C", "W") )
		{
			if (!defined ($mu{$type})) { $mu{$type} = 0; }
		}

		my $total_commits = $mu{"A"} + $mu{"M"} + $mu{"R"};
		my $total_updates = $mu{"U"} + $mu{"G"} + $mu{"C"} + $mu{"W"};

		print HTML <<"EOF";

			<td bgcolor="$bgcolor">$mu{"O"}</td>
			<td bgcolor="$bgcolor">$total_commits</td>
			<td bgcolor="$bgcolor">$mu{"A"}</td>
			<td bgcolor="$bgcolor">$mu{"M"}</td>
			<td bgcolor="$bgcolor">$mu{"R"}</td>
			<td bgcolor="$bgcolor">$total_updates</td>
			<td bgcolor="$bgcolor">$mu{"U"}</td>
			<td bgcolor="$bgcolor">$mu{"G"}</td>
			<td bgcolor="$bgcolor">$mu{"C"}</td>
			<td bgcolor="$bgcolor">$mu{"W"}</td>
		</tr>
EOF
	}

	print HTML "</table><p><br>\n";
}

print HTML <<"EOF";
<p>
</center>

Page dynamically generated using <a href="http://omega.cs.iit.edu/viewcvs/www-scripts/">cvshistory.pl</a>.

</body>
</html>
EOF

close (HTML);
chmod (0664, '/var/www/cvs_stats.html');
