#!/usr/bin/env perl

package RawFileCommon;
use strict;
use XyceRegression::Tools;
use File::Basename;

sub verifyRawFile
{
  my ($XYCE,$XYCE_VERIFY,$CIRFILE,$GOLDRAW,$absTol,$relTol,$zeroTol)=@_;

  my $Tools = XyceRegression::Tools->new();
  my $dirname = dirname($XYCE_VERIFY);
  my $fc = "$dirname/file_compare.pl";
  my $XYCE_RAW_READ = $XYCE_VERIFY;
  $XYCE_RAW_READ =~ s?xyce_verify\.pl?spice_read.py?;

  my $retcode = 0;
  my $xyceexit=0;

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

  system("rm -f $CIRFILE.raw $CIRFILE.err $CIRFILE.raw.gold-info $CIRFILE.raw.info");
  system("rm -f $CIRFILE.prn");

  # run Xyce and check out the output files
  my $CMD="$XYCE -r $CIRFILE.raw -a $CIRFILE > $CIRFILE.out 2>$CIRFILE.err";

  if (system($CMD) != 0) {
    `echo "Xyce EXITED WITH ERROR! on $CIRFILE" >> $CIRFILE.err`;
    $xyceexit=1;
  }

  if ($xyceexit!=0) {print "Exit code = 10\n"; return 10;}

  if ( -f "$CIRFILE.prn" ) {
      print STDERR "Extra output file $CIRFILE.prn\n";
      $xyceexit=14;
  }

  if ( !(-f "$CIRFILE.raw")) {
      print STDERR "Missing output file $CIRFILE.raw\n";
      $xyceexit=14;
  }

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

  #use Xyce Verify
  if (system("$XYCE_RAW_READ $GOLDRAW.raw | grep -v 'The file:' | grep -v 'Date:' | grep -v 'Vector-Length' > $CIRFILE.raw.gold-info 2>$CIRFILE.raw.gold-out") != 0) {
      print STDERR "spice_read failed on file $GOLDRAW.raw, see $CIRFILE.raw.gold-out\n";
      $retcode = 2;
  }
  if (system("$XYCE_RAW_READ $CIRFILE.raw | grep -v 'The file:' | grep -v 'Date:' | grep -v 'Vector-Length' > $CIRFILE.raw.info 2>$CIRFILE.raw.out") != 0) {
      print STDERR "spice_read failed on file $CIRFILE.raw, see $CIRFILE.raw.out\n";
      $retcode = 2;
  }

  $CMD="diff -bi $CIRFILE.raw.gold-info $CIRFILE.raw.info > $CIRFILE.raw.out";
  if (system("$CMD") != 0) {
      print STDERR "Verification failed on file $CIRFILE.raw, see $CIRFILE.raw.out\n";
      $retcode = 2;
  }
  else
  {
    print "passed verification by xyce_verify\n";
  }
   
  # now test the .RAW file output. Test that the values and strings in the 
  # match to the required tolerances
  if (system("grep -v 'Date:' $GOLDRAW.raw > $CIRFILE.raw.gold-filtered 2>$CIRFILE.raw.gold-filtered.out") != 0) {
      print STDERR "Date line not found in file $CIRFILE.raw.gold-filtered, see $CIRFILE.raw.gold-filtered.out\n";
      $retcode = 2;
  }
  if (system("grep -v 'Date:' $CIRFILE.raw > $CIRFILE.raw.filtered 2>$CIRFILE.raw.filtered.out") != 0) {
      print STDERR "Date line not found in file $CIRFILE.raw, see $CIRFILE.raw.filtered.out\n";
      $retcode = 2;
  }

  if ($retcode != 0) 
  {
    print "Exit code = $retcode\n"; 
    return $retcode;
  }
  else
  {
    `$fc $CIRFILE.raw.filtered $CIRFILE.raw.gold-filtered $absTol $relTol $zeroTol > $CIRFILE.errmsg.out 2> $CIRFILE.errmsg.err`;
    $retcode=$? >> 8;
  }

  if ( $retcode != 0 )
  {
    print "test Failed comparison of Gold and test .RAW files!\n";
  }
  else
  {
    print "Passed file comparison of .RAW files\n";
  }

  print "Exit code = $retcode\n"; return $retcode;
}

#
# We need the 1; at the end because, when Perl loads a module, Perl checks
# to see that the module returns a true value to ensure it loaded OK. 
#
1;

#

