#!/usr/bin/perl
#----------------------------->  Perl - script  <-----------------------------#
#- Copyright (C) 199x by International Computer Science Institute            -#
#- This file is part of the GNU Sather package. It is free software; you may -#
#- redistribute  and/or modify it under the terms of the  GNU General Public -#
#- License (GPL)  as  published  by the  Free  Software  Foundation;  either -#
#- version 2 of the license, or (at your option) any later version.          -#
#- This  program  is distributed  in the  hope that it will  be  useful, but -#
#- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY -#
#- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/GPL for more details.        -#
#- The license text is also available from:  Free Software Foundation, Inc., -#
#- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     -#
#------------->  Please email comments to <bug-sather@gnu.org>  <-------------#

# 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);
}
