/*
 * Copyright (C) 2015 AnySoftKeyboard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public class MainClass {

    public static void main(String[] args)
            throws IOException, ParserConfigurationException, SAXException {
        if (args.length != 3) {
            System.out.println(
                    "Usage: makedictionary [path-to-input-file] [path-to-pack-resource-folder] [prefix]");
            System.exit(1);
        }

        final File inputFile = new File(args[0]);
        final File resourcesFolder = new File(args[1]);

        buildDictionary(inputFile, resourcesFolder, args[2]);
    }

    public static void buildDictionary(
            final File inputFile, final File resourcesFolder, final String prefix)
            throws IOException, ParserConfigurationException, SAXException {
        if (!inputFile.isFile() || !inputFile.exists()) {
            throw new IllegalArgumentException("Could not find input file " + inputFile);
        }
        final File tempOutputFile = File.createTempFile("make_dictionary_temp", "bin");

        if (!resourcesFolder.isDirectory() || !resourcesFolder.exists()) {
            throw new IllegalArgumentException("Could not find resource folder " + resourcesFolder);
        }

        final File outputFolder = new File(resourcesFolder, "raw/");
        final File dict_id_array =
                new File(resourcesFolder, "values/" + prefix + "_words_dict_array.xml");

        System.out.println("Reading words from input " + inputFile.getAbsolutePath());
        System.out.println(
                "Will store output files under "
                        + outputFolder.getAbsolutePath()
                        + ". Created raw folder? "
                        + outputFolder.mkdirs());
        System.out.println("Deleting previous versions...");

        // deleting current files
        tempOutputFile.delete();
        File[] dictFiles = outputFolder.listFiles((dir, name) -> name.endsWith(".dict"));
        if (dictFiles != null && dictFiles.length > 0) {
            for (File file : dictFiles) {
                file.delete();
            }
        }
        dict_id_array.delete();

        MakeBinaryDictionary maker =
                new MakeBinaryDictionary(
                        inputFile.getAbsolutePath(), tempOutputFile.getAbsolutePath());
        maker.makeDictionary();

        if (!tempOutputFile.exists()) {
            throw new IOException("Failed to create binary dictionary file.");
        }

        if (tempOutputFile.length() <= 0) {
            throw new IOException("Failed to create binary dictionary file. Size zero.");
        }

        // now, if the file is larger than 1MB, I'll need to split it to 1MB chunks and rename them.
        BinaryDictionaryResourceNormalizer normalizer =
                new BinaryDictionaryResourceNormalizer(
                        tempOutputFile, outputFolder, dict_id_array, prefix);
        normalizer.writeDictionaryIdsResource();

        System.out.println("Done.");
    }
}

@@ -1974,132 +1974,8 @@
 /%22);
-%0A        final File dict_id_array =%0A                new File(resourcesFolder, %22values/%22 + prefix + %22_words_dict_array.xml%22);
 %0A%0A  
@@ -2670,40 +2670,8 @@
    %7D
-%0A        dict_id_array.delete();
 %0A%0A  
@@ -3244,16 +3244,629 @@
 e them.%0A
+        final File dictResFolder = new File(resourcesFolder, %22values%22);%0A        if (!dictResFolder.isDirectory() && !dictResFolder.mkdirs()) %7B%0A            throw new IOException(%0A                    %22Failed to create resource folder %22 + dictResFolder.getAbsolutePath());%0A        %7D%0A        final File dictIdArray = new File(dictResFolder, prefix + %22_words_dict_array.xml%22);%0A        if (dictIdArray.isFile() && !dictIdArray.delete()) %7B%0A            throw new IOException(%0A                    %22Failed to delete dict-id-array before recreation! %22%0A                            + dictIdArray.getAbsolutePath());%0A        %7D%0A
         
@@ -4031,13 +4031,11 @@
 dict
-_id_a
+IdA
 rray

Fix dictionary ID file creation