#!/usr/bin/perl
# $Id: cmuStats,v 1.2 2001/07/18 01:31:07 jeffb Exp $
# Cobalt Networks, Inc http://www.cobalt.com
# Copyright 2001 Sun Microsystems, Inc.  All rights reserved.

# If you are not toor, go away :)
die "You must run this script as root\n" if ($< != 0);

require Getopt::Std;
my $opts = {};
Getopt::Std::getopts('f:l:h', $opts);

# Do this b4 use and require to save time
if(defined($opts->{h})) { printUsage(); }

use lib "/usr/cmu/perl";
require CmuCfg;
use TreeXml;
use strict;

my $conf = CmuCfg::mapOpts('stats', $opts);
if(!defined($conf->{cmuConfig})) {
	$conf->{cmuConfig} = "/etc/cmu/cmuConfig.xml";
}


my @stats;


@stats = getStats("/home/cmu/cmu.log");
foreach my $stat (@stats) {
	use Data::Dumper;
	print Dumper($stat);
}

exit 0;

sub getStats
{
	my $file = shift || return;
	my $data = {};
	my @arr;

	open(FH, "< $file") || die "Cannont open log file: $file:$!\n";
	my $phase = 0;
	while(my $line = <FH>) {
		if($line =~ /INFO: (\S+) started on (.*)$/) {
			if($phase eq 1) {
				warn "Incomplete run detected, ignoring\n";
				$data = {};
			}	
			my $name = `basename $1`;
			chomp($name);
			$data->{type} = $name;
			$data->{start} = $2;
			$phase = 1;
		} elsif($line =~ /INFO: (\S+) end at (.*)$/) {
			$data->{end} = $2;
			push(@arr, $data);
			$data = {};
			$phase = 0;
		} elsif($line =~ /INFO: We imported (.*)$/) {
			$data->{exported} = $1;
		}
			
	}
	close(FH);

	return @arr;
}

sub printUsage
# Print the help message
# Side Effect: exists the program in an error state
{
    print <<EOF;
usage:   cmuStats [OPTION]
         -f File to print the stats to
         -l Location of the log file, default is /home/cmu/cmu.log
         -h help, this help text
EOF
    exit 1;
}
