eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# $Id: run_test.pl 84567 2009-02-23 09:29:20Z johnnyw $
# -*- perl -*-

use lib "$ENV{'ACE_ROOT'}/bin";
use PerlACE::Run_Test;

$CIAO_ROOT = "$ENV{'CIAO_ROOT'}";
$TAO_ROOT = "$ENV{'TAO_ROOT'}";
$DAnCE = "$ENV{'DANCE_ROOT'}";
$daemons_running = 0;
$daemons = 1;
@ports = ( 60001 );
@iorfiles = ( "NodeApp1.ior" );
@nodenames = ( "Node" );
$status = 0;

PerlACE::add_lib_path ('../Components');

$E = 0;
$EM = 0;

# Delete if there are any .ior files.
sub delete_ior_files {
    for ($i = 0; $i < $daemons; ++$i) {
        unlink $iorfiles[$i];
    }
}

sub kill_node_daemons {
  for ($i = 0; $i < $daemons; ++$i) {
    $Daemons[$i]->Kill (); $Daemons[$i]->TimedWait (1);
  }
}

sub kill_open_processes {
  if ($daemons_running == 1) {
    kill_node_daemons ();
  }
}

sub run_node_daemons {
  for ($i = 0; $i < $daemons; ++$i)
  {
      $iorfile = $iorfiles[$i];
      $port = $ports[$i];
      $nodename = $nodenames[$i];
      $iiop = "iiop://localhost:$port";
      $node_app = "$CIAO_ROOT/bin/ciao_componentserver";

      $d_cmd = "$DAnCE/bin/dance_node_manager";
      $d_param = "-ORBEndpoint $iiop -s $node_app -n $nodename=$iorfile -t 30";

      $Daemons[$i] = new PerlACE::Process ($d_cmd, $d_param);
      $result = $Daemons[$i]->Spawn ();
      push(@processes, $Daemons[$i]);

      if (PerlACE::waitforfile_timed ($iorfile,
                                      30) == -1) {
          print STDERR
            "ERROR: The ior $iorfile file of node daemon $i could not be found\n";
          for (; $i >= 0; --$i) {
            $Daemons[$i]->Kill (); $Daemons[$i]->TimedWait (1);
          }
          return -1;
      }
  }
  return 0;
}

if ($#ARGV == -1)
{
    opendir(DIR, ".");
    @files = grep(/\.cdp$/,readdir(DIR));
    closedir(DIR);
} else {
    @files = @ARGV;
}

foreach $file (@files) {
    delete_ior_files ();

    print "Running test for plan $file\n";

    # Invoke node daemons.
    print "Invoking node daemon\n";
    $status = run_node_daemons ();

    if ($status != 0) {
        print STDERR "ERROR: Unable to execute the node daemon\n";
        kill_open_processes ();
        exit 1;
    }

    $daemons_running = 1;

    # Invoke executor - start the application -.
    print "Launching the plan. file://NodeApp1.ior \n";
    $E = new PerlACE::Process ("simple_nm_launcher",
                               "file://NodeApp1.ior $file");

    $status = $E->SpawnWaitKill (5000);

    if ($status != 0)
    {
        print "ERROR:  simple_nm_launcher returned error status $status\n";
    }

    print "Test for plan $file succeeded\n";
    kill_open_processes ();
}


exit $status;
