/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.installation;

import java.io.File;

import org.bodington.ant.TemplateBuilderTask;

/**
 * Class for compiling templates standalone.
 * @author bmb6jrm
 * @author Alexis O'Connor
 * @see org.bodington.ant.TemplateBuilderTask
 * @deprecated Use {@link org.bodington.ant.TemplateBuilderTask} directly 
 * instead.
 */
public class TemplateBuilder
{
    /** Creates new TemplateBuilder */
    private TemplateBuilder()
    {
    }
    
    /**
     * Main method for executing this class from the command-line.
     * 
     * <pre>
     * 
     *       Usage: TemplateBuilder -templateDir &lt;template dir&gt; 
     *       -classesDir &lt;classes dir&gt; -classpath &lt;classpath&gt;  
     *       [-xmlWarn]
     *  
     * </pre>
     * 
     * <ul>
     * <li><code>-help</code>: print the usage message.
     * <li><code>-templateDir</code>: the location of the source (HTML)
     * templates.
     * <li><code>-classesDir</code>: the output location of the template
     * class files.
     * <li><code>-classpath</code>: the CLASSPATH to be used for template
     * compilation.
     * <li><code>-xmlWarn</code>: Report errors and warnings in the XML
     * templates.
     * </ul>
     * @param args the command line arguments.
     */
    public static void main(String args[])
    {
    String usage = "Usage: TemplateBuilder -templateDir <template dir> "
        + "-classesDir <classes dir> -classpath <classpath>  "
        + "[-xmlWarn]\n"
        + "\t-help: print this usage message.\n" 
        + "\t-templateDir: the location of the source (HTML) templates.\n"
        + "\t-classesDir: the output location of the template class files.\n"
        + "\t-classpath: the CLASSPATH to be used for template compilation.\n"
        + "property (this needs to be the directory containing the dtd "
        + "directory, e.g. bodington/WEB-INF).\n"
        + "\t-xmlWarn: Report errors and warnings in the XML templates."; 
        
    TemplateBuilderTask task = new TemplateBuilderTask();
    
    // Process the command-line arguments:
    try
    {
        for (int i = 0; i < args.length; i++)
        {
            if ( "-help".equals( args[i] ) )
            {
                System.out.println( usage );
                System.exit( 0 );
            }
            else if ( "-templateDir".equals( args[i] ) )
                task.setTemplatedir(new File( args[++i] ));
            else if ( "-classesDir".equals( args[i] ) )
                task.setTemplateclasses(new File( args[++i]) );
            else if ( "-classpath".equals( args[i] ) )
                task.setBuildpath(args[++i]);
            else if ( "-xmlWarn".equals( args[i] ) )
                task.setXmlWarn(true);
        }
    }
    catch ( ArrayIndexOutOfBoundsException e )
    {
        System.err.println( "Mandatory arguments are missing." );
        System.err.println( usage );
        System.exit( 1 );
    }
    
    try
    {
        task.execute();
    }
    catch (RuntimeException e)
    {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    }
}
