/*
 * DataCut.java
 *
 * Created on 25. elokuuta 2005, 14:27
 */
import java.io.*;
import java.util.*;


public class DataCut {
    
    /** Creates a new instance of DataCut */
    public DataCut(String sourceFile, String targetFile, String targetFile2, int cutIndex) throws IOException, FileNotFoundException{
        if (cutIndex <= 1)
            throw new IOException("Cutting from 1 which is the starting index would be equal to a move command. Exiting");
        Scanner reader = null;
        try{
            reader = new Scanner(new File(sourceFile));
        } catch (FileNotFoundException fnf){
            fnf.printStackTrace();
            System.exit(1);
        }
        
        FileWriter first = new FileWriter(new File(targetFile), false);
        FileWriter second = new FileWriter(new File(targetFile2), false);
        String temp = null;
        int line = 0;
        
        
        while (reader.hasNextLine()){
            temp = reader.nextLine().trim();
            line++;
            
            if (temp.length() == 0 || temp.charAt(0) == '#')
                continue;

            int col = 0;
            StringTokenizer tkn = new StringTokenizer(temp, " ");

            if (tkn.countTokens() < cutIndex)
                throw new IOException("Too short lines to cut from index "+ cutIndex);
            while (tkn.hasMoreTokens()){
                col++;
                if (col <  cutIndex){
                    first.write(tkn.nextToken() + " ");
                }
                else{
                    second.write(tkn.nextToken() + " ");
                }            
            }
            first.write("\n");
            second.write("\n");
        }

	first.close();
	second.close();
	reader.close();
    }
    
    public static void main(String[] args) throws IOException, FileNotFoundException{
        if (args.length != 4){
            System.out.println("Expecting parameters: sourceFile, targetFile, targetFile2, cutIndex");
            System.exit(1);
        }
        int cutIndex = 0;
        try{
            cutIndex = Integer.parseInt(args[3]);
        } catch (NumberFormatException nfe){
            System.out.println("The cutIndex was not a correct integer, exiting.");
            System.exit(1);
        }
        
        DataCut cat = new DataCut(args[0], args[1], args[2], cutIndex);
    }
}



