#!/usr/bin/env perl

use XyceRegression::Tools;

$Tools = XyceRegression::Tools->new();

# The input arguments to this script are set up in
# Xyce_Test/TestScripts/tsc_run_test_suite and are as follows:
# $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 csv file

# If Xyce does not produce the appropriate output files, then we return exit code 14.
# 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.csv.out and the STDERR output from
# comparison to go into $CIRFILE.csv.err.

$XYCE=$ARGV[0];
$XYCE_VERIFY=$ARGV[1];
$XYCE_COMPARE=$ARGV[2];
$CIRFILE=$ARGV[3];
$BADCIRFILE="bad_$CIRFILE";

$failed = 0;
$CMD="$XYCE -h > help.out 2> help.err";
$system_code = system($CMD);
if ($system_code != 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -h exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -v > version.out 2> version.err";
$system_code = system($CMD);
if ($system_code != 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -v exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -capabilities > capabilities.out 2> capabilities.err";
$system_code = system($CMD);
if ($system_code != 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -capabilities exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -license > license.out 2> license.err";
$system_code = system($CMD);
if ($system_code != 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -license exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -syntax $CIRFILE > syntax.out 2> syntax.err";
$system_code = system($CMD);
if ($system_code != 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -syntax exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -syntax $BADCIRFILE > syntax.out 2> syntax.err";
$system_code = system($CMD);
if ($system_code == 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -syntax exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -norun > norun.out 2> norun.err";
$system_code = system($CMD);
if ($system_code != 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -norun exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -count $CIRFILE > count.out 2> count.err";
$system_code = system($CMD);
if ($system_code != 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -count exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$CMD="$XYCE -junk > junk.out 2> junk.err";
$system_code = system($CMD);
if ($system_code == 0) {
  $exit_code = $system_code >> 8;
  $signal_id = $system_code & 127;
  print STDERR "Xyce -junk exited with exit code $exit_code signal $signal_id\n";
  ++$failed;
}

$retcode = 0;
if ($failed != 0) {
  $retcode = 2;
}

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