#!/spec/cpu2006/bin/specperl
#!/spec/cpu2006/bin/specperl -d
#!/usr/bin/perl
#
#  extract_config - a tool for extracting encoded config files from raw
#                   result files
#  Copyright (C) 1999-2006 Standard Performance Evaluation Corporation
#   All Rights Reserved
#
#  Author: Cloyce D. Spradling
#
# $Id: extract_config 4299 2006-05-30 13:05:40Z cloyce $

use strict;
require 'vars.pl';
require 'util.pl';

use vars qw(%config $processed %lastidx $printed);

$processed = 0;
$printed   = 0;
%config = ( 'raw' => [], 'orig' => []);
%lastidx = ( 'raw' => -1, 'orig' => -1);

# We'll use Perl's magic <> so that files can be piped in through stdin
# or given on the command line.
# There's no way to know what the config file should be called, so they'll
# just be output on stdout separated by a line of comment characters (if
# necessary).
# This will process any number of files.

while (<>) {
    tr/\015\012//d;
    next unless /^(?:spec\.${main::lcsuite}\.)?(orig|raw)config(\d+): (\S+)$/o;
    my ($tmpidx, $what, $data) = ($2+0, $1, $3);
    if ($tmpidx < $lastidx{$what}) {
	# This must be a new config file!
	foreach my $type (qw(raw orig)) {
	    if (@{$config{$type}}) {
		$processed++;
		# First, kick the old one out
		output_config(join('', @{$config{$type}}), $type);
	    }
	}

	# Reset state
	%config = ( 'raw' => [], 'orig' => []);
    }
    $lastidx{$what} = $tmpidx;
    $config{$what}->[$lastidx{$what}] = $data;
}

foreach my $type (qw(raw orig)) {
    if (@{$config{$type}}) {
	$processed++;
	# First, kick the old one out
	output_config(join('', @{$config{$type}}), $type);
    }
}

sub output_config {
    my ($encconfig, $what) = @_;

    my $cfg = decode_decompress($encconfig)."\n";
    return unless defined($cfg) && $cfg =~ /\S/;

    $printed++;
    if ($printed > 1) {
      print "#\n# --- CUT HERE --- CUT HERE --- CUT HERE --- CUT HERE --- CUT HERE ---\n#\n";
    }

    if ($what eq 'orig') {
	print "#\n# Original config file:\n";
    }
    print $cfg."\n";
}
