#!/usr/bin/perl 

use strict;
use IO::Socket;
use Getopt::Long;

my $HOST = "localhost";
my $PORT = 12000;
my $STARTTIME = 0;
my $ENDTIME = time;

my $debug = 0;
my $tempuser = `id -u -n`;
chomp($tempuser);

my $temphost = `hostname`;
chomp($temphost);

my $USER = $tempuser . "@" . $temphost;


GetOptions("host=s"=>\$HOST,
			"port=i"=>\$PORT,
			"starttime=i"=>\$STARTTIME,
			"endtime=i"=>\$ENDTIME,
			"user=s"=>\$USER);

printf("DEBUG: Going to contact $HOST at $PORT looking for $USER between $STARTTIME and $ENDTIME\n") if $debug;

my $socket = IO::Socket::INET->new("$HOST:$PORT") or die $@;

my $response;
while( !($response =~ m/READY/) ) {
$response = <$socket>;
print "DEBUG: $response" if $debug;
}
print $socket "FAILURES $USER $STARTTIME $ENDTIME\n";
print "DEBUG: FAILURES $USER $STARTTIME $ENDTIME\n" if $debug;

$response = <$socket>;
print "DEBUG: $response" if $debug;

my ($answer, $extended) = split(/:/, $response, 2);

print "DEBUG: Answer of $answer, Extended is $extended\n" if $debug;
$socket->close;

if($answer eq "YES") {
	print "$answer\n";
	exit 1;
}

if($answer eq "NO") {
	print "$answer\n";
	exit 0;
}

if($answer eq "ERROR") {
	print "$response\n";
	exit(-1);
}
