#!/usr/local/bin/perl

#
# dbstripextraheaders
# Copyright (C) 1991-1998 by John Heidemann <johnh@isi.edu>
# $Id: dbstripextraheaders,v 1.13 1999/11/23 02:04:29 johnh Exp $
#
# This program is distributed under terms of the GNU general
# public license, version 2.  See the file COPYING
# in $dblibdir for details.
#

sub usage {
    print <<END;
usage: $0
    Removes all but the first header line.
    Extra headers sometimes accumulate from concatanated tables.

It also cleans up files with bogus comments before the header.

Sample input:
#h      experiment      mean    stddev  pct_rsd conf_range      conf_low       conf_high        conf_pct        sum     sum_squared     min     max     n
ufs_mab_sys     37.25 0.070711 0.18983 0.6353 36.615 37.885 0.95 74.5 2775.1 37.2 37.3 2
#  | /home/johnh/BIN/DB/dbmultistats experiment duration
#h      experiment      mean    stddev  pct_rsd conf_range      conf_low       conf_high        conf_pct        sum     sum_squared     min     max     n
ufs_rcp_real    271.2 9.4752 3.4938 85.13 186.07 356.33 0.95 542.4 1.4719e+05 264.5 277.9 2
#  | /home/johnh/BIN/DB/dbmultistats experiment duration

Sample command:
cat data.jdb | dbstripextraheaders

Sample output:
#h      experiment      mean    stddev  pct_rsd conf_range      conf_low       conf_high        conf_pct        sum     sum_squared     min     max     n
ufs_mab_sys     37.25   0.070711        0.18983 0.6353  36.615  37.885  0.95   74.5     2775.1  37.2    37.3    2
#  | /home/johnh/BIN/DB/dbmultistats experiment duration
ufs_rcp_real    271.2   9.4752  3.4938  85.13   186.07  356.33  0.95    542.4  1.4719e+05       264.5   277.9   2
#  | /home/johnh/BIN/DB/dbmultistats experiment duration
#  | /home/johnh/BIN/DB/dbstripextraheaders 
END
    exit 1;
}

BEGIN {
    $dblibdir = "/usr/local/lib/jdb";
    push(@INC, $dblibdir);
}
require "$dblibdir/dblib.pl";
use DbGetopt;

my(@orig_argv) = @ARGV;
my($prog) = &progname;
my($dbopts) = new DbGetopt("?", \@ARGV);
my($ch);
while ($dbopts->getopt) {
    $ch = $dbopts->opt;
    if (0) {
    } else {
	&usage;
    };
};
$usage if ($#ARGV != -1);

($PRE_HEADER, $POST_HEADER) = 0..10;
$state = $PRE_HEADER;

while (<STDIN>) {
    if ($state == $PRE_HEADER) {
	next if (!/^$headertag_regexp/);
	&process_header($_, $headertag);
	&write_header();
	$state = $POST_HEADER;
    };
    next if (/^$headertag_regexp/);
    &pass_comments && next;
    &split_cols;
    &write_cols;
};

print "#  | $prog ", join(" ", @orig_argv), "\n";
exit 0;
