#!/usr/bin/env perl

use XyceRegression::Tools;

$Tools = XyceRegression::Tools->new();
#$Tools->setDebug(1);
#$Tools->setVerbose(1);

# 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.

# 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.  

use Getopt::Long;

# these search strings are supposed to occur one right after the other in the
# error output.
@searchstrings = ( "Sensitivity output cannot be written in requested format, using standard format"
);

&GetOptions( "verbose!" => \$verbose );
$XYCE=$ARGV[0];
#$XYCE_VERIFY=$ARGV[1];
#$XYCE_COMPARE=$ARGV[2];
$CIRFILE=$ARGV[3];
#$GOLDPRN=$ARGV[4];

$XYCE="$XYCE -error-test";

$retval = $Tools->runAndCheckError($CIRFILE,$XYCE,@searchstrings);
if ($retval != 0)
{
  print "Exit code = $retval\n";
  exit $retval
}

# now make sure that the netlist file did not get overwritten.  This was a specific
# error that happened during early development of this feature.
$CMD_DIFF="diff  $CIRFILE $CIRFILE.backup > $CIRFILE.backup.out 2> $CIRFILE.backup.err";
if (system("$CMD_DIFF") != 0) 
{ 
  $failure=1; 
  print "Circuit file corrupted or overwritten by .SENS output!\n";
}

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

