#!/usr/bin/perl -w

use config;
use strict;

my @arr = split / /, $viewerArgList;
my $LIBRA_ABONEVIEWERARGS = "";
while (defined($arr[0])) {
    my $str = shift(@arr);
    if ($LIBRA_ABONEVIEWERARGS eq "") {
        $LIBRA_ABONEVIEWERARGS = "S=$str";
    } else {
        $LIBRA_ABONEVIEWERARGS = "$LIBRA_ABONEVIEWERARGS S=$str";
    }
}

$LIBRA_ABONEVIEWERARGS = "$LIBRA_ABONEVIEWERARGS S=./proxy";
@arr = split / /, $proxyArgList;
while (defined($arr[0])) {
    my $str = shift(@arr);
    $LIBRA_ABONEVIEWERARGS = "$LIBRA_ABONEVIEWERARGS S=$str";
}

my @CMD = 
  (
   # hostname, login, command

   # SLP DA
   "$hostCTRL", "$useraccount", "cd $LIBRA_PATH/libra/bin; LIBRA_PATH=$LIBRA_PATH LIBRA_GNPMONITOR=$LIBRA_GNPMONITOR ./startslpd.pl dance ; echo kill SLPD with stopslpd.pl! ; bash",

   # ESM synth
   "$hostCTRL", "$useraccount", "cd $LIBRA_PATH/libra/bin; LIBRA_PATH=$LIBRA_PATH LIBRA_GNPMONITOR=$LIBRA_GNPMONITOR LIBRA_ABONEVIEWERARGS=\\\"$LIBRA_ABONEVIEWERARGS\\\" ./esmsynth 30002 $hostCTRL ; echo esmsynth terminated ; bash",

   # synth
   "$hostCTRL", "$useraccount", "cd $LIBRA_PATH/libra/bin; LIBRA_PATH=$LIBRA_PATH LIBRA_GNPMONITOR=$LIBRA_GNPMONITOR LIBRA_SOLARISHOST=$LIBRA_SOLARISHOST ./synth 35000 $hostCTRL ; echo synth terminated ; bash",
   
  );

DoDemo();

while (1) {
    if (wait == -1) {
        exit(0);
    }
}

sub DoDemo {
  while (1) {
    my $hostName = shift(@CMD);
    if (!defined($hostName)) { last; }
    
    my $user = shift(@CMD);
    my $cmd = shift(@CMD);
    
    System("xterm -e bash -c \"$cmd\"");
  }
}

sub System {
    my($cmd) = $_[0];
    if ($DISABLE_SYSTEM) {
        print STDOUT "would execute $cmd\n";
    } else {
        print STDOUT "execute $cmd\n";
        my $pid;
        if ($pid = fork) {
        } elsif (defined($pid)) {
            system($cmd);
            exit(0);
        } else {
            die "fork failed!\n";
        }
    }
}

