#!/usr/bin/perl -w

# Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer. 
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution. 
# 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
#     its contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission. 
#
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Script used by WebKit build slave to kick off performance tests.

use strict;

use FindBin;
use lib "$FindBin::Bin/../Scripts";
use webkitdirs;

use Getopt::Long;
use IO::Socket;
my $shouldPrebuild = 1;

my $perfMaster = "webkit-build-1.local";
my $perfSlave = "webkit-build-2.local";
my $slaveUser = "buildbot";
my $reportPort = 54481; # Something that looks sorta like SAFARI
my $slaveDirectory = "/ToTest";

my $buildDirectory = determineConfigurationProductDir();

my $userAndHost = $slaveUser . "@" . $perfSlave;
my $resultsUploadDestination;

GetOptions('upload-results=s' => \$resultsUploadDestination);

print "Copying built frameworks to PLT slave...\n";
my @frameworks = ("$buildDirectory/JavaScriptCore.framework", "$buildDirectory/WebCore.framework", "$buildDirectory/WebKit.framework");
die "Failed to copy to slave\n" unless (system("rsync", "-avz", @frameworks, "$userAndHost:$slaveDirectory") == 0);

print "Opening reponse port for PLT slave...\n";
my $sock = new IO::Socket::INET(LocalHost => $perfMaster,
                                LocalPort => $reportPort,
                                Proto => 'tcp',
                                Listen => 1,
                                Reuse => 1);
die "Could not create socket for port $reportPort: $!\n" unless $sock;

print "Starting performance tests on PLT slave...\n";
die "Failed to start slave!\n" unless (system("ssh", $userAndHost, "autovicki", $slaveDirectory, "--safari", "$slaveDirectory/Safari.app", "--count", 5, "--clean-exit", "--webkit-revision", currentSVNRevision(), "--show-results", "send-completed-results.command") == 0);

print "Waiting for PLT slave to respond...\n";
my $new_sock = $sock->accept();
while(<$new_sock>) {
   print $_;
}
close($sock);

if ($resultsUploadDestination) {
    print "Uploading results to $resultsUploadDestination\n";
    die "Failed to upload xml results file." unless (system("scp", "$userAndHost:/Results/PerformanceReportSummary.xml", $resultsUploadDestination) == 0);
    die "Failed to upload svg results file." unless (system("scp", "$userAndHost:/Results/PerformanceGraph.svg", $resultsUploadDestination) == 0);
}
