package io.crate.frameworks.mesos;

import com.beust.jcommander.JCommander;
import com.google.common.base.Optional;
import com.google.protobuf.ByteString;
import io.crate.frameworks.mesos.api.CrateHttpService;
import io.crate.frameworks.mesos.config.Configuration;
import org.apache.log4j.BasicConfigurator;
import org.apache.mesos.MesosSchedulerDriver;
import org.apache.mesos.Protos;
import org.apache.mesos.Scheduler;
import org.apache.mesos.state.ZooKeeperState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;


public class Main {

    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    private static final String ZK_URL = "localhost:2181";

    public static void main(String[] args) throws Exception {
        BasicConfigurator.configure();

        Configuration configuration = new Configuration();
        JCommander jCommander = new JCommander(configuration, args);

        LOGGER.debug("args: {}", configuration);

        final int frameworkFailoverTimeout = 60 * 60;

        Protos.FrameworkInfo.Builder frameworkBuilder = Protos.FrameworkInfo.newBuilder()
                .setName("CrateFramework")
                .setUser("root")
                .setFailoverTimeout(frameworkFailoverTimeout); // timeout in seconds

        PersistentStateStore stateStore = new PersistentStateStore(
                new ZooKeeperState(ZK_URL, 20_000, TimeUnit.MILLISECONDS,
                        String.format("/crate-mesos/%s", configuration.clusterName())),
                configuration.nodeCount());

        Optional<String> frameworkId = stateStore.state().frameworkId();
        if (frameworkId.isPresent()) {
            frameworkBuilder.setId(Protos.FrameworkID.newBuilder().setValue(frameworkId.get()).build());
        }

        if (System.getenv("MESOS_CHECKPOINT") != null) {
            System.out.println("Enabling checkpoint for the framework");
            frameworkBuilder.setCheckpoint(true);
        }

        final Scheduler scheduler = new CrateScheduler(stateStore, configuration);

        // create the driver
        MesosSchedulerDriver driver;

        String mesosMaster = configuration.mesosMaster();

        if (System.getenv("MESOS_AUTHENTICATE") != null) {
            System.out.println("Enabling authentication for the framework");

            if (System.getenv("DEFAULT_PRINCIPAL") == null) {
                System.err.println("Expecting authentication principal in the environment");
                System.exit(1);
            }

            if (System.getenv("DEFAULT_SECRET") == null) {
                System.err.println("Expecting authentication secret in the environment");
                System.exit(1);
            }

            Protos.Credential credential = Protos.Credential.newBuilder()
                    .setPrincipal(System.getenv("DEFAULT_PRINCIPAL"))
                    .setSecret(ByteString.copyFrom(System.getenv("DEFAULT_SECRET").getBytes()))
                    .build();

            frameworkBuilder.setPrincipal(System.getenv("DEFAULT_PRINCIPAL"));
            driver = new MesosSchedulerDriver(scheduler, frameworkBuilder.build(), mesosMaster, credential);
        } else {
            frameworkBuilder.setPrincipal("crate-framework");
            driver = new MesosSchedulerDriver(scheduler, frameworkBuilder.build(), mesosMaster);
        }

        CrateHttpService api = new CrateHttpService(stateStore, configuration.apiPort());
        api.start();
        int status = driver.run() == Protos.Status.DRIVER_STOPPED ? 0 : 1;

        // Ensure that the driver process terminates.
        api.stop();
        driver.stop();
        System.exit(status);
    }

}

@@ -109,16 +109,55 @@
 tional;%0A
+import com.google.common.collect.Sets;%0A
 import c
@@ -187,16 +187,16 @@
 String;%0A
-
 import i
@@ -557,16 +557,38 @@
 ctory;%0A%0A
+import java.util.Set;%0A
 import j
@@ -779,16 +779,110 @@
 2181%22;%0A%0A
+    private static final Set%3CString%3E HELP_OPTIONS = Sets.newHashSet(%22-h%22, %22--help%22, %22help%22);%0A%0A
     publ
@@ -1063,16 +1063,269 @@
 ommander
+;%0A%0A        for (String arg : args) %7B%0A            if (HELP_OPTIONS.contains(arg)) %7B%0A                jCommander = new JCommander(configuration);%0A                jCommander.usage();%0A                System.exit(1);%0A            %7D%0A        %7D%0A        jCommander
  = new J
@@ -1355,17 +1355,16 @@
 , args);
-%0A
 %0A       

add help options to cli