#!/usr/bin/env perl

# The input arguments to this script are set up in
# Xyce_Test/TestScripts/tsc_run_test_suite and are as follows:
# $ARGV[0] = location of Xyce binary
# $ARGV[1] = location of xyce_verify.pl script
# $ARGV[2] = location of compare script (NEVER USED!)
# $ARGV[3] = location of circuit file to test
# $ARGV[4] = 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.

$XYCE=$ARGV[0];
$XYCE_VERIFY=$ARGV[1];
$XYCE_VERIFY=~ s/xyce_verify/ACComparator/;
$CIRFILE=$ARGV[3];
$GOLDPRN=$ARGV[4];

system("rm -f $CIRFILE.FD.prn");
$CMD="$XYCE $CIRFILE > $CIRFILE.out 2> $CIRFILE.err ";
$retval = system("$CMD");

if ($retval != 0) { print "Exit code = $retval\n"; exit $retval; }

$GOLDPRN =~ s/\.prn$//; # remove the .prn at the end.

if (-f "$CIRFILE.FD.prn")
{
  $CMD="$XYCE_VERIFY $GOLDPRN.FD.prn $CIRFILE.FD.prn";
  $retval = system("$CMD");

  if ($retval == 0) { $retcode = 0; }
  else { $retcode = 2; }
}
else { $retcode = 14; }

# if the main file matches the gold standard, try the "noFlip_P" circuit
# and check it against the main file's version.
$CIRFILE2=$CIRFILE;
$CIRFILE2 =~ s/.cir/_noFlip_P.cir/;
    
if ($retcode == 0)
{
    $CMD="$XYCE $CIRFILE2 > $CIRFILE2.out 2> $CIRFILE2.err ";
    $retval = system("$CMD");
    
    if ($retval != 0) { print "Exit code = 10\n"; exit 10; }
    if (-f "$CIRFILE2.FD.prn")
    {
        $CMD="$XYCE_VERIFY $CIRFILE.FD.prn $CIRFILE2.FD.prn";
        $retval = system("$CMD");
        
        if ($retval == 0) { $retcode = 0; }
        else { $retcode = 2; }
    }
    else { $retcode = 14; }
}

print "Exit code = $retcode\n"; exit $retcode;

