/*
 * This file is part of Bitsquare.
 *
 * Bitsquare is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bitsquare is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare.seednode;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.bitsquare.app.BitsquareEnvironment;
import io.bitsquare.app.BitsquareExecutable;
import io.bitsquare.common.UserThread;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import static io.bitsquare.app.BitsquareEnvironment.*;

public class SeedNodeMain extends BitsquareExecutable {
    private static final Logger log = LoggerFactory.getLogger(SeedNodeMain.class);
    private SeedNode seedNode;
    private boolean isStopped;

    public static void main(String[] args) throws Exception {
        final ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("SeedNodeMain")
                .setDaemon(true)
                .build();
        UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));

        // We don't want to do the full argument parsing here as that might easily change in update versions
        // So we only handle the absolute minimum which is APP_NAME, APP_DATA_DIR_KEY and USER_DATA_DIR
        BitsquareEnvironment.setDefaultAppName("Bitsquare_seednode");
        OptionParser parser = new OptionParser();
        parser.allowsUnrecognizedOptions();
        parser.accepts(USER_DATA_DIR_KEY, description("User data directory", DEFAULT_USER_DATA_DIR))
                .withRequiredArg();
        parser.accepts(APP_NAME_KEY, description("Application name", DEFAULT_APP_NAME))
                .withRequiredArg();

        OptionSet options;
        try {
            options = parser.parse(args);
        } catch (OptionException ex) {
            System.out.println("error: " + ex.getMessage());
            System.out.println();
            parser.printHelpOn(System.out);
            System.exit(EXIT_FAILURE);
            return;
        }
        BitsquareEnvironment bitsquareEnvironment = new BitsquareEnvironment(options);

        // need to call that before BitsquareAppMain().execute(args)
        BitsquareExecutable.initAppDir(bitsquareEnvironment.getProperty(BitsquareEnvironment.APP_DATA_DIR_KEY));

        // For some reason the JavaFX launch process results in us losing the thread context class loader: reset it.
        // In order to work around a bug in JavaFX 8u25 and below, you must include the following code as the first line of your realMain method:
        Thread.currentThread().setContextClassLoader(SeedNodeMain.class.getClassLoader());

        new SeedNodeMain().execute(args);
    }

    @Override
    protected void doExecute(OptionSet options) {
        SeedNode.setEnvironment(new BitsquareEnvironment(options));
        UserThread.execute(() -> seedNode = new SeedNode());

        while (!isStopped) {
            try {
                Scanner scanner = new Scanner(System.in);
                while (scanner.hasNextLine()) {
                    String inputString = scanner.nextLine();
                    if (inputString.equals("q")) {
                        UserThread.execute(seedNode::shutDown);
                        isStopped = true;
                    }
                }
            } catch (Throwable ignore) {
            }
        }
    }
}

@@ -1078,34 +1078,8 @@
 y;%0A%0A
-import java.util.Scanner;%0A
 impo
@@ -1386,39 +1386,8 @@
 ode;
-%0A    private boolean isStopped;
 %0A%0A  
@@ -3563,18 +3563,12 @@
 le (
-!isStopped
+true
 ) %7B%0A
@@ -3605,396 +3605,164 @@
     
-Scanner scanner = new Scanner(System.in);%0A                while (scanner.hasNextLine()) %7B%0A                    String inputString = scanner.nextLine();%0A                    if (inputString.equals(%22q%22)) %7B%0A                        UserThread.execute(seedNode::shutDown);%0A                        isStopped = true;%0A                    %7D%0A                %7D%0A            %7D catch (Throwable ignore) %7B
+Thread.sleep(Long.MAX_VALUE);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A                log.error(e.getMessage());
 %0A   

Remove scanner as it conflicts when running seednode with nohup