// Path: src/main/java/org/graylog/collector/cli/commands/CollectorHelp.java
// public class CollectorHelp extends Help implements CollectorCommand {
//     @Override
//     public void stop() {
//         // nothing to stop
//     }
// }
// 
// Path: src/main/java/org/graylog/collector/cli/commands/Run.java
// @Command(name = "run", description = "Start the collector")
// public class Run implements CollectorCommand {
//     private static final Logger LOG = LoggerFactory.getLogger(Run.class);
// 
//     @Option(name = "-f", description = "Path to configuration file.", required = true)
//     private final File configFile = null;
// 
//     private CollectorServiceManager serviceManager;
// 
//     @Override
//     public void run() {
//         LOG.info("Starting Collector v{} (commit {})", CollectorVersion.CURRENT.version(), CollectorVersion.CURRENT.commitIdShort());
// 
//         showOsInfo();
// 
//         final Injector injector = getInjector();
//         serviceManager = injector.getInstance(CollectorServiceManager.class);
// 
//         validateConfiguration(serviceManager.getConfiguration());
// 
//         serviceManager.start();
// 
//         for (Map.Entry<Service.State, Service> entry : serviceManager.servicesByState().entries()) {
//             LOG.info("Service {}: {}", entry.getKey().toString(), entry.getValue().toString());
//         }
// 
//         serviceManager.awaitStopped();
//     }
// 
//     private void showOsInfo() {
//         try {
//             final OS os = OS.getOs();
//             LOG.info("Running on {} {} {} ({})", os.getPlatformName(), os.getName(), os.getVersion(), os.getArch());
//         } catch (Exception e) {
//             LOG.warn("Unable to get detailed platform information", e);
//         }
//     }
// 
//     @Override
//     public void stop() {
//         LOG.info("Stopping...");
//         if (serviceManager != null) {
//             serviceManager.stop();
//         }
//     }
// 
//     private Injector getInjector() {
//         Injector injector = null;
// 
//         try {
//             injector = CollectorInjector.createInjector(new ConfigurationModule(configFile),
//                     new BufferModule(),
//                     new InputsModule(),
//                     new FileModule(),
//                     new OutputsModule(),
//                     new ServicesModule(),
//                     new MetricsModule(),
//                     new MemoryReporterModule(),
//                     new ServerApiModule(),
//                     new HeartbeatModule(),
//                     new CollectorIdModule(),
//                     new CollectorHostNameModule());
//         } catch (Exception e) {
//             LOG.error("ERROR: {}", e.getMessage());
//             LOG.debug("Detailed injection creation error", e);
//             doExit();
//         }
// 
//         return injector;
//     }
// 
//     private void validateConfiguration(ConfigurationRegistry configurationRegistry) {
//         if (!configurationRegistry.isValid()) {
//             for (ConfigurationError error : configurationRegistry.getErrors()) {
//                 LOG.error("Configuration Error: {}", error.getMesssage());
//             }
// 
//             doExit();
//         }
//     }
// 
//     private void doExit() {
//         LOG.info("Exit");
//         System.exit(1);
//     }
// }


import io.airlift.airline.Cli;
import io.airlift.airline.ParseException;
import org.graylog.collector.cli.commands.CollectorCommand;
import org.graylog.collector.cli.commands.CollectorHelp;
import org.graylog.collector.cli.commands.Run;
import org.graylog.collector.cli.commands.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.graylog.collector.cli;


public class Main {
    static {
        // Hijack java.util.logging, see https://logging.apache.org/log4j/log4j-2.2/log4j-jul/index.html
        System.setProperty("java.util.logging.manager", org.apache.logging.log4j.jul.LogManager.class.getCanonicalName());
    }

    private static final Logger LOG = LoggerFactory.getLogger(Main.class);
    private static CollectorCommand command = null;

    public static void main(String[] args) {
        final Cli.CliBuilder<CollectorCommand> cliBuilder = Cli.<CollectorCommand>builder("graylog-collector")
                .withDescription("Graylog Collector")
                .withDefaultCommand(CollectorHelp.class)
                .withCommand(CollectorHelp.class)
                .withCommand(Version.class)
                .withCommand(Run.class);

        final Cli<CollectorCommand> cli = cliBuilder.build();

        try {
            command = cli.parse(args);
            configureShutdownHook(command);
            command.run();
        } catch (ParseException e) {
            LOG.error(e.getMessage());
            LOG.error("Exit");
        }
    }

    public static void stop(String[] args) {
        if (command != null) {
            command.stop();
        }
    }

// Path: src/main/java/org/graylog/collector/cli/commands/CollectorHelp.java
// public class CollectorHelp extends Help implements CollectorCommand {
//     @Override
//     public void stop() {
//         // nothing to stop
//     }
// }
// 
// Path: src/main/java/org/graylog/collector/cli/commands/Run.java
// @Command(name = "run", description = "Start the collector")
// public class Run implements CollectorCommand {
//     private static final Logger LOG = LoggerFactory.getLogger(Run.class);
// 
//     @Option(name = "-f", description = "Path to configuration file.", required = true)
//     private final File configFile = null;
// 
//     private CollectorServiceManager serviceManager;
// 
//     @Override
//     public void run() {
//         LOG.info("Starting Collector v{} (commit {})", CollectorVersion.CURRENT.version(), CollectorVersion.CURRENT.commitIdShort());
// 
//         showOsInfo();
// 
//         final Injector injector = getInjector();
//         serviceManager = injector.getInstance(CollectorServiceManager.class);
// 
//         validateConfiguration(serviceManager.getConfiguration());
// 
//         serviceManager.start();
// 
//         for (Map.Entry<Service.State, Service> entry : serviceManager.servicesByState().entries()) {
//             LOG.info("Service {}: {}", entry.getKey().toString(), entry.getValue().toString());
//         }
// 
//         serviceManager.awaitStopped();
//     }
// 
//     private void showOsInfo() {
//         try {
//             final OS os = OS.getOs();
//             LOG.info("Running on {} {} {} ({})", os.getPlatformName(), os.getName(), os.getVersion(), os.getArch());
//         } catch (Exception e) {
//             LOG.warn("Unable to get detailed platform information", e);
//         }
//     }
// 
//     @Override
//     public void stop() {
//         LOG.info("Stopping...");
//         if (serviceManager != null) {
//             serviceManager.stop();
//         }
//     }
// 
//     private Injector getInjector() {
//         Injector injector = null;
// 
//         try {
//             injector = CollectorInjector.createInjector(new ConfigurationModule(configFile),
//                     new BufferModule(),
//                     new InputsModule(),
//                     new FileModule(),
//                     new OutputsModule(),
//                     new ServicesModule(),
//                     new MetricsModule(),
//                     new MemoryReporterModule(),
//                     new ServerApiModule(),
//                     new HeartbeatModule(),
//                     new CollectorIdModule(),
//                     new CollectorHostNameModule());
//         } catch (Exception e) {
//             LOG.error("ERROR: {}", e.getMessage());
//             LOG.debug("Detailed injection creation error", e);
//             doExit();
//         }
// 
//         return injector;
//     }
// 
//     private void validateConfiguration(ConfigurationRegistry configurationRegistry) {
//         if (!configurationRegistry.isValid()) {
//             for (ConfigurationError error : configurationRegistry.getErrors()) {
//                 LOG.error("Configuration Error: {}", error.getMesssage());
//             }
// 
//             doExit();
//         }
//     }
// 
//     private void doExit() {
//         LOG.info("Exit");
//         System.exit(1);
//     }
// }


// Path: src/main/java/org/graylog/collector/cli/Main.java
import io.airlift.airline.Cli;
import io.airlift.airline.ParseException;
import org.graylog.collector.cli.commands.CollectorCommand;
import org.graylog.collector.cli.commands.CollectorHelp;
import org.graylog.collector.cli.commands.Run;
import org.graylog.collector.cli.commands.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.graylog.collector.cli;


public class Main {
    static {
        // Hijack java.util.logging, see https://logging.apache.org/log4j/log4j-2.2/log4j-jul/index.html
        System.setProperty("java.util.logging.manager", org.apache.logging.log4j.jul.LogManager.class.getCanonicalName());
    }

    private static final Logger LOG = LoggerFactory.getLogger(Main.class);
    private static CollectorCommand command = null;

    public static void main(String[] args) {
        final Cli.CliBuilder<CollectorCommand> cliBuilder = Cli.<CollectorCommand>builder("graylog-collector")
                .withDescription("Graylog Collector")
                .withDefaultCommand(CollectorHelp.class)
                .withCommand(CollectorHelp.class)
                .withCommand(Version.class)
                .withCommand(Run.class);

        final Cli<CollectorCommand> cli = cliBuilder.build();

        try {
            command = cli.parse(args);
            configureShutdownHook(command);
            command.run();
        } catch (ParseException e) {
            LOG.error(e.getMessage());
            LOG.error("Exit");
        }
    }

    public static void stop(String[] args) {
        if (command != null) {
            command.stop();
        }
    }

    private static void configureShutdownHook(final CollectorCommand command) {