// $RCSfile: MakeLatexModule.java,v $
//
// Project "Principia"
//
// Michael Meyling
// Stoltenstrasse 38
// 22119 Hamburg
// Germany

package com.meyling.principia;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.meyling.principia.io.ParsingException;
import com.meyling.principia.io.Utility;
import com.meyling.principia.latex.Module2Latex;
import com.meyling.principia.module.ModuleContext;



/**
 * This class creates LaTeX files from Principia Mathematica II qedeq modules.
 *
 * @version $Revision: 1.10 $
 * @author  Michael Meyling
 */
public class MakeLatexModule {


    /**
     * Writes calling convention to <code>System.err</code>.
     */
    private static void info() {
        System.err.println("Name");
        System.err.println("----");
        System.err.println(Utility.getClassName(MakeLatexModule.class) + " - create LaTeX document(s)");
        System.err.println();
        System.err.println("Synopis");
        System.err.println("-------------------");
        System.err.println(MakeLatexModule.class.getName() + " [-h] [<module_address>]...");
        System.err.println();
        System.err.println("Description");
        System.err.println("-----------");
        System.err.println("This program creates LaTeX files out of *Principia Mathematica II* modules.");
        System.err.println("Such a module contains mathematical knowledge in a checkable form. This ");
        System.err.println("programm tries to load the modules by the given addresses. All neccassary");
        System.err.println("other modules (\"IMPORTS\") are also loaded.");
        System.err.println("Any new module is first copied into a local file buffer and then loaded");
        System.err.println("into memory. Further attempts to load the module lead to a search in the memory");
        System.err.println("and next in the local file buffer.");
        System.err.println("If you want to create a fresh copy, you have to delete the file buffer manually");
        System.err.println("(Normal file location: \"" + ModuleContext.getBufferDirectory() + "\").");
        System.err.println();
        System.err.println("The resulting LaTeX file is at the same place as the buffered module.");
        System.err.println();
        System.err.println("Options");
        System.err.println("-------");
        System.err.println("-h\twrites this text");
        System.err.println("module_address:");
        System.err.println("\taddress of a module to create a LaTeX file from, e.g.:");
        System.err.println("\thttp://www.qedeq.org/" + ModuleContext.getVersionDirectory() + "/proptheo1_1.00.00_1.00.00.qedeq");
        System.err.println();
        System.err.println("Examples");
        System.err.println("--------");
        System.err.println(MakeLatexModule.class.getName() + " http://www.qedeq.org/" + ModuleContext.getVersionDirectory() + "/proptheo1_1.00.00_1.00.00.qedeq");
        System.err.println();
        System.err.println("Prototype Warning");
        System.err.println("-----------------");
        System.err.println("This program suite is only a working prototype for the main project");
        System.err.println("*Hilbert II*. It concentrates on the basic logic foundation of");
        System.err.println("(mathematics. For more information about *Hilbert II* look at:");
        System.err.println("http://www.qedeq.org/");

    }


    /**
     * Create LaTeX modules.
     *
     * @param   args addresses of modules and options
     */
    public static void main(final String[] args) {

        try {
            PmiiFacade.init();
            doIt(args);
            PmiiFacade.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * Create LaTeX modules.
     *
     * @param   args addresses of modules and options.
     */
    public static void doIt(final String[] args) {

        final List address = new ArrayList();

        if (args.length == 0) {
            info();
            return;
        }
        for (int i = 0; i < args.length; i++) {
            if (args[i].startsWith("-")) {  // option
                final String option = args[i].substring(1).toLowerCase();
                if (option.equals("help") || option.equals("h")
                        || option.equals("?")) {
                    info();
                } else {                    // unknown option
                    System.err.println("\nUnknown option: " + option);
                    info();
                    return;
                }
            } else {                        // no option, must be file name
                address.add(args[i]);
            }
        }
        for (int i = 0; i < address.size(); i++) {
            makeLatexModule((String) address.get(i));
        }
    }


    /**
     * Create a LaTeX module.
     *
     * @param   address  address of module
     */
     public final static void makeLatexModule(final String address) {
        try {
            (new Module2Latex(address)).writeModule();
            return;
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        } catch (IOException e) {
            System.err.println(e.getMessage());
        } catch (ParsingException e) {
            System.err.println(new File(e.getAddress())
                .getAbsolutePath() + ":" + e.getRow() + ":"
                + e.getColumn() + ":");
            System.err.println(e.getDescription());
            System.err.println(e.getLine());
            final StringBuffer pointer = Utility.getSpaces(e.getColumn() - 1);
            pointer.append("^");
            System.err.println(pointer);
        }
        System.err.println("creation of LaTeX file failed for:");
        System.err.println("  " + address);
    }

}
