#!/usr/bin/env perl

system("mkdir -p NEW DIFF");

if(@ARGV != 4) {
  print "Usage: TESTonce name input output options\n";
  exit 20;
}

$name=$ARGV[0];
$input=$ARGV[1];
$output=$ARGV[2];
$options=$ARGV[3];

my $r;

if ($^O eq 'MSWin32') {
    $r = system "..\\windump -n -t -r $input $options 2>NUL | sed 's/\\r//' | tee NEW/$output | diff $output - >DIFF/$output.diff";
    # need to do same as below for Cygwin.
}
else {
    # we used to do this as a nice pipeline, but the problem is that $r fails to
    # to be set properly if the tcpdump core dumps.
    $r = system "../tcpdump 2>/dev/null -n -t -r $input $options >NEW/$output";
    if($r != 0) {
        # this means tcpdump failed.
        open(OUTPUT, ">>"."NEW/$output") || die "fail to open $output\n";
        printf OUTPUT "EXIT CODE %08x\n", $r;
        close(OUTPUT);
        $r = 0;
    }
    if($r == 0) {
        $r = system "cat NEW/$output | diff $output - >DIFF/$output.diff";
    }
    #print sprintf("END: %08x\n", $r);
}

if($r == 0) {
  printf "    %-35s: passed\n", $name;
  unlink "DIFF/$output.diff";
  exit 0;
}
printf "    %-35s: TEST FAILED", $name;
open FOUT, '>>failure-outputs.txt';
printf FOUT "Failed test: $name\n\n";
close FOUT;
if(-f "DIFF/$output.diff") {
    system "cat DIFF/$output.diff >> failure-outputs.txt";
}

if($r == -1) {
  print " (failed to execute: $!)\n";
  exit 30;
}

# this is not working right, $r == 0x8b00 when there is a core dump.
# clearly, we need some platform specific perl magic to take this apart, so look for "core"
# too.
# In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
# a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
if($r & 127 || -f "core") {
    my $with = ($r & 128) ? 'with' : 'without';
    if(-f "core") {
        $with = "with";
    }
    printf " (terminated with signal %u, %s coredump)\n", ($r & 127), $with;
    exit ($r & 128) ? 10 : 20;
}
print "\n";
exit $r >> 8;