#!/usr/bin/perl
$base = $ENV{'ROOTDIR'};
#require "$base/bin/conf.pl";
require "sys/syscall.ph";

# begin user modified variables

@machines = ("s4","s5");
$distccNetwork = "10.1.1.0/24";
$sudo = 1;
$haveFPING = 1;

$distccbinary="ccache distcc";

# end user modified variables


$prefix="";
# as long as we are not root
$distcc_hosts = "";
$option = '';

if (@ARGV[0] eq '-ssh') {
  $prefix = '@';
  $option = shift;
}else {
 print "Pass option '-ssh' if you want to securely compile (slower though)\n\n";
 print "Pass icc,gcc,icpc,xlc,g++ or xlC if you want to use another compiler\n\n";
}

if ($haveFPING) {
 foreach $mac (@machines) {
  if (&reachable($mac)) {
  push @DISTCC_HOSTS,$mac ;
   $distcc_hosts .= "$prefix$mac ";
  }
 }
}

print "hosts: @DISTCC_HOSTS";
$ENV{'DISTCC_HOSTS'} = $distcc_hosts;

if ($> != 0){

 if ($sudo) {
   system("sudo ntpdate $timeServer");
 } 

 foreach $machine (@DISTCC_HOSTS) {
   $exec = "ssh $machine setupdistcc $option";
   print "Starting remote daemon: $exec\n";
   system($exec);
 }

}

$numMachines = 3*@DISTCC_HOSTS; # get number of machines

$TIMEVAL_T = "LL";
                                                                                
$done = $start = pack($TIMEVAL_T, ());

syscall(&SYS_gettimeofday, $start, 0) != -1 or die "gettimeofday: $!";

# work

$comptype = "";
if (@ARGV[0] =~ /icc|gcc|icpc|xlc|g\+\+|xlC|ifort|ifc|xlf|g77|g90|g95|pgcc|pgCC/) {
 	$comptype = shift; 
}

system("make -j$numMachines CC='$distccbinary $comptype' @ARGV");

#end work
syscall( &SYS_gettimeofday, $done, 0) != -1 or die "gettimeofday: $!";
                                                                                
@start = unpack($TIMEVAL_T, $start);
@done  = unpack($TIMEVAL_T, $done);
                                                                                
# fix microseconds
for ($done[1], $start[1]) { $_ /= 1_000_000 }
                                                                                
$delta_time = sprintf "%.4f", ($done[0]  + $done[1]  ) - ($start[0] + $start[1] );
                                                                                
printf("Compiling Completed in $delta_time seconds\n");



if ($> != 0) {
 foreach $machine (@machines) {
   $exec = "ssh $machine killall distccd";
   print "Cleaning up: $exec\n";
   system($exec);
 }
}

sub reachable {
  my $host = shift;
  `fping -a -r 2 $host`;
   # if not 0
  if (! $?) {
    return 1;
   }
  return 0;

   my $timeout = shift;
   if ($timeout eq "") {
     $timeout = 6;
   }
   my $transport = $machines{$host};
   if ($transport eq "") {
     $transport = "ssh";
   }
   my $evalLine = "$transport $host uname";
 eval {
        local $SIG{ALRM} = sub { die "alarm\n" };       # NB \n required
        alarm $timeout;
        system($evalLine);
        alarm 0;
    };
    if ($@ && $@ ne "alarm\n"){die;}       # propagate errors
    if ($@) {
        return 0; # alarm went off
    } else {
        return 1; # alarm didn't go off - successful
    }
}
