/**
 * Copyright (C) 2011, FuseSource Corp.  All rights reserved.
 * http://fusesource.com
 *
 * The software in this package is published under the terms of the
 * CDDL license a copy of which has been included with this distribution
 * in the license.txt file.
 */
package org.fusesource.fabric.commands;

import java.util.List;

import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.felix.gogo.commands.Option;
import org.fusesource.fabric.commands.support.EnsembleCommandSupport;
import org.fusesource.fabric.zookeeper.commands.Import;

@Command(name = "ensemble-create", scope = "fabric", description = "Create a new ZooKeeper ensemble", detailedDescription = "classpath:ensemble.txt")
public class EnsembleCreate extends EnsembleCommandSupport {

    @Option(name = "--clean", description = "Clean local zookeeper cluster and configurations")
    private boolean clean;

    @Option(name = "--no-import", description = "Disable the import of the sample registry data from ")
    private boolean noImport;

    @Option(name = "--import-dir", description = "Directory of files to import into the newly created ensemble")
    private String importDir = getDefaultImportDir();

    @Argument(required = false, multiValued = true, description = "List of agents")
    private List<String> agents;

    @Override
    protected Object doExecute() throws Exception {
        if (clean) {
            service.clean();
        } else {
            if (agents == null || agents.isEmpty()) {
                throw new IllegalStateException("No agents specified.");
            }
        }
        if (agents != null && !agents.isEmpty()) {
            service.createCluster(agents);

            // now lets populate the registry with files from a mvn plugin
            if (!noImport) {
                Import tool = new Import();
                tool.setBundleContext(getBundleContext());
                tool.setZooKeeper(service.getZooKeeper());
                tool.setSource(importDir);
                return tool.execute(session);
            }
        }
        return null;
    }

    private static String getDefaultImportDir() {
        return System.getProperty("karaf.home", ".") + "/import";
    }

}

@@ -1228,24 +1228,347 @@
 portDir();%0A%0A
+    @Option(name=%22-v%22, aliases=%7B%22--verbose%22%7D, description=%22Verbose output of files being imported%22)%0A    boolean verbose = false;%0A%0A    @Option(name=%22-t%22, aliases=%7B%22--time%22%7D, description=%22The amount of time to wait for the ensemble to startup before trying to import the default data%22)%0A    long ensembleStartupTime = 2000L;%0A%0A
     @Argumen
@@ -1635,24 +1635,24 @@
 of agents%22)%0A
-
     private 
@@ -2152,24 +2152,375 @@
 noImport) %7B%0A
+                // now lets sleep for a bit to give the ensemble chance to get ready :)%0A                if (ensembleStartupTime %3E 0L) %7B%0A                    try %7B%0A                        Thread.sleep(ensembleStartupTime);%0A                    %7D catch (InterruptedException e) %7B%0A                        // ignore%0A                    %7D%0A                %7D%0A%0A
             
@@ -2547,24 +2547,67 @@
 w Import();%0A
+                tool.setSource(importDir);%0A
             
@@ -2732,34 +2732,86 @@
     
-tool.setSource(importDir);
+if (verbose) %7B%0A                    tool.setVerbose(verbose);%0A                %7D
 %0A   

add a timeout before importing data to avoid ZK errors