/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. 

    See the LICENSE and COPYING files for more details.
*/

#include <iostream>
#include "options.h"
#include <signal.h>
#include <stdlib.h>
#include "kbestserver.h"
#include "kbestcounter.h"



kbestd::KBestServer* g_pServer = NULL;

void clean_up(int param)
{
	g_pServer->CleanUp();
	exit(0);
}

int main(int ac, char**av) 
{
	using namespace kbestd;

	std::cout << "Hello kbestd World!\n";
	try 
	{
		Matrices::init_logging();
		options opts;
		parse_options(ac, av, opts);
		KBestServer server(opts);
		g_pServer = &server;
		signal (SIGTERM,clean_up);
		signal (SIGHUP,clean_up);
		signal (SIGABRT,clean_up);
		signal (SIGINT,clean_up);
		server.Run();
	} catch (std::exception& e) 
	{
		std::cerr << e.what() << std::endl;
		clean_up(0);
		return 1;
	}
	g_pServer = NULL;

	return 0;
}

