/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.servicemix.gshell.features.internal;

import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.geronimo.gshell.command.Command;
import org.apache.geronimo.gshell.obr.ObrCommandSupport;
import org.apache.geronimo.gshell.support.OsgiCommandSupport;
import org.apache.servicemix.gshell.features.Feature;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.obr.RepositoryAdmin;

/**
 * Simple command which prompts the user and installs the needed bundles.
 * The bundles need to be available through the OBR repositories.
 */
public class CommandProxy extends ObrCommandSupport {

    private Feature feature;
    private ServiceRegistration registration;

    public CommandProxy(Feature feature, BundleContext bundleContext) {
        this.feature = feature;
        setBundleContext(bundleContext);
        Properties props = new Properties();
        props.put("shell", feature.getName());
        props.put("rank", "-1");
        registration = bundleContext.registerService(Command.class.getName(), this, props);
    }

    protected OsgiCommandSupport createCommand() {
        return this;
    }

    @Deprecated
    public String getId() {
        return feature.getName();
    }

    @Deprecated
    public String getDescription() {
        return feature.getName();
    }

    protected void doExecute(RepositoryAdmin admin) throws Exception {
        io.out.println("This feature is not yet installed.  Do you want to install it (y/n) ? ");
        int c = io.in.read();
        if (c == 'y' || c == 'Y') {
            io.out.println("Installing feature.  Please wait...");
            registration.unregister();
            deploy(admin);
        }
    }

    protected void deploy(RepositoryAdmin admin) throws Exception {
        List<String> bundles = Arrays.asList(feature.getBundles());
        int idx0 = -1;
        int idx1 = -1;
        while (++idx0 < bundles.size()) {
            if (bundles.get(idx0).startsWith("obr:")) {
                if (idx1 < 0) {
                    idx1 = idx0;
                }
                bundles.set(idx0, bundles.get(idx0).substring("obr:".length()));
            } else {
                if (idx1 >= 0) {
                    doDeploy(admin, bundles.subList(idx1, idx0), true);
                    idx1 = -1;
                } else {
                    Bundle bundle = getBundleContext().installBundle(bundles.get(idx0), null);
                    bundle.start();
                }
            }
        }
        if (idx1 >= 0) {
            doDeploy(admin, bundles.subList(idx1, idx0), true);
        }
    }

    private String readLine(Reader in) throws IOException {
        StringBuffer buf = new StringBuffer();
        while (true) {
            int i = in.read();
            if ((i == -1) || (i == '\n') || (i == '\r')) {
                return buf.toString();
            }
            buf.append((char) i);
        }
    }
}

@@ -929,16 +929,46 @@
 Arrays;%0A
+import java.util.Collections;%0A
 import j
@@ -2858,31 +2858,8 @@
 -1;%0A
-        int idx1 = -1;%0A
     
@@ -2972,211 +2972,78 @@
     
-if (idx1 %3C 0) %7B%0A                    idx1 = idx0;%0A                %7D%0A                bundles.set(idx0, bundles.get(idx0).substring(%22obr:%22.length()));%0A            %7D else %7B%0A                if (idx1 %3E= 0) %7B%0A 
+String%5B%5D b = bundles.get(idx0).substring(%22obr:%22.length()).split(%22,%22);%0A
     
@@ -3046,35 +3046,32 @@
                 
-   
 doDeploy(admin, 
@@ -3074,79 +3074,33 @@
 in, 
-bundles.subList(idx1, idx0), true);%0A                    idx1 = -1;%0A    
+Arrays.asList(b), true);%0A
     
@@ -3116,20 +3116,16 @@
  else %7B%0A
-    
         
@@ -3223,20 +3223,16 @@
         
-    
 bundle.s
@@ -3255,126 +3255,9 @@
     
-    %7D%0A            %7D%0A        %7D%0A        if (idx1 %3E= 0) %7B%0A            doDeploy(admin, bundles.subList(idx1, idx0), true);
+%7D
 %0A   

Fix jbi feature deployment