#!/usr/bin/perl
#
# This script comes from the original TAM distribution 
# (TCP/IP Active Message Library). It has not been tested
# with this version of the library and may or may not
# work on your machine.
#
# perl script to start program on multiple hosts defined in
# configuration file
#
# Syntax: prun <program> <arguments>
#
# turn in command buffering
select(STDOUT);
$| = 1;
$default_config = "./tcpam_config";
$user_config = $ENV{"TCPAM_CONFIG"};
$user_program = shift(@ARGV) || die "Need to specify program to run\n" ;
# if user has not defined configuration file name, use default 
if (!$user_config) {
  $user_config = $default_config;
}
open(CONFIG_FILE, "$user_config") || die "Cannot open $user_config";
$num_node = <CONFIG_FILE>;
chop($num_node);
$port = <CONFIG_FILE>;
$n = 0;
while (<CONFIG_FILE>) {
  @host_names = split(/ /);
  chop($host_names[1]);
  push(@node_name, $host_names[1]);
  $n = $n + 1;
}
close(CONFIG_FILE);
if ($n < $num_node) {
    die "User wants to use $num_node node(s) but only $n node(s) defined in configuration file\n";
}
print "Starting program $user_program on $num_node nodes\n";
for ($i = 0; $i < $num_node; $i++) {
   print "rsh $node_name[$i] $user_program @ARGV &\n";
   system "rsh $node_name[$i] $user_program @ARGV &";
#   sleep(5);
}
