#!/usr/bin/perl

my $hostname = `hostname`;
chomp $hostname;

($suffix, $rest) = $hostname =~ m/d-(\d+)\.cs\.wisc\.edu/;

print "$suffix\n";
my $realsuffix = $suffix + 0;
printf "real suffix is $realsuffix\n";
my $partner = sprintf("d-%02d", $realsuffix - 100);
print "My partner is $partner\n";
my $curTime = `date`;
print "Start time is $curTime";

my $alexdir = "/scratch/arasin/cacm_data";
my $andydir = "/scratch/pavlo/cacm_data";

`mkdir -p $alexdir`;
`mkdir -p $andydir`;

my %md5sums;

foreach $file ("SortGrep10GB", "SortGrep20GB", "SortGrep40GB", "SortGrep535MB") {

my $cmd = "scp " . $partner . ":" . $andydir . "/" . $file . " $andydir" . "/" . $file;
print "$cmd\n";
my $sshresults = `$cmd`;
print $sshresults;

my $verifyCmd = "ssh $partner md5sum -b $andydir" . "/" . $file;
print "$verifyCmd\n";
my $result = `$verifyCmd`;
print "got back $result\n";
($md5sum, $name) = split(' ', $result);
print "storing $md5sum in $file\n";
$md5sums{$file} = $md5sum;

my $localverifyCmd = "md5sum -b $andydir" . "/" . $file;
my $localresult = `$localverifyCmd`;
print "got back $localresult\n";


my $localfile = $andydir . "/" . $file;
if(-e $localfile) {
	($localmd5sum, $localname) = split(' ', $localresult);
	if($md5sums{$file} eq $localmd5sum) {
		print "COMPARED: $md5sums{$file} and $localmd5sum\n"; 
		print "VERIFIED\t$hostname\t$andydir\t$file\n";
	} else {
		print "NOCOMPARE: file doesn't exist";
		print"FAILED\t$hostname\t$andydir\t$file\n";
	} 
} else {
	print"FAILED\t$hostname\t$andydir\t$file\n";
}
}

exit;

#foreach $file ("Rankings.dat", "UserVisits.dat", "URLs") {
foreach $file ("Rankings.dat", "URLs") {

my $cmd = "scp " . $partner . ":" . $alexdir . "/" . $file . " $alexdir" . "/" . $file;
print "$cmd\n";
my $sshresults = `$cmd`;
print $sshresults;

my $verifyCmd = "ssh $partner md5sum -b $alexdir" . "/" . $file;
print "$verifyCmd\n";
my $result = `$verifyCmd`;
print "got back $result\n";
($md5sum, $name) = split(' ', $result);
print "storing $md5sum in $file\n";
$md5sums{$file} = $md5sum;

my $localverifyCmd = "md5sum -b $alexdir" . "/" . $file;
my $localresult = `$localverifyCmd`;
print "got back $localresult\n";

my $localfile = $alexdir . "/" . $file;
if(-e $localfile) {
	($localmd5sum, $localname) = split(' ', $localresult);
	if($md5sums{$file} eq $localmd5sum) {
		print "COMPARED: $md5sums{$file} and $localmd5sum\n"; 
		print "VERIFIED\t$hostname\t$alexdir\t$file\n";
	} else {
		print"FAILED\t$hostname\t$alexdir\t$file\n";
	} 
} else {
	print "NOCOMPARE: no such file\n";
	print"FAILED\t$hostname\t$alexdir\t$file\n";
}
}

my $endTime = `date`;
print "End time is $endTime";
