shell:source mvn:org.fusesource.fabric/fuse-fabric/1.1/karaf/wait-for-service
shell:source mvn:org.fusesource.fabric/fuse-fabric/1.1/karaf/add-scala-tools-repo

echo Add fabric features
features:addurl mvn:org.fusesource.fabric/fuse-fabric/1.1/xml/features

echo Install fabric commands

# -----------------------------------------------
# TODO - Remove this work-around that allows installation into FUSE ESB 4.4.0-fuse-00-43
features:install fabric-zookeeper
wait-for-service "(objectClass=org.linkedin.zookeeper.client.IZKClient)"
features:install fabric-maven-proxy
osgi:install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.jsch/0.1.44_2
osgi:install -s mvn:org.codehaus.jackson/jackson-core-asl/1.9.2
osgi:install -s mvn:org.codehaus.jackson/jackson-mapper-asl/1.9.2
osgi:install -s mvn:com.weiglewilczek.scala-lang-osgi/scala-library/2.9.1
osgi:install -s mvn:org.fusesource.fabric/fabric-core/1.1
# -----------------------------------------------

# Keep this guy from logging an exception stack trace every second
config:edit org.ops4j.pax.logging
config:propset log4j.logger.org.apache.zookeeper.ClientCnxn$SendThread ERROR
config:update
osgi:refresh --force "OPS4J Pax Logging - Service"

features:install fabric-core

features:install fabric-commands

wait-for-command fabric zk-cluster

features:install fabric-jaas
wait-for-service  "(&(objectClass=org.apache.karaf.jaas.config.JaasRealm)(org.apache.karaf.jaas.module=zookeeper))"

# lets not use elastic search just yet
#
# features:install insight-elasticsearch
# features:install insight-log-elasticsearch

# join existing cluster as root node (if first param is set - host:port)
# or create a new one
 if {$1} {
    echo "Joining cluster " $1
    shell:sleep 2000
    fabric:join $1
    #config:edit org.fusesource.fabric.zookeeper
    #config:propset zookeeper.url $1
    #config:update
    #shell:sleep 2000
    #zk:create /fabric/configs/agents/$APPLICATION base
    #zk:create /fabric/configs/versions/base/agents/$APPLICATION default
} {
    echo Creating initial ZooKeeper cluster
    fabric:ensemble-create --clean $APPLICATION

    echo Wait a few seconds for the cluster to initialize
    shell:sleep 5000

    # Lets define a bunch of default profiles
    echo Creating some default profiles
    shell:source mvn:org.fusesource.fabric/fuse-fabric/1.1/karaf/profiles
}



