package ITUannotation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;

public class convertInputToOutput {
	
	
	public convertInputToOutput(File fileName) throws IOException, InterruptedException {
		
		userDir = System.getProperty("user.dir");
		this.fileName=fileName;
					
	//	File dir1 = new File(".");       
	//    System.out.println("Current directory : " + dir1.getCanonicalPath());         
	    
		File tempFile = new File (userDir + "\\output2.txt");
		tempFile.delete();
		
		File tempFile2 = new File (userDir + "\\input2.txt");
		tempFile2.delete();
		
			
	
       File tempSave = new File (userDir + "\\input3.txt");
       
       OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempSave),"UTF-8");
       BufferedWriter writeFile = new BufferedWriter(writer);
       String str;
       InputStreamReader read = new InputStreamReader(new FileInputStream(fileName.getAbsoluteFile()),"UTF-8");
       BufferedReader reader = new BufferedReader(read); 
       str = reader.readLine();
       int sub = str.indexOf(" ", 0);
       String subStr = null;
       boolean notChange = false;
       if(sub == -1){
    	   subStr = str;
    	   notChange = true;
       }
       else
    	   subStr = str.substring(0, sub);
       byte [] bytSub = subStr.getBytes();
       String subLast = new String(bytSub,"UTF-8");
       if(notChange && subLast.substring(0, 1).contains("?"))
    	   writeFile.write(subLast.substring(1));
       else if(notChange)
    	   writeFile.write(subLast);
       else if(subLast.substring(0, 1).contains("?") && notChange == false){
    	   String x = new String(bytSub);
    	   String sil=x.substring(1)+ " " + str.substring(sub+1) + " ";
    	   writeFile.write(x.substring(1)+ " " + str.substring(sub+1) + " ");
    	   
       }
       else if(notChange == false)
    	   writeFile.write(subLast+ " " + str.substring(sub+1) + " ");
       while ((str = reader.readLine()) != null) {	
			writeFile.write(str + " ");	
		}
		
		writeFile.close();
		writer.close();
		
	
		Tokenizer t=new Tokenizer("input3.txt","input2.txt" );
		t.tokenize();
		 ProcessBuilder pb=null; 
		 pb=new ProcessBuilder(userDir + "\\run.bat","output2.txt");
		 try {
             Process pp = pb.start(); //run.exec("d:\\jdk1.3\\bin\\javac");
             BufferedReader in = new BufferedReader(new InputStreamReader(pp.getInputStream()));
             String line;
             while ((line = in.readLine()) != null) {
                   System.out.println(line);
             }
             pp.waitFor();
       } catch (Exception e) {
             e.printStackTrace();
             System.out.println(e.getMessage());
       }
       
   
       
		
		//Process p2=Runtime.getRuntime().exec("cmd.exe /c start \n run.bat");
		//p2.waitFor();
		
		
		
	//	Process p3 = Runtime.getRuntime().exec("perl convert.pl");  bu scripte gerek kalmadi
		
		
	}
	
	private int copyfile(String f1, String f2) {
		// TODO Auto-generated method stub
		InputStream in;
		try {
			in = new FileInputStream(f1);
			 
			  //For Append the file.
			//  OutputStream out = new FileOutputStream(f2,true);

			  //For Overwrite the file.
			  OutputStream out = new FileOutputStream(f2);

			  byte[] buf = new byte[1024];
			  int len;
			  while ((len = in.read(buf)) > 0){
			  out.write(buf, 0, len);
			  }
			  in.close();
			  out.close();
			  return 0;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		
	}

	public File getOutputFile()
	{
		return new File(userDir + "\\output2.txt");
	}

	private File fileName;
	private String userDir;
	private String tDir;

}
