/**
 * 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.zookeeper.commands;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
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.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;

@Command(name = "create", scope = "zk", description = "Create a node")
public class Create extends ZooKeeperCommandSupport {

    @Option(name = "-e", aliases = {"--ephemeral"}, description = "Create an ephemeral node")
    boolean ephemeral;

    @Option(name = "-s", aliases = {"--sequential"}, description = "Create a sequential node")
    boolean sequential;

    @Option(name = "-r", aliases = {"--recursive"}, description = "Automatically create parents")
    boolean recursive;

    @Option(name = "-i", aliases = {"--import"}, description = "Import data from an url")
    boolean importUrl;

    @Option(name = "-a", aliases = {"--acl"}, description = "Node ACLs")
    String acl;

    @Argument(index = 0, required = true, description = "Path of the node to create")
    String path;

    @Argument(index = 1, required = false, description = "Data for the node, or url if 'import' option is used")
    String data;



    @Override
    protected Object doExecute() throws Exception {
        List<ACL> acls = acl == null ? ZooDefs.Ids.OPEN_ACL_UNSAFE : parseACLs(acl);
        CreateMode mode;
        if (ephemeral && sequential) {
            mode = CreateMode.EPHEMERAL_SEQUENTIAL;
        } else if (ephemeral) {
            mode = CreateMode.EPHEMERAL;
        } else if (sequential) {
            mode = CreateMode.PERSISTENT_SEQUENTIAL;
        } else {
            mode = CreateMode.PERSISTENT;
        }

        String nodeData = data;

        if (importUrl) {
            nodeData = loadUrl(new URL(data));
        }

        if (recursive) {
            getZooKeeper().createWithParents(path, nodeData, acls, mode);
        } else {
            getZooKeeper().create(path, nodeData, acls, mode);
        }
        return null;
    }
}

@@ -666,16 +666,61 @@
 teMode;%0A
+import org.apache.zookeeper.KeeperException;%0A
 import o
@@ -748,16 +748,16 @@
 ooDefs;%0A
-
 import o
@@ -1473,24 +1473,163 @@
 tring acl;%0A%0A
+    @Option(name = %22-o%22, aliases = %7B%22--overwrite%22%7D, description = %22Overwrite existing entry if it already exists%22)%0A    boolean overwrite;%0A%0A
     @Argumen
@@ -2457,32 +2457,50 @@
 a));%0A        %7D%0A%0A
+        try %7B%0A    
         if (recu
@@ -2504,24 +2504,28 @@
 ecursive) %7B%0A
+    
             
@@ -2586,32 +2586,36 @@
  mode);%0A        
+  
+  
 %7D else %7B%0A       
@@ -2599,32 +2599,36 @@
        %7D else %7B%0A
+    
             getZ
@@ -2666,32 +2666,248 @@
 a, acls, mode);%0A
+            %7D%0A        %7D catch (KeeperException.NodeExistsException e) %7B%0A            if(overwrite) %7B%0A                getZooKeeper().setData(path, nodeData);%0A            %7D else %7B%0A                throw e;%0A            %7D%0A
         %7D%0A      

Support an --overwrite command so that zk:create can be used to create or update an entry.