#!/usr/bin/perl
#
# This is a simple stub for the 'runtest' command that is used in TestLib.pm
#
# TODO: extend this to recognize parallel execution options
#

use Sys::Hostname;
my $host = hostname();

if ($host eq "qed.sandia.gov") {

# this takes an mpiexec command, and strips out number of processors
# there must be a space after the -n, e.g.: mpiexec -n 32, or mpiexec -np 32
# assume 2 processors per node
  my $p = 1;
  for (my $i=0; $i <= $#ARGV; $i++) {
    if ($ARGV[$i] =~ /-np?.*/ ) {
      if ($ARGV[$i] =~ /-np? *(\d+)/ ) {
	$p = $1;
	break;
      } else {
        $p = $ARGV[$i + 1];
      }
    }
  }
  $p = int(($p + 1) / 2);	# 2 ppn
  $p = 1 if ($p < 1);
  my $out = "qsub.$$.out";
  my $job = `(runqsub.qed $p 01:00:00 \"( @ARGV ) >& $out\")`;
  chomp $job;

  print "job=$job proc=$p out=$out\n";

  die if ($job eq '');

# poll on the job
# qstat 7445.qed >& /dev/null || echo done
# qstat 7445.qed >& /dev/null && echo running

  while (`qstat $job >& /dev/null && echo 1`) {
    sleep 30;
  }

  system("cat $out");

} else {
  system("(@ARGV) | tr \'<&>\"\' \'+++++\' 2>&1");
}
exit 0;

