package yuku.alkitabconverter.in_tsi_usfm;

import yuku.alkitabconverter.util.Usfm2Usfx;
import yuku.alkitabconverter.util.UsfmBookName;

import java.io.File;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.util.Scanner;

public class Proses1 {
	static String INPUT_TEXT_ENCODING = "utf-8";

	public static void main(String[] args) throws Exception {
		new Proses1().u(args);
	}

	private void u(final String[] args) throws Exception {
		final String INPUT_TEXT_1_DIR = args[0];
		final String MID_DIR = args[1].endsWith("/") ? args[1] : (args[1] + "/");

		for (final File inputFile : new File(INPUT_TEXT_1_DIR).listFiles(new FilenameFilter() {
			@Override
			public boolean accept(final File dir, final String name) {
				return name.endsWith(".usfm") || name.endsWith(".SFM");
			}
		})) {
			final Scanner sc = new Scanner(inputFile, INPUT_TEXT_ENCODING);

			PrintWriter splitFile = null;

			while (sc.hasNextLine()) { // look for "\id"
				String baris = sc.nextLine();

				if (baris.startsWith("\\id ")) {
					final String newId = baris.split(" ")[1];
					final int book_0 = UsfmBookName.toBookId(newId);

					final File outputFile = new File(MID_DIR, String.format("%02d-utf8.usfm", book_0));
					//noinspection ResultOfMethodCallIgnored
					outputFile.getParentFile().mkdir();

					splitFile = new PrintWriter(outputFile, "utf-8");
					continue;
				}

				// remove ALL BOM (0xfeff)
				if (baris.length() > 0) {
					baris = baris.replace("\ufeff", "");
				}

				// patch: Ganti \s1 dengan \s2
				baris = baris.replace("\\s1", "\\s2");

				splitFile.println(baris);
			}

			splitFile.close();
		}

		final File midFile = new File(MID_DIR);
		midFile.mkdirs();
		String[] usfms = midFile.list(new FilenameFilter() {
			@Override
			public boolean accept(File parent, String name) {
				return (name.endsWith("-utf8.usfm"));
			}
		});

		for (String usfm: usfms) {
			String usfx = usfm.replace(".usfm", ".usfx.xml");
			Usfm2Usfx.convert(MID_DIR + usfm, MID_DIR + usfx);
		}
	}
}

@@ -207,24 +207,50 @@
 rintWriter;%0A
+import java.util.Objects;%0A
 import java.
@@ -263,16 +263,16 @@
 canner;%0A
-
 %0Apublic 
@@ -505,24 +505,32 @@
 T_TEXT_1_DIR
+_OR_FILE
  = args%5B0%5D;%0A
@@ -608,21 +608,16 @@
 /%22);%0A%0A%09%09
-for (
 final Fi
@@ -628,14 +628,19 @@
 nput
+DirOr
 File 
-:
+=
  new
@@ -665,128 +665,145 @@
 _DIR
-).listFiles(new FilenameFilter() %7B%0A%09%09%09@Override%0A%09%09%09public boolean accept(final 
+_OR_FILE);%0A%09%09final File%5B%5D inputFileList;%0A%09%09if (inputDirOrFile.isDirectory()) %7B%0A%09%09%09inputFileList = inputDirOrFile.list
 File
- 
+s((
 dir, 
-final String name) %7B%0A%09%09%09%09return
+name) -%3E
  nam
@@ -846,26 +846,148 @@
 (%22.SFM%22)
+)
 ;%0A%09%09
-%09%7D%0A%09%09%7D
+%7D else %7B%0A%09%09%09inputFileList = new File%5B%5D%7BinputDirOrFile%7D;%0A%09%09%7D%0A%0A%09%09for (final File inputFile : Objects.requireNonNull(inputFileList
 )) %7B%0A%09%09%09
@@ -1480,24 +1480,86 @@
 ).mkdir();%0A%0A
+%09%09%09%09%09if (splitFile != null) %7B%0A%09%09%09%09%09%09splitFile.close();%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%09splitFi

support single file input and close split file