#include <boost/filesystem.hpp>

#include <libcorpus2/tagsetmanager.h>
#include <libcorpus2/io/xcesreader.h>
#include <libcorpus2/io/xceswriter.h>

#include <libmwereader/mwereader.h>

#include "config.h"


using namespace boost::filesystem;


void convert(Corpus2::MWEReader & reader, const config & cfg, path in_path, path out_path)
{
	if (!cfg.quiet)
		std::cout << "Processing corpus: " << in_path << ", writing to: " << out_path << std::endl;
    
	create_directories(out_path.parent_path());
	
    if (!exists(out_path))
    {
        std::ofstream outfile (out_path.string().c_str(), std::ios::out | std::ios::trunc);
        outfile << "";
        outfile.close();
    }
    
	reader.setFile(in_path.string());
	
    Corpus2::TokenWriter::TokenWriterPtr writer = Corpus2::TokenWriter::create_path_writer(
	            cfg.reader_class, out_path.string(), 
	            Corpus2::get_named_tagset(cfg.tagset_name));
    
    
    Corpus2::Sentence::Ptr sentence;
    while ((sentence = reader.get_next_sentence()))
    {
        writer->write_sentence(*sentence);
    }
}




int main(int ac, char**av)
{
    config cfg = config::process(ac, av);
	
	if (!cfg.quiet)
		std::cout << "Starting..." << std::endl;
	
	Corpus2::MWEReader reader(Corpus2::get_named_tagset(cfg.tagset_name), cfg.corpus_paths[0]);
    reader.set_option("inner:" + cfg.reader_class);
	
	if (!cfg.quiet)
		std::cout << "Loading MWEs..." << std::endl;
	
	for (unsigned i = 0; i < cfg.mwe_files.size(); i++)
		reader.set_option("mwefile:" + cfg.mwe_files[i]);
	
	if (!cfg.quiet)
		std::cout << "Converting..." << std::endl;
	
    for (unsigned i = 0; i < cfg.corpus_paths.size(); i++)
    {
        convert(reader, cfg, cfg.corpus_paths[i], cfg.output_paths[i]);
    }
    
    return 0;
}
