#!/usr/bin/env perl
###########################################################################
# whatbot
###########################################################################
# executable bootstrap for whatbot.pm
###########################################################################
# the whatbot project - http://www.whatbot.org
###########################################################################

use strict;
use warnings;
use Cwd qw(realpath getcwd);
use Getopt::Long;
use EV;
use lib realpath( getcwd() ) . '/../lib';

my $basedir = realpath( getcwd() . '/..' );

# Check command line options
my $config_path = $basedir . "/conf/whatbot.conf";
my $daemon = 0;
my $help;

# Initial requirements check
eval {
    require 5.008_001;
};
if ($@) {
	print 'ERROR: whatbot requires perl 5.8 or higher.' . "\n";
	exit(-1);
}
require whatbot;

GetOptions(
    'config=s'  => \$config_path,
    'daemon'    => \$daemon,
    'help'      => \$help
);
usage() if ($help);

whatbot->import;

# Start application
my $whatbot = whatbot->new();
if ( $whatbot->config( $basedir, $config_path ) ) {
    if ($daemon) {
    	fork and exit;
    } else {
    	print 'whatbot ' . $whatbot::VERSION . "\n";
    	print 'Running in interactive mode.' . "\n";
    }

    $SIG{'INT'} = sub { $whatbot->stop(); };
    $whatbot->run();
} else {
    usage();
    exit(-1);
}

sub usage {
	print qq{whatbot $whatbot::VERSION:

 -c --config  Path to configuration file (default: conf/whatbot.conf)
 -d --daemon  Daemonize on successful launch
 -h --help	  Print this help screen\n
};
	exit(0);
}

1;
