#!/usr/bin/perl

use strict;
use warnings;
use CGI qw/:standard/;

my $PYTHON_PATH = "/usr/bin/python";
my $BUILDBOT_PATH = "/usr/bin/buildbot";
my $PATCH_DIR = "/home/buildmaster/test/patches";
my $MASTER_HOST = "localhost:9990";
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
$patchfile = "$patchfile-" . time();
my $filename = "$PATCH_DIR/$patchfile";
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, "$patchfile");
system @args;
print join(' ', @args);
