/*
    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 <iostream>
#include <fstream>
#include "options.h"
#include "index.h"

#include <boost/filesystem.hpp>

int main(int ac, char **av)
{
	Matrices::init_logging();
	using namespace tuplesproc;

	options opts;
	parse_options(ac, av, opts);

	if(!boost::filesystem::exists(opts.output_path))
		boost::filesystem::create_directories(opts.output_path);
	else if(!boost::filesystem::is_directory(opts.output_path)){
		std::cerr << opts.output_path << " exists and is not a directory"
				  << std::endl;
		exit(1);
	}

	boost::filesystem::path mpath = boost::filesystem::path(opts.output_path) / opts.matrix_name;
	if (boost::filesystem::exists( mpath )) {
		std::cerr <<  mpath.string() << " exists. Cannot proceed.\n";
		exit(2);
	}


	Index index(opts.raport_interval);

	std::cout << "\033[32mIndexing...\033[0m" << std::endl;
	if(opts.row_path!="")
		index.row_filtering(opts.row_path);
	if(opts.col_path!="")
		index.col_filtering(opts.col_path);
	if(opts.relation_path!="")
		index.relation_filtering(opts.relation_path);
	if(opts.relations.size()>0)
		index.relations_set_filtering(opts.relations);
	if(opts.input_path != "")
		index.index_path(opts.input_path, opts.include);
	else
		index.index_list(opts.input_list);
		
	std::cout << "\033[32mCreating matrix\033[0m" << std::endl;

	std::map<std::string, size_t> dict;
	tuplesproc::load_dict(opts.dict_file, dict);
	index.set_min_tf(opts.row_min_tf, opts.col_min_tf);
	Matrices::SuperMatrixPtr matrix = index.mkmatrix(opts.matrix_name, opts.tokens, opts.matrix_description, dict);

	std::cout << "\033[32mWriting matrix...\033[0m" << std::endl;

	Matrices::IO::WriteSuperMatrix(matrix, opts.output_path);
	std::cout << "\033[32mAll done\033[0m" << std::endl;


}

