#!/usr/bin/perl
###################################################################
# mantis_cvs
# Basic CVS Integration with Mantis Bug Tracker
# Author: Leonardo Herrera (leus@epublish.cl)
###################################################################
  use strict;
  use DBI();
  use Mail::Sender;


# CONFIGURATION SECTION

my $mantis_dbserver = 'localhost';
my $mantis_username = 'root';
my $mantis_password = '';
my $mantis_database = 'bugtracker';

# Define this if you want basic email support.
# Note: this is just a simple way to nag people about new checkins in
# some bugs. It doesn't attemp (yet) to determine to whom really send
# email, for example, people monitoring this bug.

my $smtp	= undef; # Example: 'smtp.mydomain.com'
my $smtp_from	= undef; # Example: 'user@mydomain.com'
my $mail_to	= undef; # Example: 'usera@domain.com'

# This variable defines who is reporting this checkin. Use
# a generic user, example, "cvsman". "administrator" is okay.
my $default_reporter_name	= 'administrator';

# This is the URL where your Mantis Bug Tracker is located. Include
# full query string for prefixing to a bug number
my $base_url	 	= 'http://127.0.0.1/mantisbt/bug_view_page.php?bug_id=';

# See "constants_inc.php" for this value.
my $CVS_CHECKIN = 15;

# This regex will find ocurrences of the word "bug" followed by a number.
my $bugregex = qr/\bbug (\d+)\b/;

# END CONFIGURATION SECTION.

##########################################################################
sub send_email {
	my %bugs = @_;

	my $sender = new Mail::Sender {
		smtp => $smtp, 
		from => $smtp_from,
		on_errors => undef,
	} or return;
	$sender->Open({
		to => $mail_to,
		subject => "Mantis: New checkins for bugs " . join ",", keys %bugs
	}) or return;

	foreach my $bug_id (keys %bugs) {
		$sender->SendLineEnc( "\t$bug_id: $base_url$bugs{$bug_id}" );
	}

	$sender->SendLineEnc( "\n" . localtime() . "\n" );
	$sender->Close() or return;
}
##########################################################################
sub registerCheckIn() {
	# Register the event into the historic table.
	my $dbh = shift;
	my $bug_id = shift;
	my $reporter_id = shift;
	my $file = $dbh->quote( shift );
	my $newv = $dbh->quote( shift );
	
	$dbh->do( 
		"INSERT INTO mantis_bug_history_table ( user_id, bug_id, date_modified, type, old_value, new_value ) " .
		"VALUES ( $reporter_id, $bug_id, NOW(), $CVS_CHECKIN, $file, $newv)" );
}
##########################################################################
sub get_user_id()
{
	my $dbh = shift;
	my $reporter_name = shift;
	my $reporter_id = $dbh->quote( $reporter_name );
	my $sth = $dbh->prepare("SELECT * FROM mantis_user_table where username = '$reporter_name'" );
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref()) {
		$reporter_id = $ref->{'id'};
	}
	$sth->finish();

	die "Cannot obtain reporter id\n" if $reporter_id == -1;
	print "Reporter id: $reporter_id\n";

	return $reporter_id;
}
##########################################################################
sub process() {
	my $dbh = shift;
	my $reporter_id = shift;
	my $bug_id = shift;

	foreach ( @ARGV ) {
		my ($file,$newv) = split ",";
		&registerCheckIn( $dbh, $bug_id, $reporter_id, $file, $newv );
	}
}
##########################################################################
sub main() {
	my %bugs;

	while( <STDIN> ) {
		while ( /$bugregex/gi ) {
			print "Mentioning of bug $1 found.\n";
			$bugs{"Bug$1"} = $1;
		}
	}
	
	if (scalar keys %bugs) {
		print "Bug mentioning in commit log found.\n";

		my $dbh = DBI->connect("DBI:mysql:database=$mantis_database;host=$mantis_dbserver",
					 "$mantis_username", "$mantis_password",
					 {'RaiseError' => 1});
		die if !$dbh;
		my $reporter_id = &get_user_id( $dbh, $default_reporter_name );

		foreach (keys %bugs) {
			print "Adding informacion of $_...\n";
			&process( $dbh, $reporter_id, $bugs{$_} );
		}
		$dbh->disconnect();
		&send_email( %bugs );
	}
}
##########################################################################
&main;

