/*
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.bootstrap;

import java.io.File;
import java.util.ServiceLoader;

import org.jboss.forge.addon.dependency.spi.DependencyResolver;
import org.jboss.forge.addon.manager.AddonManager;
import org.jboss.forge.addon.manager.InstallRequest;
import org.jboss.forge.container.AddonId;
import org.jboss.forge.container.Forge;

/**
 * A class with a main method to bootstrap Forge.
 * 
 * You can deploy addons by calling {@link Bootstrap#deploy(String)}
 * 
 * @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
 * 
 */
public class Bootstrap
{
   private final Forge forge;
   private final AddonManager addonManager;
   private boolean exitAfter = false;

   public static void main(final String[] args)
   {
      Bootstrap bootstrap = new Bootstrap(args);
      bootstrap.start();
   }

   public Bootstrap(String[] args)
   {
      String installAddon = null;
      forge = new Forge();
      if (args.length > 0 && args.length % 2 == 0)
      {
         for (int i = 0; i < args.length; i++)
         {
            if ("--install".equals(args[i]))
            {
               installAddon = args[++i];
            }
            if ("--addonDir".equals(args[i]))
            {
               forge.setAddonDir(new File(args[++i]));
            }
            if ("--batchMode".equals(args[i]))
            {
               forge.setServerMode(false);
            }
         }
      }

      final DependencyResolver dependencyResolver = lookup(DependencyResolver.class);
      addonManager = new AddonManager(forge.getRepository(), dependencyResolver);

      if (installAddon != null)
      {
         deploy(installAddon);
      }
   }

   public void start()
   {
      if (!exitAfter)
         forge.start();
   }

   public void deploy(String addonCoordinates)
   {
      try
      {
         AddonId addon = AddonId.fromCoordinates(addonCoordinates);
         InstallRequest request = addonManager.install(addon);
         request.perform();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         exitAfter = true;
      }
   }

   private <T> T lookup(Class<? extends T> service)
   {
      return ServiceLoader.load(service).iterator().next();
   }

}

@@ -1068,16 +1068,49 @@
 = null;%0A
+      String removeAddon = null;%0A
       fo
@@ -1124,24 +1124,24 @@
 ew Forge();%0A
-
       if (ar
@@ -1351,32 +1351,144 @@
 ;%0A            %7D%0A
+            if (%22--remove%22.equals(args%5Bi%5D))%0A            %7B%0A               removeAddon = args%5B++i%5D;%0A            %7D%0A
             if (
@@ -1939,24 +1939,16 @@
 = null)%0A
-      %7B%0A
         
@@ -1968,33 +1968,88 @@
 llAddon);%0A      
-%7D
+if (removeAddon != null)%0A         undeploy(removeAddon);
 %0A   %7D%0A%0A   public
@@ -2316,24 +2316,24 @@
 all(addon);%0A
-
          req
@@ -2487,24 +2487,402 @@
     %7D%0A   %7D%0A%0A
+   public void undeploy(String addonCoordinates)%0A   %7B%0A      try%0A      %7B%0A         AddonId addon = AddonId.fromCoordinates(addonCoordinates);%0A         forge.getRepository().disable(addon);%0A         forge.getRepository().undeploy(addon);%0A      %7D%0A      catch (Exception e)%0A      %7B%0A         e.printStackTrace();%0A      %7D%0A      finally%0A      %7B%0A         exitAfter = true;%0A      %7D%0A   %7D%0A%0A
    private %3C

Install not picking up resources, from UI, but all else seems good