/*
    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 "config.h"
#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include <sstream>

#include <boost/algorithm/string.hpp>


namespace keywords{

	config config::parse_args(int ac, char **av)
	{
		namespace po = boost::program_options;

		std::vector<std::string> row_poses_vec, column_poses_vec;
		std::string row_words_file, column_words_file;

		config cfg;

		po::options_description cmdline("Command line only options", 100);
		po::options_description basic("Basic options", 100);
		std::string configfile, opstr;

		cmdline.add_options()
			("help", "Prints this message")
			("cfg-file", po::value<std::string>(&configfile)->default_value(std::string("builder.cfg")),
				"Optional config file")
			;
		basic.add_options()
			("matrix-dir,d", po::value<std::string>(&cfg.mdir),
				"Directory with matrices")
			("matrix-name,n", po::value<std::string>(&cfg.mname),
					"Name of a matrix")
			("msr,m", po::value<std::string>(&cfg.msr)->default_value("tfidf()"),
						"MSR used in weighting")
			("k", po::value<size_t>(&cfg.k)->default_value(5),
				"Number of keywords")
			;

		po::options_description all("",80);
		all.add(cmdline).add(basic);

		po::variables_map vm;
		po::store(po::parse_command_line(ac, av, all), vm);
		po::notify(vm);

		if( vm.count("help" ) )
		{
			std::cout << "keywords by Bartosz Broda, bartosz.broda@gmail.com\n";
			std::cout << "Extracts keywords from given matrix (assumes that rows corresponds to documents)\n";
			std::cout << all << std::endl;
			exit(0);
		}

		std::ifstream cfgfile( configfile.c_str() );

		if(cfgfile)
		{
			po::store(po::parse_config_file(cfgfile, basic), vm);
			po::notify(vm);
		}
		else if( vm.count("cfg-file") )
			std::cerr << "Cannot open config file " << configfile << ". Proceeding with commandline and defaults...\n";

		if(! vm.count("matrix-dir") || !vm.count("matrix-name"))
		{
			std::cerr << "You need to specify matrix (matrix-dir and matrix-name)"
					  <<  std::endl;
			exit(1);
		}

		return cfg;
	}
}//namespace keywords
