#!/usr/bin/perl
use strict;
use warnings;

if($#ARGV < 2)
{
	print("Usage: ./churngraph <testruntime> <numberofnodes> <initial nodelifetime>\n");
	exit(0);
}
my ($testruntime, $numberofnodes, $initialNodetime) = @ARGV;

#this host will run ./server 5555
my $KEYSERVERHOST = "marrow";
my $SOMECSILMACHINE = "bird"; #this is needed to run the matlab script
my $nodesperrun = $numberofnodes;
my $lifetimeFile = "expchurnfile.txt";
my $noderuntime = $initialNodetime;

my $matlabinputfile = "matlabinputfile.txt";
my $matlaboutputfile = "matlaboutputfile.txt";	
my $matlabCommand = "matlab -nodisplay -nosplash -nojvm -nodesktop <$matlabinputfile >$matlaboutputfile";
my $numberOfLifeEntries = $nodesperrun + 1;
my $headCut = $numberOfLifeEntries + 20 - 2;
my $tailCut = $numberOfLifeEntries + 20 - 21;
$headCut = int($headCut);
$tailCut = int($tailCut);
`rm -f $matlabinputfile`;
`rm -f $matlaboutputfile`;
`echo "format long e" >$matlabinputfile`;
`echo "exprnd($noderuntime, [$numberOfLifeEntries 1])" >>$matlabinputfile`;
print "executing $matlabCommand \n";
print "Will then execute, cat $matlaboutputfile | head -$headCut | tail -$tailCut > $lifetimeFile\n";
`ssh $SOMECSILMACHINE $matlabCommand`;
`cat $matlaboutputfile | head -$headCut | tail -$tailCut > $lifetimeFile`;
