package org.lodder.subtools.subsort;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;

public class App {

  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
  
  public static void main(String[] args) {
    setLoggingLevel(Level.INFO);

    CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();

    CommandLine line = null;

    try {
      line = parser.parse(getCLIOptions(), args);
    } catch (ParseException e) {
      LOGGER.error("Unable to parse cli options", e);
    }

    if (line != null) {
      if (line.hasOption("help")) {
        formatter.printHelp("SubSort", getCLIOptions());
      } else {
        SortSubtitle sortSubtitle = new SortSubtitle();
        if (line.hasOption("rebuildindex")) {
          sortSubtitle.reBuildIndex(new File(line.getOptionValue("outputfolder")));
        } else if (line.hasOption("removefromarchive")) {
          sortSubtitle.removeFromRepo(new File(line.getOptionValue("removefromarchive")), new File(
              line.getOptionValue("outputfolder")));
        } else {
          sortSubtitle.run(line.hasOption("remove"), new File(line.getOptionValue("inputfolder")),
              new File(line.getOptionValue("outputfolder")), line.hasOption("cleanup"));
        }
      }
    }
  }
  
  public static void setLoggingLevel(Level level) {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
    root.setLevel(level);
}

  private static Options getCLIOptions() {
    /**
     * CLI Options
     */
    Options options = new Options();
    options.addOption("help", false, "print this message");
    options.addOption("removefromarchive", true, "remove subtitle from archive");
    options.addOption("remove", false, "remove subtitles when copy to the index folder");
    options.addOption("rebuildindex", false, "rebuild index, ignoring the current index");
    options.addOption("inputfolder", true, "the folder to be sorted");
    options.addOption("outputfolder", true, "the folder containing the index");
    options.addOption("cleanup", false, "clean up none exising subtitles in index");

    return options;
  }
}

@@ -429,170 +429,1040 @@
 l;%0D%0A
-%0D%0Apublic class App %7B%0D%0A%0D%0A  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);%0D%0A  %0D%0A  public static void main(String%5B%5D args) %7B%0D%0A    setLogging
+import ch.qos.logback.classic.LoggerContext;%0D%0Aimport ch.qos.logback.classic.encoder.PatternLayoutEncoder;%0D%0Aimport ch.qos.logback.classic.spi.ILoggingEvent;%0D%0Aimport ch.qos.logback.core.ConsoleAppender;%0D%0A%0D%0Apublic class App %7B%0D%0A%0D%0A  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);%0D%0A  %0D%0A  public static void main(String%5B%5D args) %7B%0D%0A    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();%0D%0A    PatternLayoutEncoder ple = new PatternLayoutEncoder();%0D%0A    %0D%0A    ple.setPattern(%22%25date %25level %5B%25thread%5D %25logger%7B10%7D %5B%25file:%25line%5D %25msg%25n%22);%0D%0A    ple.setContext(lc);%0D%0A    ple.start();%0D%0A    %0D%0A    ConsoleAppender%3CILoggingEvent%3E consoleAppender = new ConsoleAppender%3CILoggingEvent%3E();%0D%0A    consoleAppender.setEncoder(ple);%0D%0A    consoleAppender.setContext(lc);%0D%0A    consoleAppender.start();%0D%0A    %0D%0A    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);%0D%0A    root.addAppender(consoleAppender);%0D%0A    root.set
 Leve
@@ -2556,252 +2556,8 @@
   %7D%0D
-%0A  %0D%0A  public static void setLoggingLevel(Level level) %7B%0D%0A    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);%0D%0A    root.setLevel(level);%0D%0A%7D%0D
 %0A%0D%0A 

Configure logging to console