// Path: GerritDownloader/src/main/java/com/holmsted/gerrit/downloaders/Downloader.java
// public class Downloader {
// 
//     private static final int FILE_FORMAT_VERSION = 1;
// 
//     @Nonnull
//     private final CommandLineParser commandLine;
//     @Nonnull
//     private final GerritServer gerritServer;
// 
//     private GerritVersion gerritVersion;
// 
//     public Downloader(@Nonnull CommandLineParser commandLine) {
//         this.commandLine = commandLine;
//         gerritServer = new GerritServer(
//                 commandLine.getServerName(),
//                 commandLine.getServerPort(),
//                 commandLine.getPrivateKey());
//     }
// 
//     public void download() {
//         List<String> projectNames = commandLine.getProjectNames();
//         if (projectNames == null || projectNames.isEmpty()) {
//             projectNames = createProjectLister().getProjectListing();
//         }
// 
//         gerritVersion = GerritSsh.version(gerritServer);
//         if (gerritVersion == null) {
//             System.out.println("Could not query for Gerrit version, aborting.");
//             System.out.println("Are you sure the server name is correct, and that you are connected to it?");
//             return;
//         }
// 
//         for (String projectName : projectNames) {
//             AbstractGerritStatsDownloader downloader = createDownloader();
//             downloader.setOverallCommitLimit(commandLine.getCommitLimit());
//             downloader.setAfterDate(commandLine.getAfterDate());
//             downloader.setBeforeDate(commandLine.getBeforeDate());
//             downloader.setProjectName(projectName);
//             List<JSONObject> data = downloader.readData();
//             if (data.isEmpty()) {
//                 System.out.println(String.format("No output was generated for project '%s'", projectName));
//             } else {
//                 String outputDir = checkNotNull(commandLine.getOutputDir());
//                 String outputFilename = outputDir + File.separator + projectNameToFilename(projectName);
//                 writeJsonFile(outputFilename, data);
//                 System.out.println("Wrote output to " + outputFilename);
//             }
//         }
//     }
// 
//     private void writeJsonFile(@Nonnull String outputFilename, @Nonnull List<JSONObject> data) {
//         JSONObject root = new JSONObject();
//         root.put("gerritStatsVersion", FILE_FORMAT_VERSION);
//         root.put("gerritVersion", gerritVersion.toString());
//         root.put("commits", data);
// 
//         FileWriter.writeFile(outputFilename, root.toString());
//     }
// 
//     private ProjectLister createProjectLister() {
//         return new SshProjectLister(gerritServer);
//     }
// 
//     private AbstractGerritStatsDownloader createDownloader() {
//         return new SshDownloader(gerritServer, checkNotNull(gerritVersion));
//     }
// 
//     @Nonnull
//     private static String projectNameToFilename(@Nonnull String projectName) {
//         return sanitizeFilename(projectName) + ".json";
//     }
// 
//     @Nonnull
//     private static String sanitizeFilename(@Nonnull String filename) {
//         return filename.replaceAll("[^a-zA-Z0-9.-]", "_");
//     }
// }


import com.holmsted.gerrit.downloaders.Downloader;
package com.holmsted.gerrit;


public final class GerritStatsDownloaderMain {

    public static void main(String[] args) {
        CommandLineParser commandLine = new CommandLineParser();
        if (!commandLine.parse(args)) {
            System.out.println("Reads Gerrit statistics from a server and writes them to a file.");
            commandLine.printUsage();
            System.exit(1);
            return;
        }

// Path: GerritDownloader/src/main/java/com/holmsted/gerrit/downloaders/Downloader.java
// public class Downloader {
// 
//     private static final int FILE_FORMAT_VERSION = 1;
// 
//     @Nonnull
//     private final CommandLineParser commandLine;
//     @Nonnull
//     private final GerritServer gerritServer;
// 
//     private GerritVersion gerritVersion;
// 
//     public Downloader(@Nonnull CommandLineParser commandLine) {
//         this.commandLine = commandLine;
//         gerritServer = new GerritServer(
//                 commandLine.getServerName(),
//                 commandLine.getServerPort(),
//                 commandLine.getPrivateKey());
//     }
// 
//     public void download() {
//         List<String> projectNames = commandLine.getProjectNames();
//         if (projectNames == null || projectNames.isEmpty()) {
//             projectNames = createProjectLister().getProjectListing();
//         }
// 
//         gerritVersion = GerritSsh.version(gerritServer);
//         if (gerritVersion == null) {
//             System.out.println("Could not query for Gerrit version, aborting.");
//             System.out.println("Are you sure the server name is correct, and that you are connected to it?");
//             return;
//         }
// 
//         for (String projectName : projectNames) {
//             AbstractGerritStatsDownloader downloader = createDownloader();
//             downloader.setOverallCommitLimit(commandLine.getCommitLimit());
//             downloader.setAfterDate(commandLine.getAfterDate());
//             downloader.setBeforeDate(commandLine.getBeforeDate());
//             downloader.setProjectName(projectName);
//             List<JSONObject> data = downloader.readData();
//             if (data.isEmpty()) {
//                 System.out.println(String.format("No output was generated for project '%s'", projectName));
//             } else {
//                 String outputDir = checkNotNull(commandLine.getOutputDir());
//                 String outputFilename = outputDir + File.separator + projectNameToFilename(projectName);
//                 writeJsonFile(outputFilename, data);
//                 System.out.println("Wrote output to " + outputFilename);
//             }
//         }
//     }
// 
//     private void writeJsonFile(@Nonnull String outputFilename, @Nonnull List<JSONObject> data) {
//         JSONObject root = new JSONObject();
//         root.put("gerritStatsVersion", FILE_FORMAT_VERSION);
//         root.put("gerritVersion", gerritVersion.toString());
//         root.put("commits", data);
// 
//         FileWriter.writeFile(outputFilename, root.toString());
//     }
// 
//     private ProjectLister createProjectLister() {
//         return new SshProjectLister(gerritServer);
//     }
// 
//     private AbstractGerritStatsDownloader createDownloader() {
//         return new SshDownloader(gerritServer, checkNotNull(gerritVersion));
//     }
// 
//     @Nonnull
//     private static String projectNameToFilename(@Nonnull String projectName) {
//         return sanitizeFilename(projectName) + ".json";
//     }
// 
//     @Nonnull
//     private static String sanitizeFilename(@Nonnull String filename) {
//         return filename.replaceAll("[^a-zA-Z0-9.-]", "_");
//     }
// }


// Path: GerritDownloader/src/main/java/com/holmsted/gerrit/GerritStatsDownloaderMain.java
import com.holmsted.gerrit.downloaders.Downloader;

package com.holmsted.gerrit;


public final class GerritStatsDownloaderMain {

    public static void main(String[] args) {
        CommandLineParser commandLine = new CommandLineParser();
        if (!commandLine.parse(args)) {
            System.out.println("Reads Gerrit statistics from a server and writes them to a file.");
            commandLine.printUsage();
            System.exit(1);
            return;
        }

        Downloader downloader = new Downloader(commandLine);