package com.sns.tools.ant.taskdefs;

import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.types.*;
import java.io.*;
import java.util.*;

/**
 * Call Ant in a sub-project
 *
 *  <pre>
 *  &lt;target name="foo" depends="init"&gt;
 *    &lt;ant2 antfile="build.xml" target="bar" keepenv="true"&gt;
 *      &lt;property name="property1" value="aaaaa" /&gt;
 *      &lt;property name="foo" value="baz" /&gt;
 *    &lt;/ant2&gt;
 *  &lt;/target&gt;
 *
 *OR
 *
 *  &lt;target name="foo2" depends="init"&gt;
 *    &lt;ant target="main" keepenv="true" overwrite="true"&gt;
 *      &lt;fileset dir="."&gt;
 *          &lt;include name="build-sub.xml"/&gt;
 *      &lt;/fileset&gt;
 *     &lt;/ant2&gt;
 *  &lt;/target&gt;
 *
 * &lt;target name="bar" depends="init"&gt;
 *    &lt;echo message="prop is ${property1} ${foo}" /&gt;
 * &lt;/target&gt;
 * </pre>
 *
 *
 * @author <a href="mailto:costin@dnt.ro">costin@dnt.ro</a>
 * @version 1.0
 * Adapted from org.apache.tools.ant.taskdefs.Ant.class 
 * by <a href=mailto:john.d.casey@mail.sprint.com>John Casey</a>, 3/08/2001
 */
public class Ant2 extends Task {

    private File dir = null;
    private String antFile = null;
    private String target = null;
    private String output = null;
    private boolean preserveEnv = false;
    private Vector filesets = null;
    private boolean overwrite = false;

    Vector properties=new Vector();
    Project p1;

    /**
     * Initialize the task.
     */
    public void init() {
        p1 = new Project();
        p1.setJavaVersionProperty();
        p1.addTaskDefinition("property", 
                             (Class)project.getTaskDefinitions().get("property"));
    }

    /**
     * Get this task ready to execute.
     */
    private void reinit() {
        init();
        for (int i=0; i<properties.size(); i++) {
            Property p = (Property) properties.elementAt(i);
            Property newP = (Property) p1.createTask("property");
            newP.setName(p.getName());
            if (p.getValue() != null) {
                newP.setValue(p.getValue());
            }
            if (p.getFile() != null) {
                newP.setFile(p.getFile());
            } 
            if (p.getResource() != null) {
                newP.setResource(p.getResource());
            }
            properties.setElementAt(newP, i);
        }
    }

    /**
     * Get the child project object ready for target execution.
     */
    private void initializeProject() {
        Vector listeners = project.getBuildListeners();
        for (int i = 0; i < listeners.size(); i++) {
            p1.addBuildListener((BuildListener)listeners.elementAt(i));
        }

        if (output != null) {
            try {
                PrintStream out = new PrintStream(new FileOutputStream(output));
                DefaultLogger logger = new DefaultLogger();
                logger.setMessageOutputLevel(Project.MSG_INFO);
                logger.setOutputPrintStream(out);
                logger.setErrorPrintStream(out);
                p1.addBuildListener(logger);
            }
            catch( IOException ex ) {
                log( "Ant: Can't set output to " + output );
            }
        }

        Hashtable taskdefs = project.getTaskDefinitions();
        Enumeration et = taskdefs.keys();
        while (et.hasMoreElements()) {
            String taskName = (String) et.nextElement();
            Class taskClass = (Class) taskdefs.get(taskName);
            p1.addTaskDefinition(taskName, taskClass);
        }

        Hashtable typedefs = project.getDataTypeDefinitions();
        Enumeration e = typedefs.keys();
        while (e.hasMoreElements()) {
            String typeName = (String) e.nextElement();
            Class typeClass = (Class) typedefs.get(typeName);
            p1.addDataTypeDefinition(typeName, typeClass);
        }

        // set user-define properties
        Hashtable prop1 = project.getProperties();
        e = prop1.keys();
        while (e.hasMoreElements()) {
            String arg = (String) e.nextElement();
            String value = (String) prop1.get(arg);
            p1.setProperty(arg, value);
        }
    }

    /**
     * Handle switching between andFile and fileset options, with preference to antFile.
     * In other words, if antFile is != null, then use that and ignore the fileset(s).
     */
    public void execute() throws BuildException {
        if(antFile == null){
            if((filesets != null) && (filesets.size() > 0)){
                FileSet fs = null;
                DirectoryScanner ds = null;
                String[] files = null;
                File baseDir = null;
                File tmp = null;
                int countFS = filesets.size();
                for(int j=0; j<countFS; j++){
                    fs = (FileSet)filesets.elementAt(j);
                    baseDir = fs.getDir(project);
                    this.setDir(baseDir);
                    ds = fs.getDirectoryScanner(project);
                    files = ds.getIncludedFiles();
                    int count = files.length;
                    log("Batch processing " + count + " ant files...");
                    for(int i=0; i<count; i++){
                        this.log("Processing antfile: " + files[i], project.MSG_VERBOSE);
                        executeAnt(files[i]);
                    }
                }
            }
            else{
                executeAnt("build.xml");
            }
        }
        else{
            executeAnt(antFile);
        }
    }
    
    /**
     * Do the execution.
     */
    private void executeAnt(String antfile) throws BuildException {
        try {
            if (p1 == null) {
                reinit();
            }
        
            if(dir == null) 
                dir = project.getBaseDir();

            initializeProject();

            p1.setBaseDir(dir);
            p1.setUserProperty("basedir" , dir.getAbsolutePath());
            
            // Override with local-defined properties
            Enumeration e = properties.elements();
            while (e.hasMoreElements()) {
                Property p=(Property) e.nextElement();
                p.execute();
            }
            
            if (antfile == null){ 
                log("antfile is null. Cannot proceed.", project.MSG_ERR);
                return;
            }

            File file = new File(antfile);
            if (!file.isAbsolute()) {
                antfile = (new File(dir, antfile)).getAbsolutePath();
                file = (new File(antfile)) ;
                if( ! file.isFile() ) {
                  throw new BuildException("Build file " + file + " not found.");
                }
            }

            p1.setUserProperty( "ant.file" , antfile );
            ProjectHelper.configureProject(p1, new File(antfile));
            
            if (target == null) {
                target = p1.getDefaultTarget();
            }

            // Are we trying to call the target in which we are defined?
            if (p1.getBaseDir().equals(project.getBaseDir()) &&
                p1.getProperty("ant.file").equals(project.getProperty("ant.file")) &&
                target.equals(this.getOwningTarget().getName())) { 

                throw new BuildException("ant task calling its own parent target");
            }

            p1.executeTarget(target);
            
            if(preserveEnv){
                // Integrate child environment into parent's.
                includeSettings(p1);
            }
            
        } finally {
            // help the gc
            p1 = null;
        }
    }

    /**
     * Sets the directory to look for build files under.
     *
     *@param d The directory to look for build files under.
     */
    public void setDir(File d) {
        this.dir = d;
    }

    /**
     * Sets the antfile to use.
     *
     *@s The name of the antfile to use.
     */
    public void setAntfile(String s) {
        this.antFile = s;
    }

    /**
     * Sets the target to execute within the child project.
     *
     *@param s The target name.
     */
    public void setTarget(String s) {
        this.target = s;
    }

    /**
     * Sets the logfile to send the child's output to.
     *
     *@param s The output filename.
     */
    public void setOutput(String s) {
        this.output = s;
    }

    /**
     * Sets the flag for propagating the child's environment back to the 
     * parent project.  This is useful for using a child project to set up
     * an environment that may be used from multiple build files.
     *
     *@param preserve Whether or not to keep the child's environment in the
     * parent project when execution of the child is finished.
     */
    public void setKeepenv(boolean preserve){
        this.preserveEnv = preserve;
    }
    
    /**
     * Sets the flag for overwriting the parent's environment settings with
     * those from the child.  This works in conjunction with the keepenv 
     * attribute.
     *
     *@param over Whether or not to overwrite the parent's environment with
     * duplicate values in the child project.
     */
    public void setOverwrite(boolean over){
        this.overwrite = over;
    }
    
    /**
     * Adds a fileset for batching.  This method and the dir/antfile attributes
     * <b>are mutually exclusive</b>.
     *
     *@param fs The fileset to add to the current batch of antfiles to run.
     */
    public void addFileset(FileSet fs){
        if(filesets == null){filesets = new Vector();}
        filesets.addElement(fs);
    }
    
    /**
     * Creates a property task in the child project.
     */
    public Property createProperty() {
        if (p1 == null) {
            reinit();
        }

        Property p=(Property)p1.createTask("property");
        p.setUserProperty(true);
        properties.addElement( p );
        return p;
    }

    /**
     * Includes the child environment into the parent project.
     *
     *@param from The child project to get the settings from.
     */
    protected void includeSettings(Project from){
        integrate(from.getDataTypeDefinitions(), project.getDataTypeDefinitions(), "dataTypeDefinition");
        integrate(from.getFilters(), project.getFilters(), "filter");
        integrate(from.getProperties(), project.getProperties(), "property");
        integrate(from.getReferences(), project.getReferences(), "reference");
        integrate(from.getTaskDefinitions(), project.getTaskDefinitions(), "taskDefinition");
        integrate(from.getUserProperties(), project.getUserProperties(), "userProperty");
    }
    
    /**
     * Integrates the settings from the first hashtable into the second
     * table.
     *
     *@param defs The settings to copy
     *@param parent The hashtable to store the child settings into
     *@param desc A descriptive name for the section of the environment we
     * are working with now. This is mostly for debugging.
     */
    private void integrate(Hashtable defs, Hashtable parent, String desc){
        Enumeration enum = defs.keys();
        Object name = null;
        while(enum.hasMoreElements()){
            name = enum.nextElement();
            if((overwrite) || (parent.get(name) == null)){
                log("Including " + desc + " from sub-project: name=" + name + ", value=" + defs.get(name).toString(), Project.MSG_VERBOSE);
                parent.put(name, defs.get(name));
            }
            else{
                log("NOT including " + desc + " from sub-project: name=" + name + ", value=" + defs.get(name).toString(), Project.MSG_VERBOSE);
            }
        }
    }
    
    /**
     * Integrates the settings from the first vector into the second
     * vector.
     *
     *@param defs The settings to copy
     *@param parent The vector to store the child settings into
     *@param desc A descriptive name for the section of the environment we
     * are working with now. This is mostly for debugging.
     */
    private void integrate(Vector defs, Vector parent, String desc){
        int count = defs.size();
        Object obj = null;
        for(int i=0; i<count; i++){
            obj = defs.get(i);
            if((overwrite) || (!parent.contains(obj))){
                parent.add(obj);
                log("Including " + desc + " from sub-project: " + obj.toString(), Project.MSG_VERBOSE);
            }
            else{
                log("NOT including " + desc + " from sub-project: " + obj.toString(), Project.MSG_VERBOSE);
            }
        }
    }

}
