e4078d8d1009e454dfcea46ea3907ce45212a7ca
[gofetch.git] / Main.java
1 package be.nikiroo.gofetch;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import be.nikiroo.gofetch.support.Type;
7
8 /**
9 * This class is tha main entry point of the program. It will parse the
10 * arguments, checks them (and warn-and-exit if they are invalid) then call
11 * {@link Fetcher#start()}.
12 *
13 * @author niki
14 */
15 public class Main {
16 /**
17 * Main entry point.
18 *
19 * @param args
20 * save-to-dir selector-subdir type max hostname port
21 *
22 * @throws IOException
23 * in case of I/O error
24 */
25 public static void main(String[] args) throws IOException {
26 if (args.length < 6) {
27 System.err
28 .println("Syntax error: gofecth [target dir] [selector] [type or 'ALL'] [max stories] [hostname] [port]");
29 System.exit(1);
30 }
31
32 String dirStr = args[0];
33 String preselectorStr = args[1];
34 String typeStr = args[2];
35 String maxStoriesStr = args[3];
36 String hostnameStr = args[4];
37 String portStr = args[5];
38
39 // Dir
40 File dir = new File(dirStr);
41 dir.mkdirs();
42
43 if (!dir.exists()) {
44 System.err.println("Cannot open/create the root directory: "
45 + dirStr);
46 System.exit(1);
47 }
48
49 if (dir.isFile()) {
50 System.err
51 .println("Root directory exists and is a file: " + dirStr);
52 System.exit(1);
53 }
54
55 // Selector base :
56 // - empty is ok
57 // - DO NOT end with /
58 // - always starts with / if not empty
59 String preselector = "";
60 if (preselectorStr != null && !preselectorStr.startsWith("/")) {
61 preselector = "/" + preselectorStr;
62 }
63 while (preselector.endsWith("/")) {
64 preselector = preselector.substring(0, preselector.length() - 1);
65 }
66
67 // Type to download
68 Type type = null;
69 if (!"ALL".equals(typeStr)) {
70 try {
71 type = Type.valueOf(typeStr.toUpperCase());
72 } catch (IllegalArgumentException e) {
73 System.err.println("Invalid type: " + typeStr);
74 System.exit(1);
75 }
76 }
77
78 // Max number of stories to display in the cache
79 int maxStories = 0;
80 try {
81 maxStories = Integer.parseInt(maxStoriesStr);
82 } catch (NumberFormatException e) {
83 System.err
84 .println("The maximum number of stories cannot be parsed: "
85 + maxStoriesStr);
86 System.exit(1);
87 }
88
89 //
90 String hostname = hostnameStr;
91
92 //
93 int port = 0;
94 try {
95 port = Integer.parseInt(portStr);
96 } catch (NumberFormatException e) {
97 System.err.println("The port cannot be parsed: " + portStr);
98 System.exit(1);
99 }
100
101 if (port < 0 || port > 65535) {
102 System.err.println("Invalid port number: " + portStr);
103 System.exit(1);
104 }
105
106 new Fetcher(dir, preselector, type, maxStories, hostname, port).start();
107 }
108 }