#!/usr/bin/perl

# usage: test-all.pl hosts

use warnings;
use strict;
use autodie;
use File::Slurp;
use Data::Dump qw(dump);

chdir '/srv/sysadmin-cookbook/recepies/netpipe-tcp/';

my @hosts = read_file 'hosts';
@hosts = map { chomp; $_ } @hosts;
warn "hosts = ",dump(@hosts);

foreach my $host ( @hosts ) {
	chomp($host);

	my @test;

	foreach my $to ( @hosts ) {
		next if -s "$host/$to.np";
		warn "start NPtcp on $to\n";
		system "ssh $to NPtcp &";
		push @test, $to;
	}

	warn "# missing ", dump(@test);

	open(my $ssh, '|-', "ssh $host xargs -i NPtcp -h {} -u 1048576 -o /tmp/{}.np");
	foreach my $to ( @test ) {
		warn "TEST from $host to $to\n";
		print $ssh "$to\n";
	}
	close($ssh);

	system "rsync -v $host:/tmp/*.np $host/";
}
