#!/usr/bin/perl -w
require 5.002;
use IO::Handle;
use FileHandle;
use sigtrap;
use Socket;

$SIG{USR1} = \&handler;
$SIG{ALRM} = \&dummy;

$interval = 1;
@colors = ("black", "red", "orange", "yellow", "green", "blue", "purple", "tan", "OliveDrab", "brown");
#$cc=0;
$ccount=0;
$pcount=0;
$id=0;
$time=0.0;
$step=0.002;
readData();

open(NAM, "| ./nam -");

setup();
show(\@clients, "circle", $ccount);
show(\@proxies, "box", $pcount);
myprint("T -t $time\n");
$time+=$step;

alarm($interval);

$port = 5646;
$proto = getprotobyname('tcp');
socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l",  1))
    or die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";

# input format
#
# host1 host2 host3 color\n
#

while (1) {
    listen(Server, SOMAXCONN) or die "listen: $!";
    accept(Client,Server);

    #print "Accepted connection\n";
    
    while ($l = <Client>) {
	#print "Got a line: $l";
    if ($l =~ m/RESET/) {
        kill 10, $$;
        next;
    }

	@la = split(/\s+/, $l);
	for ($i=0; $i<@la-1; $i++) {
        my $rl = $la[$i];
        if ($rl =~ m/\/\//) {
            $rl =~ m/\/\/([\d.]+).*/;
            $rl = $1; 
        }
        print "color $rl $la[$#la]\n";
	    color($rl, $la[$#la]);
	}
	
#	$cc++;
#	if ($cc > $#colors) {
#	    $cc = 0;
#	}
    }
    close(Client);
}

close(NAM);

sub handler {
    local($sig) = @_;
    #print "Caught a SIG$sig\n";
    myreset();
}

sub dummy {
    local($sig) = @_;
    #print "Caught a SIG$sig\n";
    myprint("T -t $time\n");
    $time+=$step;
    alarm($interval);
}

sub mysleep {
    for (my $i=0; $i<$_[0]; $i++) {
	myprint("T -t $time\n");
	$time+=$step;
    }
}

sub myreset {
    for (my $i=0; $i<$id; $i++) {
	myprint("n -t $time -s $i -S COLOR -c black\n");
	$time+=$step;
    }
}	

sub color {
    my $name = $_[0];
    my $cc = $_[1];

    for (my $i=0; $i<$ccount; $i++) {
	if ($clients[$i][6] eq $name || $clients[$i][0] eq $name) {
	    myprint("n -t $time -s $i -S COLOR -c $colors[$cc]\n");
	    $time+=$step;
	    myprint("T -t $time\n");
	    $time+=$step;
	    last;
	}
    }

    for (my $i=0; $i<$pcount; $i++) {
	if ($proxies[$i][6] eq $name || $proxies[$i][0] eq $name) {
	    my $id = $i+$ccount;
	    myprint("n -t $time -s $id -S COLOR -c $colors[$cc]\n");
	    $time+=$step;
	    myprint("T -t $time\n");
	    $time+=$step;
	    return;
	}
    }

}

sub show {
    my @list = @{$_[0]};
    my $shape = $_[1];
    my $count = $_[2];

    for (my $i=0; $i<$count; $i++) {
	$x = $list[$i][2] + 130;
	$y = ($list[$i][1]-30)*3;
# -b $list[$i][6]
	myprint("n -t * -s $id -x $x -y $y -z 2.5 -v $shape -c black -b $list[$i][6] -w\n");
	$id++;
    }
}

sub myprint {
    print NAM "$_[0]";
    #print STDERR "$_[0]";
    NAM->autoflush(1);
}

sub setup {
    myprint("V -t * -v 1.0a5 -a 0\n");
    myprint("W -t * -x 50 -y 50\n");
}

sub readData {
    my ($l1, $l2, @a);

    open(CLOC, "<clients");
    while ($l2 = <CLOC>) {

	@a = split(/\s+/, $l2);
	if ($a[1] eq "?" ||
	    $a[2] eq "?") {
	    # bad coordinates
	    next;
	}

	for (my $i=0; $i<@a; $i++) {
	    $clients[$ccount][$i] = $a[$i];
	}
	$ccount++;
    }
    close(CLOC);


    open(PLOC, "<proxies");
    while ($l2 = <PLOC>) {

	@a = split(/\s+/, $l2);
	if ($a[1] eq "?" ||
	    $a[2] eq "?") {
	    # bad coordinates
	    next;
	}

	for (my $i=0; $i<@a; $i++) {
	    $proxies[$pcount][$i] = $a[$i];
	}
	$pcount++;
    }
    close(PLOC);
}
