/**
 *  Copyright 2005-2014 Red Hat, Inc.
 *
 *  Red Hat 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 io.fabric8.boot.commands;

import io.fabric8.api.Container;
import io.fabric8.api.FabricService;
import io.fabric8.api.RuntimeProperties;
import org.apache.felix.gogo.commands.Command;
import org.apache.karaf.shell.console.AbstractAction;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationAdmin;

@Command(name = WelcomeCommand.FUNCTION_VALUE, scope = WelcomeCommand.SCOPE_VALUE, description = WelcomeCommand.DESCRIPTION)
final class WelcomeAction extends AbstractAction {

    private final FabricService fabricService;
    private final ConfigurationAdmin configAdmin;
    private final BundleContext bundleContext;
    private final RuntimeProperties runtimeProperties;

    WelcomeAction(BundleContext bundleContext, FabricService fabricService, ConfigurationAdmin configAdmin, RuntimeProperties runtimeProperties) {
        this.bundleContext = bundleContext;
        this.fabricService = fabricService;
        this.configAdmin = configAdmin;
        this.runtimeProperties = runtimeProperties;
    }

    @Override
    protected Object doExecute() throws Exception {
        String name = runtimeProperties.getRuntimeIdentity();

        System.out.println("Welcome to Fabric.");
        System.out.println("");

        // are we part of fabric?
        if (fabricService != null) {
            Container container = fabricService.getCurrentContainer();
            if (container != null) {
                boolean alive = container.isAliveAndOK();
                if (!alive) {
                    System.out.println("This container \u001B" + container.getId() + "\u001B has a problem connecting to the Fabric. For more details type '\u001B[1mcontainer:info\u001B[0m'");
                } else {
                    boolean ensemble = container.isEnsembleServer();
                    if (ensemble) {
                        System.out.println("This container \u001B" + container.getId() + "\u001B is a Fabric ensemble server.");
                    } else {
                        System.out.println("This container \u001B" + container.getId() + "\u001B is joined to an existing Fabric.");
                    }
                }
            }
            System.out.println("Open a browser to " + fabricService.getWebConsoleUrl() + " to access the management console.");
            System.out.println("");
        } else {
            // no we are standalone
            System.out.println("This container " + name + " is a standalone container.");
            System.out.println("");
            System.out.println("Create a new Fabric via '\u001B[1mfabric:create\u001B[0m'");
            System.out.println("or join an existing Fabric via '\u001B[1mfabric:join [someUrls]\u001B[0m'");
            System.out.println("");
        }

        return null;
    }

}

@@ -1804,24 +1804,99 @@
 dentity();%0A%0A
+        String appName = runtimeProperties.getProperty(%22karaf.app.name%22);%0A%0A
         Syst
@@ -1922,24 +1922,27 @@
 come to 
-Fabric.%22
+%22 + appName
 );%0A     
@@ -2172,20 +2172,23 @@
 boolean 
-aliv
+ensembl
 e = cont
@@ -2199,18 +2199,22 @@
 r.is
-AliveAndOK
+EnsembleServer
 ();%0A
@@ -2237,13 +2237,15 @@
 if (
-!aliv
+ensembl
 e) %7B
@@ -2298,32 +2298,35 @@
 container %5Cu001B
+%5B1m
 %22 + container.ge
@@ -2344,200 +2344,42 @@
 001B
- has a problem connecting to the Fabric. For more details type '%5Cu001B%5B1mcontainer:info%5Cu001B%5B0m'%22);%0A                %7D else %7B%0A                    boolean ensemble = container.isEnsembleS
+%1B%5B0m is a Fabric ensemble s
 erver
-(
+.%22
 );%0A 
@@ -2397,32 +2397,17 @@
     
-    if (ensemble) %7B%0A    
+%7D else %7B%0A
     
@@ -2455,32 +2455,35 @@
 container %5Cu001B
+%5B1m
 %22 + container.ge
@@ -2501,36 +2501,44 @@
 001B
- is a Fabric ensemble server
+%1B%5B0m is joined to an existing Fabric
 .%22);
@@ -2538,32 +2538,42 @@
 ric.%22);%0A        
+        %7D%0A
             %7D el
@@ -2573,32 +2573,24 @@
    %7D else %7B%0A
-        
             
@@ -2634,37 +2634,27 @@
 r %5Cu001B
+%5B1m
 %22 + 
-container.getId()
+name
  + %22%5Cu00
@@ -2659,99 +2659,150 @@
 001B
+%5B0m
  is 
-joined to an existing Fabric.%22);%0A                    %7D%0A                %7D%0A        
+a standalone container.%22);%0A            %7D%0A            String url = fabricService.getWebConsoleUrl();%0A            if (url != null) %7B%0A
     
-%7D%0A
     
@@ -2833,103 +2833,68 @@
 ln(%22
-Open a browser to %22 + fabricService.getWebConsoleUrl() + %22 to access the management console.%22);
+Web management console available at: %22 + url);%0A            %7D
 %0A   
@@ -3026,16 +3026,25 @@
 ntainer 
+%5Cu001B%5B1m
 %22 + name
@@ -3047,16 +3047,25 @@
 name + %22
+%5Cu001B%5B0m
  is a st

Make the welcome screen adapt to if we are connected to a fabric or not.