#include "service.h"

#include <cstdlib>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
#include <boost/property_tree/ini_parser.hpp>

#include "servicemanager.h"

namespace nlp_service {

Service::Service(const std::string& config_path, std::string logfile_path,
				logging_lvl log_lvl, bool run_as_daemon)
: run_as_daemon(run_as_daemon)
{
	this->config = this->read_config_file(config_path);

	std::string error_dir = this->config.get("service.error_dir", "./");
	this->logger = boost::make_shared<ServiceLogger>(log_lvl, logfile_path, error_dir, !run_as_daemon);
}

Service::~Service()
{ }

void Service::run() const
{
	ServicePtr self = boost::shared_ptr<const Service>(this);
	ServiceManager service_manager(self, this->config, this->logger, this->run_as_daemon);
	service_manager.run();
}

/* Reads config file given at the command line (default ./config.ini)
 * It simply reads the file and returns boost::property_tree::ptree.
 */
Service::service_config Service::read_config_file(const std::string& config_path)
{
	using boost::property_tree::ptree;

	service_config config;
	read_ini(config_path, config);

	return config;
}

}	// end namespace nlp_service
