First version (slashdot supported)
[gofetch.git] / src / be / nikiroo / gofetch / 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.BasicSupport.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 (empty is ok, DO NOT end with /)
56 String preselector = "";
57 if (preselectorStr != null && !preselectorStr.startsWith("/")) {
58 preselector = "/" + preselectorStr;
59 }
60 while (preselector.endsWith("/")) {
61 preselector = preselector.substring(0, preselector.length() - 1);
62 }
63
64 // Type to download
65 Type type = null;
66 if (!"ALL".equals(typeStr)) {
67 try {
68 Type.valueOf(typeStr.toUpperCase());
69 } catch (IllegalArgumentException e) {
70 System.err.println("Invalid type: " + typeStr);
71 System.exit(1);
72 }
73 }
74
75 // Max number of stories to display in the cache
76 int maxStories = 0;
77 try {
78 maxStories = Integer.parseInt(maxStoriesStr);
79 } catch (NumberFormatException e) {
80 System.err
81 .println("The maximum number of stories cannot be parsed: "
82 + maxStoriesStr);
83 System.exit(1);
84 }
85
86 //
87 String hostname = hostnameStr;
88
89 //
90 int port = 0;
91 try {
92 port = Integer.parseInt(portStr);
93 } catch (NumberFormatException e) {
94 System.err.println("The port cannot be parsed: " + portStr);
95 System.exit(1);
96 }
97
98 if (port < 0 || port > 65535) {
99 System.err.println("Invalid port number: " + portStr);
100 System.exit(1);
101 }
102
103 new Fetcher(dir, preselector, type, maxStories, hostname, port).start();
104 }
105 }