#ifndef NLP_SERVICE_UTILS_H
#define NLP_SERVICE_UTILS_H

#include <iostream>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>

#include "logger.h"

namespace nlp_service {

typedef boost::program_options::options_description opts_desc;
typedef boost::program_options::variables_map var_map;

/* Utility function that returns default boost::program_options::options_description
 * for services.
 * Command line arguments provided:
 * - config - path to config file
 * - logfile - path to main logfile
 * - logging_lvl
 * - daemon - flag indicating if the service should run in background
 * - help
 * Most of services won't use any specific command-line arguments, so this is
 * provided for user's convinience. If you want to use specific argument consider
 * putting it into config file.
 */
opts_desc get_default_options_desc();

/* Utility function for parsing command line arguments with given option description.
 * Returns boost::program_options::variables_map.
 */
var_map parse_cmdln_options(int argc, char** argv, opts_desc* description = 0);

/* This is the utility function, that runs service given as template argument
 * using default command line options (see nlp_service::get_default_options_desc).
 * Most of services won't use any specific command-line arguments, so this is
 * provided for user's convinience. If you want to use specific argument consider
 * putting it into config file.
 */
template<class ServiceType>
void run_service(int argc, char** argv)
{
	var_map vm = parse_cmdln_options(argc, argv);

	if(vm["help"].as<bool>()) {
		std::cout << get_default_options_desc() << std::endl;
		exit(EXIT_SUCCESS);
	}

	std::string config_path = vm["config"].as<std::string>();
	std::string logfile_path = vm["logfile"].as<std::string>();
	logging_lvl log_lvl = ServiceLogger::str2logging_lvl(vm["logging_lvl"].as<std::string>());
	bool run_as_daemon = vm["daemon"].as<bool>();

	ServiceType service(config_path, logfile_path, log_lvl, run_as_daemon);
	service.run();
}

}	// end namespace nlp_service

#endif	// NLP_SERVICE_UTILS_H
