#!/usr/local/bin/perl

use strict;
use FindBin;
use LWP::Simple;

eval { require "/usr/local/etc/foundry.conf" }                                  || eval { require "$FindBin::Bin/../etc/foundry.conf" }
|| eval { require "$FindBin::Bin/../etc/Foundry/foundry.conf" }
|| die $@;

my ($svn_base) = grep { defined and -d } (
    $ENV{SVNROOT}, 
    '/home/svn',
    '/home/svn-repositories',
) or die "Cannot find SVNROOT";
$svn_base .= "/foundry-repos";

my $rt_url = $ENV{RT_URL} || $ENV{WEB_HOST} or die 'Must specify $ENV{RT_URL}'.$/;
$rt_url = "http://$rt_url" unless $rt_url =~ m!^[a-z]+://!;

my ($repos, $user) = @ARGV;

my $project = $1 if $repos =~ m!([^/]+)/*\z!;

exit 1 unless $project;

my $content = get("$rt_url/REST/0.0/NoAuth/has_member?Queue=$project&User=$user");

exit 0 if $content =~ '^OK';

die "permission denied for user $user to commit to project $project";

