//
// Core Program for TETDM 
// EXECUTE.java Version 0.11
// Copyright(C) 2011 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//


package source;

import java.io.*;

public class EXECUTE
{
	public static void exeProcess(String command[])
	{
		//Display commands
		for(int i=0;i<command.length;i++)
			System.out.print(command[i]+" ");
		System.out.println();

		try				//Execute Process
		{
			Runtime rt = Runtime.getRuntime(); // Without using constructor
			Process ps = rt.exec( command );
			InputStreamReader isr = new InputStreamReader( ps.getInputStream() );
			BufferedReader br = new BufferedReader( isr );
			StringBuffer out = new StringBuffer();			
			String line;
			
			while ((line = br.readLine()) != null) //data withdraw from the buffer
				;
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static String getExeResult(String command[])
	{
		//Display commands
		for(int i=0;i<command.length;i++)
			System.out.print(command[i]+" ");
		System.out.println();
		
		String result = "";
		
		try				//Execute Process
		{
			Runtime rt = Runtime.getRuntime(); // Without using constructor
			Process ps = rt.exec( command );
			InputStreamReader isr = new InputStreamReader( ps.getInputStream() );
			BufferedReader br = new BufferedReader( isr );
			StringBuffer out = new StringBuffer();			
			String line;
			
			while ((line = br.readLine()) != null) //data withdraw from the buffer
				result += line;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return result;
	}	
/*
	public static void filecopy(String filename1, String filename2)
	{
		String command0[] = {"cp", filename1, filename2};

		exeProcess(command0);
	}
*/
	/**
	 * File copy with binary mode
	 * @param filename1 copy source
	 * @param filename2 copy target
	 */
	public static void filecopy(String filename1, String filename2)
	{
		
		BufferedInputStream src=null;
		BufferedOutputStream trg=null;
		
		try {
			src = new BufferedInputStream(new FileInputStream(filename1));
		} catch (FileNotFoundException e) {
			System.out.println("input file "+filename1+" not found.");
			e.printStackTrace();
		}
		
		try {
			trg = new BufferedOutputStream(new FileOutputStream(filename2));
		} catch (IOException e) {
			System.out.println("Error while opening file "+filename2+".");
			e.printStackTrace();
		}
		
		try {
			int val=0;
			while((val = src.read()) >= 0){
				trg.write(val);
			}
		} catch (IOException e) {
			System.out.println("Error while copying "+filename1+" to "+filename2+".");
			e.printStackTrace();
		}
		
		try {
			src.close();
		} catch (IOException e) {
			System.out.println("input file "+filename1+" has not closed.");
			e.printStackTrace();
		}
		try {
			trg.close();
		} catch (IOException e) {
			System.out.println("input file "+filename2+" has not closed.");
			e.printStackTrace();
		}
	}
	
//	public static String getChasenPath()
//	{
//		String command0[] = {"/usr/bin/which","chasen"};
//		
//		return EXECUTE.getExeResult(command0);
//	}
	
	public static void exe_chasen(boolean chasenexe, String sourcefile, String chaf, boolean windows, String chasenPath)
	{
		File chafile = new File(chaf);

		if(chafile.exists() == false)
			chasenexe = true;

		if(chasenexe)
		{
			//			String command0[] = {"/bin/sh","-c","chasen "+sourcefile+" > "+chafile};
			String commandwin[] = {"cmd","/c","chasen "+sourcefile+" > "+chafile};
			
			if(windows)
				EXECUTE.exeProcess(commandwin);
			else
			{

	//			String chaPath = EXECUTE.getChasenPath();
	//			chasenPath = chaPath.substring(0,chaPath.length());
				
	//			FILEIO.TextFileWrite2(new File(sourcefile+"chapath"), chasenPath);
	
		//		chasenPath = "/usr/local/bin/chasen";
				
			String command0[] = {"/bin/sh","-c",chasenPath+" "+sourcefile+" > "+chafile};			
//							String command0[] = {"/bin/sh","-c","/usr/local/bin/chasen "+sourcefile+" > "+chafile};				
				
				EXECUTE.exeProcess(command0);
			}
		}
	}
	
	public static void exe_stemming(String sourcefile, String stemf, boolean windows)
	{
		File stemfile = new File(stemf);

		String command0[] = {"/bin/sh","-c","porter "+sourcefile+" > "+stemfile};
		String commandwin[] = {"cmd","/c","porter "+sourcefile+" > "+stemfile};
		
		if(windows)
			EXECUTE.exeProcess(commandwin);
		else
			EXECUTE.exeProcess(command0);
	}	
}
