
/**
 * @author Luis
 *
 */
import java.io.*;

public class foamMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// args.length is equivalent to argc in C
		FileOutputStream out; // declare a file output object
		PrintWriter p; // declare a print stream object
		
		// System.out.println("TRAZA 0");
		// File 1
		FileInputStream fstream; // declare a file input object
		BufferedReader in; // declare a file input object

		// File 2
		FileInputStream fstream2; // declare a file input object
		BufferedReader in2; // declare a file input object

		if (args.length == 2){
			try{
				// Open the file that is the first 
				// command line parameter
				fstream = new FileInputStream(args[0]);
				fstream2 = new FileInputStream(args[1]);

				// Convert our input stream to a DataInputStream
				in = new BufferedReader(new InputStreamReader(fstream));
				in2 = new BufferedReader(new InputStreamReader(fstream2));

				// Tokenizer to split the sentence:
				StreamTokenizer st = new StreamTokenizer(in);
				StreamTokenizer st2 = new StreamTokenizer(in2);

				// Create a new file output stream connected to "output.gcode"
				out = new FileOutputStream("output.gcode");
				
				// Connect print stream to the output stream
				p = new PrintWriter( out );
			
				// Command
				st.nextToken();
				st2.nextToken();

				//FIRST LINE
				//System.out.println("G1 X-1400 Y700 A-1400 B-700 F3000.0");
				// CONFIGURATION G20 G17 G90 M3
				
				System.out.println("G20");
				System.out.println("G17");
				System.out.println("G90");
				System.out.println("M3");
				 
				/*p.println("G20");
				p.println("G17");
				p.println("G90");
				p.println("M3");*/
				
				// Continue to read lines while there are still some left to read
				while (in.ready() && in2.ready()){
										
					// First buffer for X and Y
					String buffer1 = new String();
					// Second buffer for A and B
					String buffer2 = new String();
					// Third buffer to mix them
					String buffer3;
					//Check if there is enumeration code before the instruction
					if (st.sval.contains("N"))
						st.nextToken();
					if (st2.sval.contains("N"))
						st2.nextToken();
					
					// If the first command is G we compare if not we print the sentence.
					if (((st.sval.compareTo("G00")==0)||(st.sval.compareTo("G0")==0)) && ((st2.sval.compareTo("G00")==0)||(st2.sval.compareTo("G0")==0))||((st.sval.compareTo("G01")==0)||(st.sval.compareTo("G1")==0)) && ((st2.sval.compareTo("G01")==0)||(st2.sval.compareTo("G1")==0))){
						
						/////// File 1 ///////
						if (st.sval.contains("G")){
							buffer1 = buffer1.concat(st.sval+" "); // G1
							st.nextToken();
						}else if(st.sval.contains("M")){// G1
							buffer1 = buffer1.concat(st.sval+" "); // G1
							st.nextToken();
						}
						
						if(st.sval.contains("Z")){
							while (!((st.sval.contains("G")||st.sval.contains("M")))){
								st.nextToken();
								buffer1 = "";
							}
							if (st.sval.contains("G")){
								buffer1 = buffer1.concat(st.sval+" "); // G1
								st.nextToken();
							}else if(st.sval.contains("M")){// G1
								buffer1 = buffer1.concat(st.sval+" "); // G1
								st.nextToken();
							}
						}
						if (st.sval.contains("X")||st.sval.contains("Y")){
							buffer1 = buffer1.concat(st.sval+" "); // X or Y
							st.nextToken();
						}
						if (st.sval.contains("X")||st.sval.contains("Y")){
							buffer1 = buffer1.concat(st.sval+" "); // X or Y
							st.nextToken();
						}else if (st.sval.contains("Z")){
							st.nextToken();
						}
						if (st.sval.contains("X")||st.sval.contains("Y")){
							buffer1 = buffer1.concat(st.sval+" "); // X or Y
							st.nextToken();
						}else if (st.sval.contains("Z")){
							st.nextToken();
						}
						buffer3 = new String(buffer1); // Mix buffer1 and 2
						
						
						//// File 2 ////
						if (st2.sval.contains("G")){
							buffer2 = buffer2.concat(st2.sval+" "); // G2
							st2.nextToken(); // X
						}else if(st2.sval.contains("M")){// M
							buffer2 = buffer2.concat(st2.sval+" "); // M
							st2.nextToken();
						}
						
						// change X per A, Y per B (first round)
						if(st2.sval.contains("Z")){
							while (!((st2.sval.contains("G")||st2.sval.contains("M")))){
								st2.nextToken();
								buffer2 = "";
							}
							if (st2.sval.contains("G")){
								buffer2 = buffer2.concat(st2.sval+" "); // G1
								st2.nextToken();
							}else if(st2.sval.contains("M")){// G1
								buffer2 = buffer2.concat(st2.sval+" "); // G1
								st2.nextToken();
							}
						}
						if (st2.sval.contains("X")){
							st2.sval = st2.sval.replace('X','A');
							buffer2 = buffer2.concat(st2.sval+" "); // A
							buffer3 = buffer3.concat(st2.sval+" "); // mix buffer 1 and 2
							st2.nextToken();
						}
						if (st2.sval.contains("Y-")){
							st2.sval = st2.sval.replace("Y-","B");
							buffer2 = buffer2.concat(st2.sval+" "); // B
							buffer3 = buffer3.concat(st2.sval+" "); // mix buffer 1 and 2
							st2.nextToken();
						}else if (st2.sval.contains("Z")){
							st2.nextToken();
						}
						if(st2.sval.contains("Y")){
							st2.sval = st2.sval.replace("Y","B-");
							buffer2 = buffer2.concat(st2.sval+" "); // B-
							buffer3 = buffer3.concat(st2.sval+" "); // mix buffer 1 and 2
							st2.nextToken();
						}else if(st2.sval.contains("Z")){
							st2.nextToken();
						}
						
						// change X per A, Y per B (second round)
						if (st2.sval.contains("X")){
							st2.sval = st2.sval.replace('X','A');
							buffer2 = buffer2.concat(st2.sval+" "); // A
							buffer3 = buffer3.concat(st2.sval+" "); // mix buffer 1 and 2
							st2.nextToken();
						}else if (st2.sval.contains("Y-")){
							st2.sval = st2.sval.replace("Y-","B");
							buffer2 = buffer2.concat(st2.sval+" "); // B
							buffer3 = buffer3.concat(st2.sval+" "); // mix buffer 1 and 2
							st2.nextToken();
						}else if(st2.sval.contains("Y")){
							st2.sval = st2.sval.replace("Y","B-");
							buffer2 = buffer2.concat(st2.sval+" "); // B-
							buffer3 = buffer3.concat(st2.sval+" "); // mix buffer 1 and 2
							st2.nextToken();
						}else if(st2.sval.contains("Z")){
							st2.nextToken();
						}
						
						//Compare F: To mix times
						if ((st.sval.contains("F"))&&(st2.sval.contains("F"))){
							//st.nextToken();
							buffer1 = buffer1.concat(st.sval); // F
							//st2.nextToken();
							buffer2 = buffer2.concat(st2.sval); // F
							
							if (st.sval.compareTo(st2.sval)==0){ // Same time
								buffer3 = buffer3.concat(st.sval);
								System.out.println(buffer3);
								//p.println (buffer3);
								
								//prepare for next loop
								st.nextToken();
								st2.nextToken();
							}else if (st.sval.compareTo(st2.sval)<0){ // Less time
								
								// Calculate time
								st.sval = st.sval.replace('F','0');
								st2.sval = st2.sval.replace('F','0');
								double aDouble = Double.parseDouble(st.sval);
								double bDouble = Double.parseDouble(st2.sval);
								double valueF = (bDouble-aDouble);
								st.sval = String.valueOf(valueF);
								buffer3 = buffer3.concat('F'+st.sval);
								//Print the result
								System.out.println(buffer3);
								//p.println (buffer3);
								
								//prepare for next loop
								st.nextToken();
								st2.nextToken();
							}else if (st.sval.compareTo(st2.sval)>0){ // More time
								// Calculate time
								st.sval = st.sval.replace('F','0');
								st2.sval = st2.sval.replace('F','0');
								double aDouble = Double.parseDouble(st.sval);
								double bDouble = Double.parseDouble(st2.sval);
								double valueF = (aDouble-bDouble);
								st.sval = String.valueOf(valueF);
								buffer3 = buffer3.concat('F'+st.sval);
								//Print the result
								System.out.println(buffer3);
								//p.println (buffer3);
								
								//prepare for next loop
								st.nextToken();
								st2.nextToken();
							}else{ // If I cannot mix them
								System.out.println(buffer1); // 1
								//p.println (buffer1);
								System.out.println(buffer2); // 2
								//p.println (buffer2);
								//prepare for next loop
								st.nextToken();
								st2.nextToken();
							}
						}else{
							System.out.println(buffer3+"F3000.0");
							//p.println (buffer3+"F3000.0");
						}
					}else{
						//Check if there is enumeration code before the instruction
						while (st.sval.contains("N"))
							st.nextToken();
						while (st2.sval.contains("N"))
							st2.nextToken();
						do{
							buffer1 = buffer1.concat(st.sval+" ");
							st.nextToken();
							st2.nextToken();
						}while (!(st.sval.contains("N"))&&((st.sval==null)||!(st.sval.contains("G"))||!(st.sval.contains("M"))));
						System.out.println(buffer1);
						//p.println (buffer1);
						
						//prepare for next loop*/
						
					}
					
					/*switch (){
					case 'G':
						System.out.println(st.sval);
						break;
					default:
						System.out.println("Nothing");
					}//*/
					
					//TO DO: Mix lines, checking time
					// Take number after F of input1
					// Take number after F of input2
					// If they are the same:
						// Take everything before F of input1
						// take everything after X
						// Change X per A, change Y per B
					// If is not the same
						// Put input1 the same
						// put the input2 chaging X per A and Y per B
					
					// Print line to the file
					//p.println (in.readLine());
					//p.println (in2.readLine());
				}
				//LAST LINE */
				//p.println ("G1 X1200 Y-700 A1200 B700 F3000.0");
				//System.out.println("G1 X1200 Y-700 A1200 B700 F3000.0");
				
				//Closing files
				in.close();
				in2.close();
				out.close();
				p.close();
				
			} catch (Exception e){
				//System.out.println("Error writing to file");
				System.err.println("Error writing to file");
			}
		}else
			System.out.println("Invalid parameters");
	}
	
}            
