# -*- perl -*-
# write B::Stats overhead with zero program -e1 to %B::Stats::Minus::overhead
# TODO: add -f w/o sitecustomize
#       if $Config{usesitecustomize}
use File::Spec;
use Config;
my $lib = File::Spec->catfile(qw(lib B Stats Minus.pm));
my $X = $^X =~ m/\s/ ? qq{"$^X"} : $^X; 
my $redir = '2>&1';

# -qq not with 5.6
my $t = `$X -Iblib/arch -Iblib/lib -MO=-qq,Terse -e1 | $X -anl -e"print \$F[2]"`;
my $c = `$X -Iblib/arch -Iblib/lib -MB::Stats=-u -e1 $redir`;

my (%t, %c);
my $t_ops = scalar split(/\n/,$t);
$t{$_}++ for split/\n/,$t;

my $key = 'c';
for (split/\n/,$c) {
  $key = 'e' if /B::Stats static end-time:/;
  $key = 'r' if /B::Stats dynamic run-time:/;
  my($op,$n) = split/\s+/ unless /^(B::Stats|op name|files=)/;
  if (/^files=/) {
    for (split/\s+/) {
      if (/(files|ops|lines)=(\d+)/) {
	$c{$key}{"_$1"} = 0+$2;
      }
    }
    next;
  }
  $c{$key}{$op} = 0+$n if $op and $n;
}

for (keys %t) { # nextstate null enter leave w/o sitecustomize
  for my $key (qw(c e r)) {
    $c{$key}{$_} -= $t{$_} if $c{$key}{$_};
  }
}

use Data::Dumper;
open P,">",$lib or die;
print P "package B::Stats::Minus;
# Automatically generated by B::Stats Makefile.PL
# Install-time generated data, to calculate the B::Stats overhead.
#   c includes B with its 14 dependencies
#   e adds Opcodes
";
print P Data::Dumper->Dump([\%c, \%t], [qw(overhead test)]);
print P "# \$test from -e1
";
print P "1;
";
close P;
