/*
    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 "options.h"
#include <boost/program_options.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

namespace filtrator
{
	Options::Options(int ac, char *av[])
	{
		InitOptions(ac,av);
	}

	Params& Options::GetParams()
	{
		return m_Params;
	}

	void Options::InitOptions(int ac, char *av[])
	{
		namespace po = boost::program_options;

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

		cmdline.add_options()
			("help", "Prints this message")
			("cfg-file", po::value<std::string>(&configfile)->default_value(std::string("filtrator.cfg")), "Config file")
			;
		basic.add_options()
			("matrix", po::value<std::string>(&m_sMatrixPath), "Matrix path.")
			("out-name", po::value<std::string>( & m_Params.m_sMatrixOutName ), "Name of output matrix.")
			("out-dir", po::value<std::string>( & m_Params.m_sOutputDir )->default_value("./wd"), "Name of output dir.")
			("words", po::value<std::string>( & m_Params.m_sWordsFile ), "Name of file with words to extract.")
			("mode", po::value<std::string>( & mode )->default_value("row"), "Filtering mode. One of: row or column")
			;

		po::options_description all("",100);
		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 << "Filtraotr by Bartosz Broda, bartosz.broda@pwr.wroc.pl\n" << 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 defaults...\n";
		if(mode == "column" )
			m_Params.m_bRows = false;
		else m_Params.m_bRows = true;

		if(! vm.count("out-name") )
			throw std::runtime_error("out-name should be set!");
		if(! vm.count("matrix") )
			throw std::runtime_error("matrix should be set!");

		CheckMatrix();
	}

	std::string Options::NormalizeDir(const std::string &s)
	{
		char last_char = s[s.length() - 1];
		std::string ret (s);
		if(last_char == '\\' || last_char == '/')
			ret = ret.substr(0, s.length() - 1);
		return ret;

	}

	void Options::CheckMatrix()
	{
		std::string matrix_full_path = m_sMatrixPath;
		matrix_full_path = NormalizeDir(matrix_full_path);
		boost::filesystem::path p(matrix_full_path);
		std::string matrix_name = p.filename().c_str();
		std::string matrix_dir = p.branch_path().string();
		Matrices::IO::CheckSuperMatrixFiles(matrix_dir, matrix_name);
		m_Params.m_sMatrixName = matrix_name;
		m_Params.m_sMatrixDir = matrix_dir;
	}

}//vector extractor

