/*
 * (C) Copyright 2017 Boni Garcia (http://bonigarcia.github.io/)
 *
 * Licensed 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.github.bonigarcia;

import static java.lang.Runtime.getRuntime;
import static java.lang.invoke.MethodHandles.lookup;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Scanner;

import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;

import io.github.bonigarcia.config.Config;
import io.github.bonigarcia.handler.DockerDriverHandler;

/**
 * Main class for interactive mode.
 *
 * @author Boni Garcia (boni.gg@gmail.com)
 * @since 1.0.0
 */
public class SeleniumJupiter {

    static final Logger log = getLogger(lookup().lookupClass());

    protected static Config config;

    public static synchronized Config config() {
        if (config == null) {
            config = new Config();
        }
        return config;
    }

    public static void main(String[] args) {
        String validBrowsers = "chrome|firefox|opera|android";
        if (args.length <= 0) {
            log.error(
                    "Usage: SeleniumJupiter browserName <version> <deviceName> <browserNameInAdroid>");
            log.error("\t...where:");
            log.error("\tbrowserName = {}", validBrowsers);
            log.error("\tversion = optional version (latest if empty)");
            log.error("\tdeviceName = Device name (only for Android)");
            log.error(
                    "\browserNameInAdroid = Browser name in Android (only for Android)");

        } else {
            String browser = args[0];
            String version = "";
            String deviceName = "";
            String browserNameInAdroid = "";
            String versionMessage = "(latest)";
            if (args.length > 1) {
                version = args[1];
                deviceName = args[2];
                browserNameInAdroid = args[3];
                versionMessage = version;
            }
            log.info("Using SeleniumJupiter to execute {} {} in Docker",
                    browser, versionMessage);

            try {
                config().setVnc(true);
                config().setBrowserSessionTimeoutDuration("99h0m0s");

                DockerDriverHandler dockerDriverHandler = new DockerDriverHandler();

                BrowserType browserType = BrowserType
                        .valueOf(browser.toUpperCase());
                browserType.init();

                WebDriver webdriver = dockerDriverHandler.resolve(browserType,
                        version, browserNameInAdroid, deviceName);

                getRuntime().addShutdownHook(new Thread() {
                    @Override
                    public void run() {
                        cleanContainers(dockerDriverHandler, webdriver);
                    }
                });

                log.info("Press ENTER to exit");
                Scanner scanner = new Scanner(System.in);
                scanner.nextLine();
                scanner.close();

                cleanContainers(dockerDriverHandler, webdriver);

            } catch (Exception e) {
                log.error("Exception trying to execute {} {} in Docker",
                        browser, versionMessage, e);
            }
        }
    }

    private static void cleanContainers(DockerDriverHandler dockerDriverHandler,
            WebDriver webdriver) {
        if (webdriver != null) {
            webdriver.quit();
        }
        if (dockerDriverHandler != null) {
            dockerDriverHandler.cleanup();
            dockerDriverHandler.close();
        }
    }

}

@@ -727,35 +727,116 @@
 ang.
-invoke.MethodHandles.lookup
+String.join;%0Aimport static java.lang.invoke.MethodHandles.lookup;%0Aimport static java.util.Arrays.copyOfRange
 ;%0Aim
@@ -1702,21 +1702,8 @@
 ion%3E
- %3CdeviceName%3E
  %3Cbr
@@ -1720,16 +1720,29 @@
 nAdroid%3E
+ %3CdeviceName%3E
 %22);%0A    
@@ -1934,148 +1934,149 @@
 ror(
-%22%5CtdeviceName = Device name (only for Android)%22);%0A            log.error(%0A                    %22%5CbrowserNameInAdroid = Browser name in Android
+%0A                    %22%5CtbrowserNameInAdroid = Browser name in Android (only for Android)%22);%0A            log.error(%22%5CtdeviceName = Device name
  (on
@@ -2402,29 +2402,82 @@
     
-deviceName = args%5B2%5D;
+versionMessage = version;%0A            %7D%0A            if (args.length %3E 2) %7B
 %0A   
@@ -2520,19 +2520,27 @@
 rgs%5B
-3
+2
 %5D;%0A
+%0A  
        
+   %7D%0A
     
@@ -2548,32 +2548,107 @@
     
-versionMessage = version
+   if (args.length %3E 3) %7B%0A                deviceName = join(%22 %22, copyOfRange(args, 3, args.length))
 ;%0A  
@@ -2651,32 +2651,33 @@
 ;%0A            %7D%0A
+%0A
             log.

Fix interactive usage for Android devices