#! /usr/bin/perl
$bindir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/bin";
$includedir    = "/afs/cad.njit.edu/u/a/l/alexg/BSP/include";
$libdir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/lib";
$mandir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/man";






















push(@INC,$includedir);
push(@INC,$bindir);
require "bsputil";

select(STDERR); $| = 1; select(STDOUT); # no STDERR buffering, please.
($Pgm = $0) =~ s|.*/||;
$Version        = "1.3, 25th Nov 1997";
$bug_reports_to = 'bsplib-bugs@comlab.ox.ac.uk';

$ShortUsage = "\n$Pgm usage: for basic information, try the `-help' option\n";



$Verbose        = 0;
$Arch   = &backtick("$bindir/bsparch -arch");
$Arch   = &backtick("bsparch -arch")  if ($Arch eq "");
$Portnumber     = -1;
$Hostnames      = ".bsptcphosts";
@Hostnames_list = ();
$Forallhosts    = 0;




  arg: while ($_ = $ARGV[0]) {
  shift(@ARGV);
  #--------HELP------------------------------------------------
  /^-(help|man)$/     && do {&FormatManual("bsplibd.1");exit(0);};

  /^-v$/              && do {$Verbose = 1; next arg;};

  /^-all$/            && do {$Forallhosts=1; next arg;};

  /^-f$/              && do {$Hostnames= &grab_next_arg("-f");
                             next arg;};

  /^-p$/              && do {$Portnumber = &grab_next_arg("-p");
			     if (!($Portnumber =~ /^\d+$/)) {
			       printf STDERR "%s: invalid port \"%s\"\n",
					     $Pgm,$Portnumber;
			       $Status++;
			     }
			     next arg;
			     };		

  print STDERR "Unrecognised option \"",$_,"\"\n";
  $Status++;
  }
  if ($Status>0) {
    print STDERR $ShortUsage;
    exit(1);
  }
  print STDERR "$Pgm: ($Version)\n" if $Verbose;
  @Hostnames_list = &readHostNames($Hostnames);
  $cmd_d = "$bindir/$Arch/bspnowd -p 0";
  $cmd_p = "$bindir/$Arch/bspportd";
  $cmd_p.= " -p $Portnumber" if ($Portnumber>=0);
  if (!$Forallhosts) {
       &run_something($cmd_p . " & ","local port daemon");
       &run_something($cmd_d,"local daemon");
  } else {
    foreach $i (@Hostnames_list) {
      &run_something("rsh $i -n '$cmd_p &' &","remote port daemon");
    }
    $i = 1+$#Hostnames_list;
    print "$Pgm: sleeping for $i seconds before starting daemons\n";
    sleep($i);
    foreach $i (@Hostnames_list) {
      &run_something("rsh $i -n '$cmd_d &' &","remote daemon");
    }
  }
  exit(0);

