package ie.tcd.slscs.bundles;

/*
 * Copyright 2016 Jim O'Regan <jaoregan@tcd.ie>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import ie.tcd.slscs.bundles.Utils;

public class SketchEngineVerticalSplitByDirectory {

    public static void main(String[] args) {
        String line;

        try {
            InputStream fis = new FileInputStream(args[0]);
            InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
            BufferedReader br = new BufferedReader(isr);
            OutputStream fos;
            OutputStreamWriter osw;
            BufferedWriter bw;
            String lastpath = "";
            while ((line = br.readLine()) != null) {
                if(line.startsWith("<file ")) {
                    int parent_beg = line.indexOf("parent_folder=\"", 5);
                    parent_beg += 14;
                    int parent_end = line.indexOf('"', parent_beg+1);
                    String folder = line.substring(parent_beg+1, parent_end);
                    int filename_beg = line.indexOf(" filename=\"", 5);
                    filename_beg += 11;
                    int filename_end = line.indexOf('"', filename_beg+1);
                    String filename = line.substring(filename_beg, filename_end);
                    int dir_end = filename.lastIndexOf('/');
                    String directory = filename.substring(0, dir_end);
                    
                    if(directory != lastpath) {
                        if(bw != null) {
                            bw.flush();
                            bw.close();
                        }
                        fos = new FileOutputStream(directory.replaceAll("/", "_") + "merged.vert");
                        osw = new OutputStreamWriter(fos, Charset.forName("UTF-8"));
                        bw = new BufferedWriter(osw);
                        lastpath = directory;
                    }
                    bw.write(line);
                    bw.newLine();

                } else {
                    if(bw != null) {
                        bw.write(line);
                        bw.newLine();
                    }
                }
            }
            if(bw != null) {
                bw.flush();
                bw.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

@@ -14,22 +14,20 @@
 d.slscs.
-bundle
+tool
 s;%0D%0A%0D%0A/*
@@ -1992,16 +1992,23 @@
 ream fos
+ = null
 ;%0D%0A     
@@ -2036,16 +2036,23 @@
 iter osw
+ = null
 ;%0D%0A     
@@ -2075,16 +2075,23 @@
 riter bw
+ = null
 ;%0D%0A     
@@ -2915,16 +2915,78 @@
       %0D%0A
+                    System.err.println(%22File: %22 + filename);%0D%0A
         
@@ -3000,16 +3000,17 @@
      if(
+!
 director
@@ -3010,20 +3010,24 @@
 irectory
- != 
+.equals(
 lastpath
@@ -3027,20 +3027,87 @@
 astpath)
+)
  %7B%0D%0A
+                        System.err.println(%22DIR: %22 + directory);%0D%0A
         

.equals, not ==