initial commit
[meviz.git] / src / main / java / eu / svjatoslav / meviz / Main.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz;
11
12 import java.util.ArrayList;
13
14 import eu.svjatoslav.meviz.encoder.Encoder;
15
16 public class Main {
17
18         public static ArrayList<Module> modules;
19
20         public static String[] getRemainingOptions(final String[] options) {
21                 final String[] remainingOptions = new String[options.length - 1];
22
23                 for (int i = 1; i < options.length; i++)
24                         remainingOptions[i - 1] = options[i];
25
26                 return remainingOptions;
27         };
28
29         public static void initModules() {
30                 modules = new ArrayList<Module>();
31
32                 modules.add(new Encoder());
33                 modules.add(new eu.svjatoslav.meviz.htmlindexer.Main());
34                 modules.add(new eu.svjatoslav.meviz.renamer.Main());
35                 modules.add(new eu.svjatoslav.meviz.grabmemcard.Main());
36                 modules.add(new eu.svjatoslav.meviz.textsplitter.Main());
37
38         }
39
40         public static void main(final String[] args) throws Exception {
41
42                 initModules();
43
44                 if (args.length < 1)
45                         showHelp();
46                 else if (args[0].equals("help")) {
47                         if (args.length != 2)
48                                 showHelp();
49                         else {
50
51                                 for (final Module module : modules)
52                                         if (args[1].equals(module.getModuleCommand())) {
53                                                 module.showCommandlineHelp();
54                                                 return;
55                                         }
56
57                                 System.out.println("Error: unrecognized module by name:"
58                                                 + args[1] + "\n");
59                                 showHelp();
60
61                         }
62                 } else {
63
64                         for (final Module module : modules)
65                                 if (args[0].equals(module.getModuleCommand())) {
66                                         module.run(getRemainingOptions(args));
67                                         return;
68                                 }
69
70                         System.out.println("Error: unrecognized commandline option:"
71                                         + args[0] + "\n");
72                         showHelp();
73                 }
74
75         }
76
77         public static void showHelp() {
78                 final StringBuffer buffer = new StringBuffer();
79
80                 buffer.append("Commandline options: \n"
81                                 + "help\n    show this help screen \n\n");
82
83                 buffer.append("help <module>\n    show module specific help screen\n\n");
84
85                 buffer.append("Available modules:\n\n");
86
87                 for (final Module module : modules) {
88                         buffer.append(module.getModuleCommand() + "\n");
89                         buffer.append("    " + module.getDescription() + "\n\n");
90                 }
91
92                 System.out.println(buffer.toString());
93         }
94 }