package Monitor::bbscript;

require 5.002;
use FileHandle;

use strict;
use Monitor::statusfile;

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


# 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, @vars ) = @_;
    my( $htable ) = $self->{"htable"};

    $file = BigSister::common::find_file( $file );
    # add this info to the configuration array
    push( @$htable, { "file" => $file, "vars" => \@vars } );
}





sub check
{
    my ($self) = @_;
    my $check;
    my $tmp = "/tmp/bbscr$$";

    my %vars = (
	"BBHOME" => $BigSister::common::root,
	"BB" => "$BigSister::common::fs{'bin'}/bbecho",
	"BBPAGE" => "$tmp",
	"BBDISP" => "$tmp",
	"MACHINE" => $uxmon::hostname
    );

    foreach $check (@{$self->{"htable"}}) {
	my $file = $check->{"file"};
	my $vars = $check->{"vars"};
	my %SENV = %ENV;
	my( $var, $cont, $text );

	open( ST, ">$tmp" );
	while( ($var,$cont) = each %vars ) {
	    $ENV{$var} = $cont;
	}
	foreach $text (@$vars) {
	    print "bbscript: pass var $text\n" if( $BigSister::common::dl>6 );
	    if( $text =~ /^(.*?)=(.*)$/ ) {
		$ENV{$1} = $2;
	    }
	}
	print "bbscript: executing $file\n" if( $BigSister::common::dl>4 );
	system( $file );
	%ENV = %SENV;
	$self->statusfile( $tmp );
	unlink( $tmp );
    }
}

1;
