require "cfgfile.pm";

my $file = $args{"cfg"};
my $logfile;
my $type = $args{"cmd"};
my ( $pattern, $status, $time, $text, $item );

if ( not defined( $file ) )
{
    $file = $type;
};

my $config = new cfgfile( $file );
my $gotCfg = $config->rewind();

if ( not $gotCfg )
{
    BigSister::common::log( "warning", "$type: Cannot read a file named $file" );
};


if( ($type eq "eventlog") && ! Platforms::iswin32() ) 
{
    BigSister::common::log( "warning", "$type: you cannot monitor EventLog on non-Win32-Systems" );
}
elsif ( $gotCfg )
{
    &uxmon::load_module( "$type" );
    while( $_ = $config->nextline() ) {
	chomp;
	next unless( $_ );
	if( /^([^\s]+):$/ ) {
	    my $file;

	    foreach (split(",",$1)) {
		$file = $_;
		unless( ($file =~ /^\//) || ($type eq "eventlog") ) {
	 	    $file = $BigSister::common::root."/".$file;
		}
		last if( -f $file );
	    }
	    if( $type eq "OV" ) {
		$logfile = Monitor::OV->new( $file );
	    }
	    elsif( $type eq "syslog" ) {
		$logfile = Monitor::syslog->new( $file );
	    }
	    elsif( $type eq "logfile" ) {
		$logfile = Monitor::logfile->new( $file );
	    }
	    elsif( $type eq "eventlog" ) {
		$logfile = Monitor::eventlog->new( $args{"host"}.":".$file );
	    }
	    elsif( $type eq "snmp_trap" ) {
		$logfile = Monitor::snmp_trap->new( $file );
	    }
	    # Take a crack at loading things just based on $type
	    else
	    {
	    	my $moniType = 'Monitor::' . $type;
	    	$logfile = $moniType->new( $file, \%args );
	    };
	    $logfile->setlocalhost( $args{"alias"} );
	    $logfile->sched_frequency( $args{"frequency"} );
	    uxmon::checker( $logfile );
	}
	elsif( /^node (.*)=([^\s\t]*)$/ ) {
	    $logfile->translation( $1, $2 );
	}
	else {
	    my $text;
	    ($pattern,$status,$time,$text,$item) = split( /\t+/ );
	    next unless( $pattern && $status );
	    my $code;
	    if( $status =~ /^\d$/ ) { $code = $status; }
	      elsif( $status eq "clear" ) { $code = -1; }
	      else { $code = $BigSister::common::status_codes{$status}; }
	    if( $pattern eq "default" ) {
		$logfile->default( $time, "$item", $code, $text );
	    }
	    else {
		$logfile->add_check( "$item", $pattern, $code, $time*60, sub { return eval '"'.$text.'"'; } );
	    }
	}
    }
    $config->close();
}
else
{
    BigSister::common::log( "warning", "Module $type could not be loaded" );
};

1;
