#!/usr/local/bin/perl
if (@ARGV >= 4) {
  $ttype = $ARGV[0];
  $minhosts = $ARGV[1];
  $numhosts = $ARGV[2];
  $numtests = $ARGV[3];
} else {
  @groups = &get_groups;
  print("\n   Usage: run_e [group] [Min Hosts] [Max Hosts] [Num tests]\n\n");
  print("\tWhere group is one of:\n\t    @groups\n");
  die "\n";    
}

@hosts = &get_hosts($ttype, $numhosts);
if ($hosts[0] eq "") {
  die "No such test\n\n";
}

chop($host = `hostname`);  
print("\nEvent master started on $host.\n\n");

if ($host ne "ocarina") {
  system("rsh ocarina barrier -s $ARGV[4] > /dev/null &");
} else {
  system("barrier -s $ARGV[4] > /dev/null &");  
}
    
if (&is_running("barrier ") == 0) {
  die "\nPlease start event master first\n\n";
}

$testN = 0;
while ($testN++ < $numtests) {
  print("Test #$testN:\n");
  $hostN = $minhosts - 1;

  while ($hostN++ < $numhosts) {
    print("barrier -- -n $hostN\n");
    $n = 0;

    for ($n=0; $n < $hostN; $n++) {
      $host = $hosts[$n];
      $user = "";

      if (index($host,"edu") >= 0) {
        if (index($host,"uky") < 1) {
          $user = "-l griff";
        }
      }
      $f[$n] = fork;
      unless($f[$n]) {
	printf("  %25s   %s\n","[".$host."]", "started.");
	system("rsh $user $host barrier -n $hostN > /tmp/$host.run");
	exit 0;
      }
    }

    foreach $pid (@f) {
       waitpid($pid,0);
    }

    $_ = &get_time(@hosts);
    $outfile = "SEC_" . $ttype . "." . "$hostN".".out";
    system("echo $_ >> $outfile");
    printf("\nElapsed time: %.5f seconds (average)\n\n",$_);
  }
}

kill_proc("barrier \-s|rsh");


sub get_time {
  local (@times, $n, $total, $ave);
  print("\n");
  foreach $host (@_) {
    open(H, "/tmp/$host.run") || last;
    while (<H>) {
      chop;
      if (/^(Elapsed time:)/) {
        s/Elapsed time: //;
	printf("  %25s   %s\n","[".$host."]", $_);
	s/ \w*.*//;
	$times[$n++] = $_;
	$total += $_;
	last;
      }	      
    }
    close(H);
    unlink("/tmp/$host.run");
  }
  if ($n != 0) {
  $ave = $total/($n);
  }
  $ave;
}

sub kill_proc {
  open(PS, '/bin/ps -ef |') || warn "can't run ps\n";
  $head = <PS>;
  $COMMAND = index($head, "CMD") if $COMMAND <= 0;

  while (<PS>) {
    $pid = substr($_,9,6) + 0;
    $cmd = substr($_,$COMMAND);
    chop($cmd);
    if ($cmd =~ /^($_[0])/) {
      print("Killing pid $pid: $cmd\n");
      kill 9, $pid;
    }
  }
  close(PS);
  print("\n");
}

sub is_running {
  $found_ = 0;
  $proc_str = $_[0];  
  open(PS, '/bin/ps -ef |') || warn "can't run ps\n";
  $head = <PS>;
  $COMMAND = index($head, "CMD") if $COMMAND <= 0;
  while (<PS>) {
    $pid = substr($_,9,6) + 0;
    $cmd = substr($_,$COMMAND);
    chop($cmd);

    if (index($cmd,$proc_str) >= 0) {
      $found_ = 1;
      last;
    }
  }
  close(PS);
  $found_;
}


sub get_hosts {
  local(@h);
  chop($rcFile =`echo \$HOME`);
  $rcFile .= "/.SECrc";
  open(HOST, $rcFile) || die "can't find $rcFile\n";

  while (<HOST>) {
    s/\s+//;
    s/=//;
	
    if ($_ eq $_[0]) {
      last;
    }
  }

  $hostNum = 0;
  while ($host = <HOST>) {
    $host =~ s/\s+//;

    if ($host eq "") {
      next;
    }
    
    $h[$hostNum++] = $host;

    if ($hostNum == $_[1]) {
      last;
    }
  }
  close(HOST);

  @h;
}


sub get_groups {
  local(@groups, $num);
  chop($rcFile =`echo \$HOME`);
  $rcFile .= "/.SECrc";
  open(HOST, $rcFile) || die "can't find $rcFile\n";
  
  while (<HOST>) {
    s/\s+//;

    if ($_ =~ /\=/) {
      s/=//;
      $groups[$num++] = $_;      
    }
  }
  close(HOST);
  
  @groups;
}
