package Monitor::statusfile;

require 5.002;
use FileHandle;

use strict;

@Monitor::statusfile::ISA = qw(Monitor::Monitor);

# create the objects + memory space needed
sub new
{

    my ($this) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);

    $self->{"htable"} = [];

    return($self);
}


sub add_check 
{
    my( $self, $file ) = @_;
    my( $htable ) = $self->{"htable"};

    $file = BigSister::common::find_file( $file );
    # add this info to the configuration array
    print "adding statusfile $file\n" if( $BigSister::common::dl>4 );
    push( @$htable, { "file" => $file } );
}



sub flush {
    my( $self, $cmd, $data ) = @_;

    print "statusfile::flush( $cmd, $data )\n" if( $BigSister::common::dl>5 );
    return unless( $data =~ /^([^\s]+)\s+([a-z]+)/ );
    my( $item, $color, $text ) = ($1,$2,$');
    $text =~ s/^.*?\d\d:\d\d.*?\d{4}\s//; 
    $color = $BigSister::common::status_codes{$color};
    print "item: $item, color: $color, text: $text\n" if( $BigSister::common::dl>6 );
    return unless( defined $color );
    $self->report( $item, $color, $text );
}


sub statusfile {
    my( $self, $file ) = @_;
    my $fh = FileHandle->new();
    my( $buff, $bcmd );

    open( $fh, "<$file" );
    $buff = $bcmd = ""; 
    while( <$fh> ) {
	chomp;
	if( /^(status|page)\s+(.*)$/ ) {
	    my( $cmd, $data ) = ($1,$2);
	    $self->flush( $bcmd, $buff ) if( $bcmd );
	    $bcmd = $cmd;
	    $buff = $data;
	}
	else {
	    $buff .= "|>".$_;
	}
    }
    $self->flush( $bcmd, $buff ) if( $bcmd );
    close $fh;
}




sub check
{
    my ($self) = @_;
    my $check;

    foreach $check (@{$self->{"htable"}}) {
	$self->statusfile( $check->{"file"} );
    }
}

1;
