package main.java.latexee.runtime;

import java.util.ArrayList;

import main.java.latexee.docast.ParsedStatement;
import main.java.latexee.logging.Logger;
import main.java.latexee.utils.DeclarationParser;
import main.java.latexee.utils.DocumentParser;
import main.java.latexee.utils.GrammarGenerator;
import main.java.latexee.utils.OutputWriter;

import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.PosixParser; // Default or posix?
//import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
//import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.ParseException;



public class Main {
    public static String outputFile;
    public static String inputFile;
    public static boolean verbose = false;
    
    
    /* Construct options for the command line parser. */
    private static Options constructOptions() {
        final Options options = new Options();
        
        Option o = OptionBuilder.withArgName( "file" )
                                .hasArg()
                                .withDescription( "output will be here")
                                .create( "o" );
        
        options.addOption(o);
        options.addOption("v", "be verbose");
        options.addOption("h", "do you need help?");
        
        return options;
    }
    /* Use the command line parser */
    private static void useParser(final String[] args) {
        final CommandLineParser parser = new PosixParser();    
        final Options options = constructOptions();  

        CommandLine cmd;     
        try {
            cmd = parser.parse(options, args);
            // If asked for help or there are no arguments.
            if (args.length == 0 || cmd.hasOption("h")) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp( "LaTeXEE [inputFile]", options );
            }
            if (args.length > 0) {
                // input file can not start with "-"
                if (args[0].charAt(0) != '-') {
                    inputFile = args[0];
                    Logger.log("Inputfile is: " + inputFile);
                    
                    // If verbose is enabled
                    if (cmd.hasOption("v")) {
                        verbose = true;
                        System.out.println("Verbose option enable. Enjoy your text");
                    }
                                
                    if (cmd.hasOption ("o")) {
                        // Check if can write there, etc... Legal path, warning
                        outputFile = cmd.getOptionValue("o");
                        System.out.println("Outputfile is: " + outputFile);
                    }
                    else {
                        // Default outputfile is the same as input file, but change extension.
                        // If no extension, then just add extension
                        if (inputFile.indexOf('.') == -1) {
                            outputFile = inputFile + ".txt";
                        } else {
                            outputFile = inputFile.substring(0, inputFile.lastIndexOf(".")) + ".txt";
                        }
                        System.out.println("Outputfile is: " + outputFile);
                    }
                }
                else {
                    System.out.println("No inputfile specified");
                }
               
            }

            
        }
        catch (ParseException parseException) {
            System.err.println("Encountered exception while parsing arguments\n"  
                + parseException.getMessage());
        }
    }
    
    
	public static void main(String[] args) {
        
                // Call parser.
                useParser(args);   
        
		//quick way to enable fast testing during writing. Uncomment to test.
        	//args = new String[] {"src/test/antlr/basic_with_declare.tex"};
        	//String outputFile = "output.txt";
        	//String inputFile = args[0];
        	if (inputFile != null) {
                    ParsedStatement AST = DocumentParser.parse(inputFile);
                    DeclarationParser.declarationFinder(AST);
                    OutputWriter.formulasToTXT(AST, outputFile);
                    Logger.log("Finished without errors");
                }
	}
}

@@ -3895,32 +3895,24 @@
  %7B%0A        %0A
-        
         // C
@@ -3923,24 +3923,16 @@
 parser.%0A
-        
         
@@ -4032,27 +4032,18 @@
 o test.%0A
-        %09//
+%09%09
 args = n
@@ -4097,27 +4097,18 @@
 .tex%22%7D;%0A
-        %09//
+%09%09
 String o
@@ -4137,19 +4137,10 @@
 t%22;%0A
-        %09//
+%09%09
 Stri
@@ -4163,24 +4163,17 @@
 rgs%5B0%5D;%0A
-        
+%09
 %09if (inp
@@ -4186,32 +4186,24 @@
  != null) %7B%0A
-        
             
@@ -4269,24 +4269,16 @@
         
-  
-      
 Declarat
@@ -4323,24 +4323,16 @@
         
-    
-    
 OutputWr
@@ -4376,32 +4376,24 @@
             
-      
-  
 Logger.log(%22
@@ -4415,32 +4415,24 @@
 t errors%22);%0A
-        
         %7D%0A%09%7D

Revert "#12 #13 Forgot to uncomment lines."