#include "config.h"

config config::process(int ac, char **av)
{
    config cfg;
    
    namespace po = boost::program_options;
    
    po::options_description cmdline("Command line only options", 100);
    po::options_description basic("Basic options", 100);
    std::string configfile;
    
    cmdline.add_options()
            ("help", "Prints this message")
            ("cfg-file", po::value<std::string>(&configfile)->default_value(std::string("mwefinder.cfg")), "Optional config file")
            ;
    basic.add_options()
            ("tagset", po::value<std::string>(&cfg.tagset_name)->default_value("kipi"), "Name of a tagset.")
            ("reader", po::value<std::string>(&cfg.reader_class), "Name of a reader.")
            ("corpus", po::value<std::vector<std::string> >(&cfg.corpus_paths)->multitoken(), "Path(s) to corpus.")
            ("outputs", po::value<std::vector<std::string> >(&cfg.output_paths)->multitoken(), "Output names for each input corpus.")
            ("mwefiles", po::value<std::vector<std::string> >(&cfg.mwe_files)->multitoken(), "Paths to files with mwe definitions.")
	        ("quiet", po::value<bool>(&cfg.quiet)->default_value(false), "If set the program won't speak.")
            ;
    
    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 << "mwefinder by Michał Moczulski, michalmoczulski1a@gmail.com\n";
        std::cout << "Merges hopefully all multi-word-expressions into single tokens.\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);
    }
    
    
    if (!vm.count("reader"))
    {
        std::cerr << "You need to specify reader." << std::endl;
        exit(1);
    }
    if (!vm.count("corpus"))
    {
        std::cerr << "You need to specify corpus." << std::endl;
        exit(1);
    }
    if (!vm.count("outputs"))
    {
        std::cerr << "You need to specify output paths." << std::endl;
        exit(1);
    }
    if (cfg.corpus_paths.size() != cfg.output_paths.size())
    {
        std::cerr << "Number of input and output paths should be equal!" << std::endl;
        exit(1);
    }
    if (!vm.count("mwefiles"))
    {
        std::cerr << "You need to specify mwefile." << std::endl;
        exit(1);
    }
    
    return cfg;
}
