#!/usr/bin/perl

use strict;
use warnings;
use CGI qw/:standard/;

my $PYTHON_PATH = "/tools/python2.4.4/bin/python";
my $BUILDBOT_PATH = "/tools/python2.4.4/bin/buildbot";
my $PATCH_DIR = "/builds/buildbot/patchstore/";
my $MASTER_HOST = "localhost:9989";
my $BRANCH = "HEAD_TRY";

# get the parameters
my $name = param("name");
if ($name eq "") {
    $name = "Noname";
}
my $patchfile = param("patchfile");

# pull all of the contents of the file
my $patch_handle = upload("patchfile");

# strip off everything except the filename itself
$patchfile =~ s/.*[\/\\](.*)/$1/;

# write out the contents
my $filename = "$PATCH_DIR/$patchfile-" . time();
open PATCH, ">$filename";
binmode PATCH;

while ( <$patch_handle> ) {
  print PATCH;
}
close PATCH;

print "Content-Type: text/html\n\n";

# and generate the buildbot change command
my @args = ($PYTHON_PATH,
	    $BUILDBOT_PATH, "sendchange", "--username", $name, "--master",
            $MASTER_HOST, "--branch", $BRANCH, "$filename");
system @args;
print join(' ', @args);
