// $Id: MakeCompressedModule.java,v 1.9 2004/04/26 05:29:53 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

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.module.ModuleContext;



/**
 * This class compresses Principia Mathematica II qedeq modules..
 *
 * @version $Revision: 1.9 $
 * @author  Michael Meyling
 */
public class MakeCompressedModule {


    /**
     * Writes calling convention to <code>System.err</code>.
     */
    private static void info() {
        System.err.println("Name");
        System.err.println("----");
        System.err.println(Utility.getClassName(MakeCompressedModule.class) + " - compress qedeq modules");
        System.err.println();
        System.err.println("Synopis");
        System.err.println("-------------------");
        System.err.println(MakeCompressedModule.class.getName() + " [-h] [<module_address>]...");
        System.err.println();
        System.err.println("Description");
        System.err.println("-----------");
        System.err.println("This program compresses *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("All unused proof lines will be removed and all proof lines that have the same");
        System.err.println("formula will be removed except for the first one.");
        System.err.println("The resulting module file replaces the original module in the file buffer.");
        System.err.println();
        System.err.println("For more information on Principia Mathematica II look at:");
        System.err.println("http://www.meyling.com/principia/principia.html");
        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 compress, 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(MakeCompressedModule.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 compressed 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 compressed 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++) {
            makeCompressedModule((String) address.get(i));
        }
    }


    /**
     * Create a rule reduced module.
     *
     * @param   address  address of module
     */
     public final static void makeCompressedModule(final String address) {
        try {
            ModuleContext.getInstance().compress(address);
            System.out.println("compressed:");
            System.out.println("  " + address);
            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("compression of module file failed for:");
        System.err.println("  " + address);
    }


}
