package org.shackelford.ant;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.tools.ant.Main;

/**
 * Executes Ant launching a version specific build.xml
 * 
 * <p>using the -buildfile option overrides the special functionality of this
 * class. If the required property file and key are not located somewhere on
 * the classpath, the AntRunner will use the default build.xml file.</p>
 *
 * <p>Note that this class needs to be rewritten to support JDK 1.x.</p>
 *
 * <p>Thanks to <a href="mailto:DDevienne@lgc.com">Dominique Devienne</a>
 * for suggesting this approach.</p>
 *
 * @author <a href="mailto:shacjo@ncs.com">John-Mason P. Shackelford</a>
 * @version $Revision:   1.0  $, $Date:   Jul 24 2002 12:07:44  $
 */
public class AntRunner {

    public static String PROPERTY_FILE = "local.build.properties";
    public static String PROPERTY_FILE_ALTERNATE = "build.properties";

    public static String PROPERTY_KEY = "application.build.version";

    public static void main(String[] args) {

        Date time = new Date();
        System.out.println("-------------------------------------------------------");
        System.out.println("AntRunner        " + time);
        System.out.println("-------------------------------------------------------\n");

        //        // debugging -- iterate through args
        //        System.out.println("INPUT:");
        //        for (int i = 0; i < args.length; i++) {
        //            System.out.println((i + 1) + ". " + args[i]);
        //        }
        //        if (args.length == 0) {
        //            System.out.println("No args");
        //        }
        //

        List myArgs = new ArrayList();
        boolean isBuildfileSpecified = false;
        int buildfileAtElement = 0;

        // process the command line arguments to include our special version
        // swapping and strip the old AntRunner if it should appear
        for (int i = 0; i < args.length; i++) {

            if (!args[i].equals("org.apache.tools.ant.Main")) {

                myArgs.add(args[i]);
            }

            if (args[i].equalsIgnoreCase("-buildfile")) {
                isBuildfileSpecified = true;
                buildfileAtElement = i + 1;
            }

        }

        // get the build version from a property file
        String version = getBuildVersion(PROPERTY_FILE, PROPERTY_KEY);

        if (version == null || version.length() < 1)
            version = getBuildVersion(PROPERTY_FILE_ALTERNATE, PROPERTY_KEY);

        // add our own buildfile specification based on our version

        String buildfile = null;
        if (!isBuildfileSpecified) {
            if (version == null || version.length() < 1) {
                System.out.println("Using default buildfile: build.xml.");
            } else {
                buildfile = "build-" + version + ".xml";
                System.out.println(
                    "Using buildfile for version " + version + ": " + buildfile + ".");
                myArgs.add("-buildfile");
                myArgs.add(buildfile);
            }
        } else {
            System.out.println("Using the specified buildfile: " + args[buildfileAtElement] + ".");
        }

        // launch ant
        String[] newArgs = (String[]) myArgs.toArray(new String[0]);

        //        // debugging -- iterate through args
        //        for (int i = 0; i < newArgs.length; i++) {
        //            System.out.println((i + 1) + ". " + newArgs[i]);
        //        }
        //        if (newArgs.length == 0) {
        //            System.out.println("No args");
        //        }
        Main.main(newArgs);
    }

    /**
     * load up the build version from the specified key in the named 
     * property file located somewhere on the classpath
     * @param file the file name of the property file to load 
     * (Do not include path information!)
     * @param key the property key to use for the version
     */
    private static String getBuildVersion(String file, String key) {

        // get the property file 
        System.out.println("Searching the classpath for " + file + ".");
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        InputStream stream = classLoader.getResourceAsStream(file);

        Properties props = null;

        if (stream != null) {
            props = new Properties();
            try {
                props.load(stream);
            } catch (IOException ioe) {}
        }

        // if the property file is found and a build version is 
        // specified, use it to launch the correct build.xml
        // otherwise run ant as usual
        String version = null;

        if (props == null) {
            System.out.println("The " + file + " file was not found.");
        } else {
            version = props.getProperty(key);
            if (version == null || version.length() < 1) {
                System.out.println("The " + key + " key was not found.");
            } else {
                System.out.println("Running Ant for application version " + version + ".");
            }
        }

        return version;
    }
}

