#!/usr/bin/env perl

# The input arguments to this script are: 
# $1 = location of Xyce binary
# $2 = location of xyce_verify.pl script
# $3 = location of compare script 
# $4 = location of circuit file to test
# $5 = location of gold standard prn file

# If Xyce does not produce a prn file, then we return exit code 10.
# If Xyce succeeds, but the test fails, then we return exit code 2.
# If the shell script fails for some reason, then we return exit code 1.
# Otherwise we return the exit code of compare or xyce_verify.pl

# Since the shell script runs Xyce and the comparison program, it is
# responsible for capturing any error output from Xyce and the STDOUT & STDERR
# from the comparison program.  The outside script, run_xyce_regression,
# expects the STDERR output from Xyce to go into $CIRFILE.err, the STDOUT
# output from comparison to go into $CIRFILE.prn.out and the STDERR output from
# comparison to go into $CIRFILE.prn.err.  


$CIR1 = "VSWITCH_NOIC.cir";
$CIR2 = "VSWITCH_ON_OFF.cir";
$searchstring = "Number Jacobians Evaluated:";
$goldval1 = "3";
$goldval2 = "2";

$XYCE=$ARGV[0];
#$XYCE_VERIFY=$ARGV[1];
#$XYCE_COMPARE=$ARGV[2];
#$CIRFILE=$ARGV[3];
#$GOLDPRN=$ARGV[4];

$CMD="$XYCE $CIR1 > $CIR1.out 2> $CIR1.err";
if (system("$CMD") != 0) { print "Exit code = 10\n"; exit 10; }
$CMD="$XYCE $CIR2 > $CIR2.out 2> $CIR2.err";
if (system("$CMD") != 0) { print "Exit code = 10\n"; exit 10; }

open(OUT1,"$CIR1.out");
while ($line = <OUT1>)
{
  if ($line =~ m/$searchstring/)
  {
    @linelist = split(" ",$line);
    $val1 = $linelist[3];
  }
}
close(OUT1);

open(OUT2,"$CIR2.out");
while ($line = <OUT2>)
{
  if ($line =~ m/$searchstring/)
  {
    @linelist = split(" ",$line);
    $val2 = $linelist[3];
  }
}
close(OUT2);

if ($val1 != $goldval1)
{ $failure = 1; }

if ($val2 != $goldval2)
{ $failure = 1; }

if ($failure)
{
  print "Exit code = 2\n"; exit 2;
}

# At this point the test has passed, so lets remove the Xyce error output.
`rm -f $CIR1.err`; 
`rm -f $CIR2.err`; 

print "Exit code = 0\n"; exit 0;


