#!/usr/bin/perl

# infobot -- copyright kevin lenzo (c) 1997-infinity

BEGIN {
    $filesep = '/';
    # set this to the absolute path if you need it; especially
    # if . is not in your path
    $infobot_base_dir = '.'; 

    $infobot_src_dir = $infobot_base_dir.$filesep."src";
    # $infobot_src_dir = '/usr3/infobot/infobot/src'; 

    $initmiscdir = $infobot_base_dir.$filesep.'files';

    $param{'miscdir'} = $initmiscdir;

    # these are here individually so you can mix
    # and match code segments for different infobots.

    # everything is loaded, then the variables that
    # you want to set will override the defaults; this
    # is why all these requires are here.  

    opendir DIR, $infobot_src_dir 
	or die "can't open source directory $infobot_src_dir: $!";

    while ($file = readdir DIR) {
	next unless $file =~ /\.pl$/;
	require "$infobot_src_dir$filesep$file";
    }
    closedir DIR;
}

# get the command line arguments
&getArgs();

# param setup should stay after most of the requires
# so that it overrides anything they might set.
&paramSetup();

if ($param{VERBOSITY} > 1) {
    my $params = "Parameters are:\n";
    foreach (sort keys %param) {
	$params .= "   $_ -> $param{$_}\n";
    }
    &status($params);
}

die "dbname is null" if (!$param{'dbname'});
&setup("is" => $infobot_base_dir.$filesep.$param{'dbname'}.'-is',
       "are" => $infobot_base_dir.$filesep.$param{'dbname'}.'-are');

# unfortunate use of a global list here
&parseUserfile();

# launch the irc event loop
&irc();

exit 0;  # just so you don't look farther down in this file :)


# --- support routines

sub usage {
    print "\n";
    print "  usage: $0 [-h] [<paramfile1> [<pf2> ...]]\n";
    print "\n";
    print "		 -h   this message\n";
    print "\n";
}

sub getArgs {
    if (@ARGV) {
	while (@ARGV) {
	    my $arg = shift @ARGV;
	    if ($arg =~ s/^-//) {
		# switchies
		if ($arg eq 'i') {
		    # go into irc mode despite db setting
		    $overrideMode = 'IRC';
		} else {
		    # -h is in here by default
		    &usage;
		    exit(1);
		}
	    } else {
		# no switchie.  currently assumed to be
		# a paramfile by default
		push @paramfiles, $arg;
	    }
	}
    } else {
	@paramfiles = ();
    }
}


sub paramSetup {
    my $initdebug = 1;
    $param{'DEBUG'} = $initdebug;

    if (!@paramfiles) {
	# if there is no list of param files, just go for the default
	# (usually ./files/infobot.config)

	@paramfiles = ($initmiscdir.$filesep."infobot.config");
    }

    # now read in the parameter files
    &loadParamFiles(@paramfiles);
}

1;
