import com.lowagie.text.DocumentException;
import grammar.AstParser;
import grammar.FunctionReducer;
import nottexast.NottexNode;
import org.apache.commons.cli.*;
import org.w3c.dom.Document;
import pdfgen.PDFCreator;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

import static pdfgen.XmlTranslator.createDocument;

public class Main {

    private static final boolean inProduction = false;

    public static void main(String[] args) throws IOException, DocumentException, TransformerException, ParseException {

        if (inProduction) {
            compile(args);

        } else {
            String input = getFileContent("src/main/resources/input.ntex".replaceAll("/", File.separator));
            NottexNode astTree = AstParser.parse(input);
            NottexNode reducedTree = FunctionReducer.reduceFunctions(astTree);
            Document xmlDocument = createDocument(reducedTree);
            PDFCreator.convertDocumentToPDF(xmlDocument, "src/main/resources/test.pdf".replaceAll("/", File.separator));

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            //Create XML
            DOMSource source = new DOMSource(xmlDocument);
            StreamResult result = new StreamResult(new File("src/main/resources/test.xhtml".replaceAll("/", File.separator)));
            transformer.transform(source, result);
        }

    }

    private static void compile(String[] args) throws IOException, DocumentException, TransformerException, ParseException {

        Options options = new Options();
        options.addOption("i", "input-file", true, "Path and full name of input .ntex file.");
        options.addOption("d", "debug", false, "Generates additional debugging files and info.");
        options.addOption("o", "output-file", true, "Path and full name of output .pdf file.");

        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = parser.parse(options, args);

        if (!cmd.hasOption("i")) {
            throw new RuntimeException("Missing input file, provide it with flag -i or --input-file.");
        }

        String inFilename = new File(cmd.getOptionValue("i")).getAbsolutePath();
        String input = getFileContent(inFilename);
        String outDefaultFilename = removeFileExtension(inFilename) + ".pdf";
        String outFilename = cmd.getOptionValue("o", outDefaultFilename);

        // Parse & generate output
        NottexNode astTree = AstParser.parse(input);
        NottexNode reducedTree = FunctionReducer.reduceFunctions(astTree);
        Document xmlDocument = createDocument(reducedTree);
        PDFCreator.convertDocumentToPDF(xmlDocument, outFilename);

        // Output debug info
        if (cmd.hasOption("d")) {
            // Create XML
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource source = new DOMSource(xmlDocument);
            StreamResult result = new StreamResult(new File(removeFileExtension(outFilename) + ".xhtml"));
            transformer.transform(source, result);
        }

    }

    private static String removeFileExtension(String filename) {
        return filename.substring(0, filename.lastIndexOf("."));
    }



    private static String getFileContent(String filePath) throws IOException {
        return Files.readAllLines(Paths.get(filePath)).stream().collect(Collectors.joining("\n"));
    }
}

@@ -840,30 +840,32 @@
 Content(%22src
-/
+%5C%5C
 main
-/
+%5C%5C
 resources/in
@@ -861,17 +861,18 @@
 esources
-/
+%5C%5C
 input.nt
@@ -878,40 +878,8 @@
 tex%22
-.replaceAll(%22/%22, File.separator)
 );%0A 
@@ -1134,30 +1134,32 @@
 cument, %22src
-/
+%5C%5C
 main
-/
+%5C%5C
 resources/te
@@ -1151,25 +1151,26 @@
 n%5C%5Cresources
-/
+%5C%5C
 test.pdf%22.re
@@ -1170,40 +1170,8 @@
 pdf%22
-.replaceAll(%22/%22, File.separator)
 );%0A%0A
@@ -1480,14 +1480,16 @@
 %22src
-/
+%5C%5C
 main
-/
+%5C%5C
 reso
@@ -1493,17 +1493,18 @@
 esources
-/
+%5C%5C
 test.xht
@@ -1510,40 +1510,8 @@
 tml%22
-.replaceAll(%22/%22, File.separator)
 ));%0A

Revert "Once they were united; now they're separated properly"