#!/usr/bin/perl

use strict;

use Config::Simple;
use DBI;
use File::Basename;

my $CFG_FILE = '@CFGDIR@/codepot.ini';
my $REPOBASE = basename($ARGV[0]);
my $USER = $ARGV[1];

my $QC = '';

sub get_config
{
	my $cfg = new Config::Simple();

	if (!$cfg->read ($CFG_FILE))
	{
		return undef;
	}

	my $config = {
		database_hostname => $cfg->param ("database_hostname"),
		database_port => $cfg->param ("database_port"),
		database_username => $cfg->param ("database_username"),
		database_password => $cfg->param ("database_password"),
		database_name => $cfg->param ("database_name"),
		database_driver => $cfg->param ("database_driver"),
		database_prefix => $cfg->param ("database_prefix")
	};

	return $config;
}

sub open_database
{
	my ($cfg) = @_;

	my $dbtype = $cfg->{database_driver};
	my $dbname = $cfg->{database_name};
	my $dbhost = $cfg->{database_hostname};
	my $dbport = $cfg->{database_port};

	if ($dbtype eq 'postgre') { $dbtype = 'Pg'; }
	elsif ($dbtype eq 'oci8') { $dbtype = 'Oracle'; }
	elsif ($dbtype eq 'mysqli') { $dbtype = 'mysql'; }

	my $dbstr;
	my $dbuser;
	my $dbpass;
	if ($dbtype eq 'Oracle')
	{
		$QC = '"';
		$dbstr = "DBI:$dbtype:";
		$dbuser = $cfg->{database_username} . '/' . $cfg->{database_password} . '@' . $dbhost;
		$dbpass = '';
	}
	else
	{
		$dbstr = "DBI:$dbtype:database=$dbname;";
		if (length($dbhost) > 0) { $dbstr .= "host=$dbhost;"; }
		if (length($dbport) > 0) { $dbstr .= "port=$dbport;"; }

		$dbuser = $cfg->{database_username};
		$dbpass = $cfg->{database_password};
	}

	my $dbh = DBI->connect(
		$dbstr, $dbuser, $dbpass,
		{ RaiseError => 0, PrintError => 0, AutoCommit => 0 }
	);

	return $dbh;
}

sub close_database
{
	my ($dbh) = @_;
	$dbh->disconnect ();
}

sub is_project_member
{
	my ($dbh, $prefix, $projectid, $userid) = @_;

	my $query = $dbh->prepare ("SELECT ${QC}projectid${QC} FROM ${QC}${prefix}project_membership${QC} WHERE ${QC}userid${QC}=? AND ${QC}projectid${QC}=?");
	if (!$query || !$query->execute ($userid, $projectid))
	{
		return (-1, $dbh->errstr());
	}

	my @row = $query->fetchrow_array;
	$query->finish ();
	return (((scalar(@row) > 0)? 1: 0), undef);
}

sub is_project_commitable
{
	my ($dbh, $prefix, $projectid) = @_;

	my $query = $dbh->prepare ("SELECT ${QC}commitable${QC} FROM ${QC}${prefix}project${QC} WHERE ${QC}id${QC}=?");
	if (!$query || !$query->execute ($projectid))
	{
		return (-1, $dbh->errstr());
	}

	my @row = $query->fetchrow_array;
	$query->finish ();
	return (((scalar(@row) > 0 && $row[0] eq 'Y')? 1: 0), undef);
}

#------------------------------------------------------------
# MAIN
#------------------------------------------------------------

my $cfg = get_config ();
if (!defined($cfg))
{
	print (STDERR "Cannot load codepot configuration file\n");
	exit (1);
}

my $dbh = open_database ($cfg);
if (!defined($dbh))
{
	printf (STDERR "Cannot open database - %s\n", $DBI::errstr);
	exit (1);
}

my $member; 
my $commitable; 
my $errstr;

($member, $errstr) = is_project_member (
	$dbh, $cfg->{database_prefix}, $REPOBASE, $USER);
if ($member <= -1)
{
	print (STDERR "Cannot check membership - $errstr\n");
	close_database ($dbh);
	exit (1);
}

($commitable, $errstr) = is_project_commitable (
	$dbh, $cfg->{database_prefix}, $REPOBASE);
if ($commitable <= -1)
{
	print (STDERR "Cannot check commitability - $errstr\n");
	close_database ($dbh);
	exit (1);
}

close_database ($dbh);

if ($member == 0)
{
	print (STDERR "$USER doesn't belong to the $REPOBASE project\n");
	exit (1);
}

if ($commitable == 0)
{
	print (STDERR "The $REPOBASE project is not commitable\n");
	exit (1);
}

exit (0);
