/*
 * @(#)CompactMindprod.java
 *
 * Summary: Feeds the entire Mindprod website to the Compactor to squish HTML down to smallest size.
 *
 * Copyright: (c) 2009-2010 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  2.8 2009-04-04 - no longer correct missing entities. Just issue warning messages.
 */
package com.mindprod.compactor;

import com.mindprod.commandline.CommandLine;
import com.mindprod.filter.AllDirectoriesFilter;
import com.mindprod.filter.ExtensionListFilter;
import com.mindprod.htmlmacros.Configure;

import java.io.File;

/**
 * Feeds the entire Mindprod website to the Compactor to squish HTML down to smallest size.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.8 2009-04-04 - no longer correct missing entities. Just issue warning messages.
 * @since 2009
 */
public final class CompactMindprod
    {
    // ------------------------------ CONSTANTS ------------------------------

    /**
     * root html directory as a File.
     */
    private static final File localWebrootDir =
            new File( Configure.LOCAL_WEBROOT_WITH_BACKSLASHES );

    /**
     * where we build list of files to process, contains fully qualified file names.
     */
    private static CommandLine filesToProcess;

    // -------------------------- STATIC METHODS --------------------------

    /**
     * Get fully qualified name of all the files to process.
     */
    private static void gatherFiles()
        {
        String[] args = new String[Configure.HTML_DIRS.length + Configure.INCLUDE_DIRS.length + 1];
        args[ 0 ] = "-q";
        int i = 1;
        for ( String aDir : Configure.HTML_DIRS )
            {
            args[ i++ ] = localWebrootDir + "\\" + aDir;
            }
        // add include dirs
        for ( String aDir : Configure.INCLUDE_DIRS )
            {
            args[ i++ ] = localWebrootDir + "\\" + aDir;
            }
        filesToProcess = new CommandLine( args, new AllDirectoriesFilter(), new ExtensionListFilter( "html" ) );
        }

    // --------------------------- main() method ---------------------------

    /**
     * Expands macros written in Java in HTML files files.
     *
     * @param args names of files to process, no wildcards.
     */
    public static void main( String[] args )
        {
        // anything on command line gets all files redone.
        // nothing, default gets recently changed files only.
        System.out.println( "Compacting the entire Mindprod website." );

        gatherFiles();

        System.out.println( filesToProcess.size() + " files gathered for CompactMindprod" );
        final Compactor compactor = new Compactor();
        for ( File fileToProcess : filesToProcess )

            {
            assert fileToProcess != null;
            assert fileToProcess.exists() : "file to process missing";
            try
                {
                compactor.compactFile( true, fileToProcess );
                }
            catch ( Exception e )
                {
                System.out
                        .println( "Compactor failed: "
                                  + e.getMessage()
                                  + " in file "
                                  + fileToProcess );
                System.out.println();
                e.printStackTrace();
                }
            }
        System.out.println( "done" );
        // don't remove the System.exit( 0 ). It bypasses a Sun bug.
        // It works OK in JET.
        System.exit( 0 );
        }
    }// end CompactMindprod
