#!/usr/bin/perl

use strict;
use warnings;
use IO;

STDOUT->autoflush;

use Getopt::Std;

my %opts;
getopts( 'hn:', \%opts );

my $usage = "
$0 -h -c <loop count>
This program runs <src/tools/pgtest -n> for the number of loops specified.  It
will run through all loops, saving regression.diffs files in /tmp/failX.diff
for future analysis.  Additionally, it counts the number of time each test
fails and presents a simple summary.
   -h  Help
   -n  Number of loops to run

";

die "Usage:$usage"
    if exists $opts{ 'h' };
die "Bad Params:$usage"
    if !exists $opts{ 'n' };

my $cmd = 'sh src/tools/pgtest -n';

my %failed;
my $total_failed = 0;
my $count = 0;

for $count ( 1..$opts{ 'n' } )
   {
   my $failed = 0;
   open PIPE, "$cmd 2>&1 |";
   open FILE, "> /tmp/run${count}.out";
   open FILE_ALL, ">> /tmp/run.out";
   while( <PIPE> )
      {
      next if /^gmake/;
      print FILE $_;
      print FILE_ALL $_;
      next unless /([_\w]+)\s+\.{3}\s+FAILED/;
      my $test = $1;
      next if /ignore/;
      ++$failed{ $test };
      $failed = 1;
      }
   close PIPE;
   close FILE;
   close FILE_ALL;
   if ( $failed )
      {
      system("cp ./src/test/regress/regression.diffs /tmp/fail${count}.diffs");
      }
   else
      {
      unlink "/tmp/run${count}.out";
      }
   $total_failed += $failed;
   printf "%5d of %5d - failed %5d\n", $count, $opts{ 'n' }, $total_failed;
   }

for ( keys %failed )
   {
   print "$_ failed $failed{$_} times\n";
   }
