/*
    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 <stdio.h>
#include <iostream>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp> 

int m_to[2];
int m_from[2];


std::string getbase(std::string word, std::string cat)
{
	std::string r = "";

	static char * morpha = "/home/boban/software/morph/morpha.ix86_linux";
	static char * verb = "/home/boban/software/morph/verbstem.list";
	static char buf[1024];

	std::string command = std::string("echo \"") + word + std::string("_") + cat +std::string("\" | ") + morpha + std::string(" -f ") + verb;
	system(command.c_str());
	/*
	FILE * pfile = popen(command.c_str(), "r");

	fgets(buf, 1024, pfile);
	r= buf;
	boost::trim(r);
	pclose(pfile);
	fflush(pfile);
	pclose(pfile);
*/
	return r;
}

void process_file(const std::string &filename)
{
	static const boost::regex e("<w ([^>]+)>([^<>$]+)");

	std::ifstream file(filename.c_str());
	std::string line;
	while( std::getline(file,line) )
	{
		std::string::const_iterator start, end; 
		start = line.begin();
		end = line.end();
		boost::match_results<std::string::const_iterator> what; 
		boost::match_flag_type flags = boost::match_default; 

		while(regex_search(start,end, what, e, flags)) 
		{
			std::string category = what[1];
			std::string word = what[2];
			start = what[0].second; 
			flags |= boost::match_prev_avail; 
			flags |= boost::match_not_bob;
			std::string base = getbase(word,category);
			//std::cout << word << "_" << category << "; " << base << std::endl;
		}
	}

}

int main(int ac, char*av[])
{
	//std::cout << "Word: \"" << getbase( std::string("syntehesizers"), std::string("NN1") )  << "\"" << std::endl ;
	//process_file( std::string (av[1]) );
	//
	pipe( m_to );
	pipe( m_from );
	pid_t pID = fork();
	if(pID == 0)//child
	{
		dup2( m_to[0], 0 );
		dup2( m_from[1], 1 );

		std::string output;
		while( output != "<EOF>" )
		{
/*			char c;
			while ( read( m_to[0], &c, 1 ) ) {
				if ( c == '\n' )
					break;
				output += c;
			}*/

			std::getline(std::cin, output);
			std::cout << "From child: " << output << std::endl;
			//write(m_from[1], "KOTY\n", 5);
			std::cout.flush();
		}
	}
	else if (pID < 0 )
	{
		std::cerr << "Failed to fork!" << std::endl;
	}
	else//parent
	{
		std::string output;
		char c;
		write(m_to[1], "tttt\n", 5);
		while ( read( m_from[0], &c, 1 ) ) {
			if ( c == '\n' )
				break;
			output += c;
		}

//		std::cout << "parent:" << output << std::endl;

/*		write(m_to[1], "yyyy\n", 5);
		while ( read( m_from[0], &c, 1 ) ) {
			if ( c == '\n' )
				break;
			output += c;
		}*/

//		std::cout << "parent:" << output << std::endl;
	}
	return 0;
}
