Main.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2015 iZc
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package de.nplusc.izc.Utilities.WPCMgr;
  18. import java.io.FileNotFoundException;
  19. import java.util.HashMap;
  20. import org.apache.commons.cli.CommandLine;
  21. import org.apache.commons.cli.CommandLineParser;
  22. import org.apache.commons.cli.DefaultParser;
  23. import org.apache.commons.cli.HelpFormatter;
  24. import org.apache.commons.cli.Option;
  25. import org.apache.commons.cli.OptionBuilder;
  26. import org.apache.commons.cli.OptionGroup;
  27. import org.apache.commons.cli.Options;
  28. import org.apache.commons.cli.ParseException;
  29. import org.apache.logging.log4j.Level;
  30. import org.apache.logging.log4j.LogManager;
  31. import org.apache.logging.log4j.Logger;
  32. import org.apache.logging.log4j.core.LoggerContext;
  33. import org.apache.logging.log4j.core.config.LoggerConfig;
  34. /**
  35. *
  36. * @author LH
  37. */
  38. public class Main
  39. {
  40. /*
  41. ProgressBar progressBar = ConsoleProgressBar.on(System.out)
  42. .withFormat("[:bar] :percent% :elapsed/:total ETA: :eta")
  43. .withTotalSteps(500);
  44. */
  45. private static Logger l;
  46. public static void main(String[] args)
  47. {
  48. CommandLineParser parser = new DefaultParser();
  49. Options options = new Options();
  50. OptionGroup modes = new OptionGroup();
  51. options.addOption("v", "verbose", false, "Enables verbose logging");
  52. modes.addOption(OptionBuilder.withLongOpt("process")
  53. .withDescription("Process FOLDER")
  54. .create("p"));
  55. options.addOption(OptionBuilder.withLongOpt("folder")
  56. .withDescription("Sets the current WPCache to FOLDER")
  57. .hasArg()
  58. .withArgName("FOLDER")
  59. .create("f"));
  60. modes.addOption(OptionBuilder.withLongOpt("initDirectory")
  61. .withDescription("init selected wpcache that converts into TARGET")
  62. .hasArg()
  63. .withArgName("TARGET")
  64. .create("i"));
  65. modes.addOption(OptionBuilder.withLongOpt("addScreen")
  66. .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"+
  67. "Alternative Sybtax for screen is \"id,width,height\" to get a screen at default position of 0,0")
  68. .hasArgs(Option.UNLIMITED_VALUES)
  69. .withArgName("SCREEN")
  70. .create());
  71. options.addOptionGroup(modes);
  72. try
  73. {
  74. CommandLine cl = parser.parse(options, args);
  75. Main.setupLogging(cl.hasOption("verbose"));
  76. String folder = "XXX";
  77. if (cl.hasOption("folder"))
  78. {
  79. folder = cl.getOptionValue("folder");
  80. }
  81. else
  82. {
  83. throw new ParseException("Folder undefined");
  84. }
  85. l.trace(folder);
  86. if (cl.hasOption("initDirectory"))
  87. {
  88. String initArg = cl.getOptionValue("initDirectory");
  89. WPCUtils.initCache(folder,initArg);
  90. }
  91. else
  92. {
  93. if (cl.hasOption("addScreen"))
  94. {
  95. String[] screendefs = cl.getOptionValues("addScreen");
  96. HashMap<String,Object> cfg = WPCUtils.getConfig(folder);
  97. for (String screendef : screendefs)
  98. {
  99. String[] screen = screendef.split(",");
  100. int x=0,y=0;
  101. int w=0,h=0;
  102. try{
  103. if(screen.length==5)
  104. {
  105. x=Integer.valueOf(screen[1]);
  106. y=Integer.valueOf(screen[2]);
  107. w=Integer.valueOf(screen[3]);
  108. h=Integer.valueOf(screen[4]);
  109. }
  110. else if (screen.length==3)
  111. {
  112. w=Integer.valueOf(screen[1]);
  113. h=Integer.valueOf(screen[2]);
  114. }
  115. else
  116. {
  117. throw new ParseException("Malformed screen string: parameter count wrong @ "+screendef);
  118. }
  119. WPCUtils.addMonitor(cfg,screen[0], x, y, w, h);
  120. }
  121. catch(NumberFormatException ex)
  122. {
  123. throw new ParseException("Malformed screen string: Invalid number giveen @ "+screendef);
  124. }
  125. }
  126. WPCUtils.saveConfig(folder, cfg);
  127. }
  128. else
  129. {
  130. if(cl.hasOption("process"))
  131. {
  132. Synchronizer.main(folder);
  133. }
  134. else
  135. {
  136. throw new ParseException("No PARAMS");
  137. }
  138. }
  139. }
  140. }
  141. catch (ParseException ex)
  142. {
  143. String msg = ex.getMessage();
  144. System.err.println("Failed to parse the commandline");
  145. if(msg!=null)
  146. {
  147. System.err.println(msg);
  148. }
  149. else
  150. {
  151. System.err.println("No further details given");
  152. }
  153. HelpFormatter formatter = new HelpFormatter();
  154. formatter.printHelp("wpcmgr", options);
  155. System.exit(0);
  156. }
  157. catch (FileNotFoundException ex)
  158. {
  159. String msg = ex.getMessage();
  160. System.err.println("Failed to parse the commandline, invalid cachedir");
  161. if(msg!=null)
  162. {
  163. System.err.println(msg);
  164. }
  165. else
  166. {
  167. System.err.println("No further details given");
  168. }
  169. HelpFormatter formatter = new HelpFormatter();
  170. formatter.printHelp("wpcmgr", options);
  171. System.exit(0);
  172. }
  173. /*
  174. System.out.println(args.length);
  175. if (args.length>0)
  176. {
  177. System.out.println(args[0]);
  178. if(args[0].equals("--help")||args[0].equals("-h")||args[0].equals("/?"))
  179. {
  180. printHelp();
  181. }
  182. if(args.length>2)
  183. {
  184. System.out.println(args[1]);
  185. System.out.println(args[2]);
  186. switch (args[0])
  187. {
  188. case "--wpcprocess":
  189. Synchronizer.main(new String[]{args[1],args[2]});
  190. break;
  191. case "--initDirectory":
  192. break;
  193. default:
  194. printHelp();
  195. break;
  196. }
  197. }
  198. return;
  199. }
  200. printHelp();*/
  201. }
  202. private static void printHelp()
  203. {
  204. System.out.println("Cmdline usage of WPC-Mgr");
  205. System.out.println("wpc-mgr.jar [mode] <values>");
  206. System.out.println("Modes and its values");
  207. System.out.println("--wpcprocess : Processes the given cacheDictory");
  208. System.out.println("usage: --wpcprocess P:\\ath\\to\\CacheDir screenWidthxScreenHeight");
  209. System.out.println("--initDirectory : initializes a CacheDirectory for being used by wpcprocess");
  210. System.out.println("P:\\ath\\to\\CacheDir");
  211. System.out.println("T:\\arged\\Directory\\Of\\ProcessedFiles");
  212. System.out.println("No parameter:");
  213. System.out.println("currently help; later :starts in GUI-Mode to manage the last opened Cache");
  214. }
  215. private static void setupLogging(boolean verbose)
  216. {
  217. l=LogManager.getLogger(Main.class.getName());
  218. LoggerContext cx = (LoggerContext) LogManager.getContext(false);
  219. org.apache.logging.log4j.core.config.Configuration config = cx.getConfiguration();
  220. LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
  221. LoggerConfig externalloggerConfig = config.getLoggerConfig("External");
  222. if (verbose)
  223. {
  224. loggerConfig.setLevel(Level.TRACE);
  225. externalloggerConfig.setLevel(Level.TRACE);
  226. }
  227. else
  228. {
  229. loggerConfig.setLevel(Level.INFO);
  230. externalloggerConfig.setLevel(Level.INFO);
  231. }
  232. cx.updateLoggers();
  233. }
  234. }