package Statusmon::BBLog;

use Statusmon::Statusmon;

@Statusmon::BBLog::ISA = qw(Statusmon::Statusmon);


sub new
{

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

    bless($self, $class);

    $self->register( "status" );
    $self->register( "config" );
    $self->{"statedir"} = "$BigSister::common::fs{'web'}/logs";
    return($self);
}


sub start {
    my( $self ) = @_;
    my $file = $self->getarg( "statedir" );
    if( $file ) {
	$file = BigSister::common::find_file( $file );
	$self->{"statedir"} = $file;
    }
    my $config = $self->get_feature_provider( "displayconfig" );
    if( $config ) { $self->read_config( $config->get_current_config() ) };
}
    


sub read_config {
    my( $self, $newconfig, $oldconfig ) = @_;

    if( $newconfig->{"options"}->{"BBLog"} ) {
        $self->register( "status" );
    }
    else {
        $self->register( "-status" );
    }
}


sub event {
    my( $self, $cmd ) = @_;
    my $text;
    my $logdir = $self->{"statedir"};

    print STDERR "Statusmon::BBLog got an event\n" if( $BigSister::common::dl > 6 );
    if( $cmd->{"cmd"} eq "displayconfig" ) {
	$self->read_config( $cmd->{"config"}, $cmd->{"previous_config"} );
    }
    return unless( $cmd->{"cmd"} eq "status" );
    $text = $cmd->{"status"}." ".$cmd->{"rawtext"};
    unless( -d "$logdir/tmp" ) { mkdir( $logdir."/tmp", 0700 ); }
    my $check = $cmd->{"host"}.".".$cmd->{"check"};
    open( _LOG, ">$logdir/tmp/$check" ) || return 0;
    print _LOG $text."\n";
    close _LOG;
    unless( Platform::replace( "$logdir/tmp/$check", "$logdir/$check" ) ) {
	print STDERR "could not rename $logdir/tmp/$check to $logdir/$check\n:$!\n";
	unlink( "$logdir/tmp/$check" );
	return 0;
    }
}

