/*
* Copyright (C) 2015 iZc
*
* This program 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.
*
* This program 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 this program. If not, see .
*/
package de.nplusc.izc.Utilities.WPCMgr;
import java.io.FileNotFoundException;
import java.util.HashMap;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.LoggerConfig;
/**
*
* @author LH
*/
public class Main
{
/*
ProgressBar progressBar = ConsoleProgressBar.on(System.out)
.withFormat("[:bar] :percent% :elapsed/:total ETA: :eta")
.withTotalSteps(500);
*/
private static Logger l;
public static void main(String[] args)
{
CommandLineParser parser = new DefaultParser();
Options options = new Options();
OptionGroup modes = new OptionGroup();
options.addOption("v", "verbose", false, "Enables verbose logging");
modes.addOption(OptionBuilder.withLongOpt("process")
.withDescription("Process FOLDER")
.create("p"));
options.addOption(OptionBuilder.withLongOpt("folder")
.withDescription("Sets the current WPCache to FOLDER")
.hasArg()
.withArgName("FOLDER")
.create("f"));
modes.addOption(OptionBuilder.withLongOpt("initDirectory")
.withDescription("init selected wpcache that converts into TARGET")
.hasArg()
.withArgName("TARGET")
.create("i"));
modes.addOption(OptionBuilder.withLongOpt("addScreen")
.withDescription("add SCREEN with given settings (\"id,xpos,ypos,width,height\") to the screen registry of the selected cache, allows multiple screens to be defined at once. existing Screens with the same ID get overwritten"+
"Alternative Sybtax for screen is \"id,width,height\" to get a screen at default position of 0,0")
.hasArgs(Option.UNLIMITED_VALUES)
.withArgName("SCREEN")
.create());
options.addOptionGroup(modes);
try
{
CommandLine cl = parser.parse(options, args);
Main.setupLogging(cl.hasOption("verbose"));
String folder = "XXX";
if (cl.hasOption("folder"))
{
folder = cl.getOptionValue("folder");
}
else
{
throw new ParseException("Folder undefined");
}
l.trace(folder);
if (cl.hasOption("initDirectory"))
{
String initArg = cl.getOptionValue("initDirectory");
WPCUtils.initCache(folder,initArg);
}
else
{
if (cl.hasOption("addScreen"))
{
String[] screendefs = cl.getOptionValues("addScreen");
HashMap cfg = WPCUtils.getConfig(folder);
for (String screendef : screendefs)
{
String[] screen = screendef.split(",");
int x=0,y=0;
int w=0,h=0;
try{
if(screen.length==5)
{
x=Integer.valueOf(screen[1]);
y=Integer.valueOf(screen[2]);
w=Integer.valueOf(screen[3]);
h=Integer.valueOf(screen[4]);
}
else if (screen.length==3)
{
w=Integer.valueOf(screen[1]);
h=Integer.valueOf(screen[2]);
}
else
{
throw new ParseException("Malformed screen string: parameter count wrong @ "+screendef);
}
WPCUtils.addMonitor(cfg,screen[0], x, y, w, h);
}
catch(NumberFormatException ex)
{
throw new ParseException("Malformed screen string: Invalid number giveen @ "+screendef);
}
}
WPCUtils.saveConfig(folder, cfg);
}
else
{
if(cl.hasOption("process"))
{
Synchronizer.main(folder);
}
else
{
throw new ParseException("No PARAMS");
}
}
}
}
catch (ParseException ex)
{
String msg = ex.getMessage();
System.err.println("Failed to parse the commandline");
if(msg!=null)
{
System.err.println(msg);
}
else
{
System.err.println("No further details given");
}
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("wpcmgr", options);
System.exit(0);
}
catch (FileNotFoundException ex)
{
String msg = ex.getMessage();
System.err.println("Failed to parse the commandline, invalid cachedir");
if(msg!=null)
{
System.err.println(msg);
}
else
{
System.err.println("No further details given");
}
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("wpcmgr", options);
System.exit(0);
}
/*
System.out.println(args.length);
if (args.length>0)
{
System.out.println(args[0]);
if(args[0].equals("--help")||args[0].equals("-h")||args[0].equals("/?"))
{
printHelp();
}
if(args.length>2)
{
System.out.println(args[1]);
System.out.println(args[2]);
switch (args[0])
{
case "--wpcprocess":
Synchronizer.main(new String[]{args[1],args[2]});
break;
case "--initDirectory":
break;
default:
printHelp();
break;
}
}
return;
}
printHelp();*/
}
private static void printHelp()
{
System.out.println("Cmdline usage of WPC-Mgr");
System.out.println("wpc-mgr.jar [mode] ");
System.out.println("Modes and its values");
System.out.println("--wpcprocess : Processes the given cacheDictory");
System.out.println("usage: --wpcprocess P:\\ath\\to\\CacheDir screenWidthxScreenHeight");
System.out.println("--initDirectory : initializes a CacheDirectory for being used by wpcprocess");
System.out.println("P:\\ath\\to\\CacheDir");
System.out.println("T:\\arged\\Directory\\Of\\ProcessedFiles");
System.out.println("No parameter:");
System.out.println("currently help; later :starts in GUI-Mode to manage the last opened Cache");
}
private static void setupLogging(boolean verbose)
{
l=LogManager.getLogger(Main.class.getName());
LoggerContext cx = (LoggerContext) LogManager.getContext(false);
org.apache.logging.log4j.core.config.Configuration config = cx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
LoggerConfig externalloggerConfig = config.getLoggerConfig("External");
if (verbose)
{
loggerConfig.setLevel(Level.TRACE);
externalloggerConfig.setLevel(Level.TRACE);
}
else
{
loggerConfig.setLevel(Level.INFO);
externalloggerConfig.setLevel(Level.INFO);
}
cx.updateLoggers();
}
}