Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.net.MalformedURLException; | |
6 | import java.net.URL; | |
91b82a5c | 7 | import java.util.ArrayList; |
f569d249 | 8 | import java.util.List; |
08fe2e33 NR |
9 | |
10 | import be.nikiroo.fanfix.bundles.StringId; | |
11 | import be.nikiroo.fanfix.data.Chapter; | |
f569d249 | 12 | import be.nikiroo.fanfix.data.MetaData; |
08fe2e33 | 13 | import be.nikiroo.fanfix.data.Story; |
ff05b828 NR |
14 | import be.nikiroo.fanfix.library.BasicLibrary; |
15 | import be.nikiroo.fanfix.library.CacheLibrary; | |
e42573a0 NR |
16 | import be.nikiroo.fanfix.library.LocalLibrary; |
17 | import be.nikiroo.fanfix.library.RemoteLibrary; | |
18 | import be.nikiroo.fanfix.library.RemoteLibraryServer; | |
08fe2e33 NR |
19 | import be.nikiroo.fanfix.output.BasicOutput; |
20 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
3727aae2 | 21 | import be.nikiroo.fanfix.reader.BasicReader; |
e42573a0 NR |
22 | import be.nikiroo.fanfix.reader.Reader; |
23 | import be.nikiroo.fanfix.reader.Reader.ReaderType; | |
91b82a5c | 24 | import be.nikiroo.fanfix.searchable.BasicSearchable; |
08fe2e33 | 25 | import be.nikiroo.fanfix.supported.BasicSupport; |
0ffa4754 | 26 | import be.nikiroo.fanfix.supported.SupportType; |
3b2b638f | 27 | import be.nikiroo.utils.Progress; |
39c3c689 | 28 | import be.nikiroo.utils.Version; |
62c63b07 | 29 | import be.nikiroo.utils.serial.server.ServerObject; |
08fe2e33 NR |
30 | |
31 | /** | |
32 | * Main program entry point. | |
33 | * | |
34 | * @author niki | |
35 | */ | |
36 | public class Main { | |
d0114000 | 37 | private enum MainAction { |
8b153400 | 38 | IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, SET_READER, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, TAG |
d0114000 NR |
39 | } |
40 | ||
08fe2e33 NR |
41 | /** |
42 | * Main program entry point. | |
43 | * <p> | |
44 | * Known environment variables: | |
45 | * <ul> | |
d0114000 | 46 | * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode |
08fe2e33 NR |
47 | * {@link String}s when possible</li> |
48 | * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files | |
edd46289 NR |
49 | * before taking the usual ones; they will also be saved/updated into this |
50 | * path when the program starts</li> | |
d0114000 NR |
51 | * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR |
52 | * configuration value with 'true'</li> | |
53 | * </ul> | |
54 | * <p> | |
55 | * <ul> | |
56 | * <li>--import [URL]: import into library</li> | |
57 | * <li>--export [id] [output_type] [target]: export story to target</li> | |
58 | * <li>--convert [URL] [output_type] [target] (+info): convert URL into | |
59 | * target</li> | |
60 | * <li>--read [id] ([chapter number]): read the given story from the library | |
61 | * </li> | |
333f0e7b | 62 | * <li>--read-url [URL] ([chapter number]): convert on the fly and read the |
d0114000 | 63 | * story, without saving it</li> |
8b153400 NR |
64 | * <li>--search: list the supported websites (where)</li> |
65 | * <li>--search [where] [keywords] (page [page]) (item [item]): search on | |
66 | * the supported website and display the given results page of stories it | |
67 | * found, or the story details if asked</li> | |
68 | * <li>--tag [where]: list all the tags supported by this website</li> | |
69 | * <li>--tag [index 1]... (page [page]) (item [item]): search for the given | |
70 | * stories or subtags, tag by tag, and display information about a specific | |
71 | * page of results or about a specific item if requested</li> | |
333f0e7b | 72 | * <li>--list ([type]): list the stories present in the library</li> |
e10b51a2 NR |
73 | * <li>--set-source [id] [new source]: change the source of the given story</li> |
74 | * <li>--set-title [id] [new title]: change the title of the given story</li> | |
75 | * <li>--set-author [id] [new author]: change the author of the given story</li> | |
c1873e56 NR |
76 | * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL |
77 | * for this command</li> | |
39c3c689 | 78 | * <li>--version: get the version of the program</li> |
2070ced5 NR |
79 | * <li>--server [key] [port]: start a server on this port</li> |
80 | * <li>--stop-server [key] [port]: stop the running server on this port if | |
81 | * any</li> | |
82 | * <li>--remote [key] [host] [port]: use a the given remote library</li> | |
08fe2e33 NR |
83 | * </ul> |
84 | * | |
85 | * @param args | |
d0114000 | 86 | * see method description |
08fe2e33 NR |
87 | */ |
88 | public static void main(String[] args) { | |
d0114000 NR |
89 | String urlString = null; |
90 | String luid = null; | |
b0e88ebd | 91 | String sourceString = null; |
e10b51a2 NR |
92 | String titleString = null; |
93 | String authorString = null; | |
d0114000 NR |
94 | String chapString = null; |
95 | String target = null; | |
2070ced5 | 96 | String key = null; |
333f0e7b | 97 | MainAction action = MainAction.START; |
d0114000 | 98 | Boolean plusInfo = null; |
b0e88ebd NR |
99 | String host = null; |
100 | Integer port = null; | |
91b82a5c NR |
101 | SupportType searchOn = null; |
102 | String search = null; | |
8b153400 | 103 | List<Integer> tags = new ArrayList<Integer>(); |
91b82a5c NR |
104 | Integer page = null; |
105 | Integer item = null; | |
73ce17ef | 106 | |
d0114000 NR |
107 | boolean noMoreActions = false; |
108 | ||
109 | int exitCode = 0; | |
110 | for (int i = 0; exitCode == 0 && i < args.length; i++) { | |
111 | // Action (--) handling: | |
112 | if (!noMoreActions && args[i].startsWith("--")) { | |
113 | if (args[i].equals("--")) { | |
114 | noMoreActions = true; | |
115 | } else { | |
116 | try { | |
117 | action = MainAction.valueOf(args[i].substring(2) | |
118 | .toUpperCase().replace("-", "_")); | |
119 | } catch (Exception e) { | |
62c63b07 NR |
120 | Instance.getTraceHandler().error( |
121 | new IllegalArgumentException("Unknown action: " | |
122 | + args[i], e)); | |
d0114000 NR |
123 | exitCode = 255; |
124 | } | |
125 | } | |
08fe2e33 | 126 | |
d0114000 NR |
127 | continue; |
128 | } | |
129 | ||
130 | switch (action) { | |
131 | case IMPORT: | |
132 | if (urlString == null) { | |
133 | urlString = args[i]; | |
134 | } else { | |
135 | exitCode = 255; | |
136 | } | |
137 | break; | |
138 | case EXPORT: | |
139 | if (luid == null) { | |
140 | luid = args[i]; | |
b0e88ebd NR |
141 | } else if (sourceString == null) { |
142 | sourceString = args[i]; | |
d0114000 NR |
143 | } else if (target == null) { |
144 | target = args[i]; | |
145 | } else { | |
146 | exitCode = 255; | |
147 | } | |
148 | break; | |
149 | case CONVERT: | |
150 | if (urlString == null) { | |
151 | urlString = args[i]; | |
b0e88ebd NR |
152 | } else if (sourceString == null) { |
153 | sourceString = args[i]; | |
d0114000 NR |
154 | } else if (target == null) { |
155 | target = args[i]; | |
156 | } else if (plusInfo == null) { | |
157 | if ("+info".equals(args[i])) { | |
158 | plusInfo = true; | |
159 | } else { | |
160 | exitCode = 255; | |
161 | } | |
162 | } else { | |
163 | exitCode = 255; | |
08fe2e33 | 164 | } |
d0114000 NR |
165 | break; |
166 | case LIST: | |
b0e88ebd NR |
167 | if (sourceString == null) { |
168 | sourceString = args[i]; | |
d0114000 NR |
169 | } else { |
170 | exitCode = 255; | |
08fe2e33 | 171 | } |
d0114000 | 172 | break; |
e10b51a2 NR |
173 | case SET_SOURCE: |
174 | if (luid == null) { | |
175 | luid = args[i]; | |
176 | } else if (sourceString == null) { | |
177 | sourceString = args[i]; | |
178 | } else { | |
179 | exitCode = 255; | |
180 | } | |
181 | break; | |
182 | case SET_TITLE: | |
183 | if (luid == null) { | |
184 | luid = args[i]; | |
185 | } else if (sourceString == null) { | |
186 | titleString = args[i]; | |
187 | } else { | |
188 | exitCode = 255; | |
189 | } | |
190 | break; | |
191 | case SET_AUTHOR: | |
192 | if (luid == null) { | |
193 | luid = args[i]; | |
194 | } else if (sourceString == null) { | |
195 | authorString = args[i]; | |
196 | } else { | |
197 | exitCode = 255; | |
198 | } | |
199 | break; | |
d0114000 NR |
200 | case READ: |
201 | if (luid == null) { | |
202 | luid = args[i]; | |
203 | } else if (chapString == null) { | |
204 | chapString = args[i]; | |
205 | } else { | |
206 | exitCode = 255; | |
08fe2e33 | 207 | } |
d0114000 NR |
208 | break; |
209 | case READ_URL: | |
210 | if (urlString == null) { | |
211 | urlString = args[i]; | |
212 | } else if (chapString == null) { | |
213 | chapString = args[i]; | |
214 | } else { | |
215 | exitCode = 255; | |
08fe2e33 | 216 | } |
d0114000 | 217 | break; |
91b82a5c NR |
218 | case SEARCH: |
219 | if (searchOn == null) { | |
220 | searchOn = SupportType.valueOfAllOkUC(args[i]); | |
8b153400 | 221 | |
91b82a5c NR |
222 | if (searchOn == null) { |
223 | Instance.getTraceHandler().error( | |
224 | "Website not known: <" + args[i] + ">"); | |
225 | exitCode = 255; | |
226 | } | |
8b153400 | 227 | |
91b82a5c NR |
228 | if (BasicSearchable.getSearchable(searchOn) == null) { |
229 | Instance.getTraceHandler().error( | |
230 | "Website not supported: " + searchOn); | |
231 | exitCode = 255; | |
232 | } | |
233 | } else if (search == null) { | |
234 | search = args[i]; | |
8b153400 | 235 | } else if (page != null && page == -1) { |
91b82a5c NR |
236 | try { |
237 | page = Integer.parseInt(args[i]); | |
8b153400 NR |
238 | } catch (Exception e) { |
239 | page = -2; | |
91b82a5c | 240 | } |
8b153400 | 241 | } else if (item != null && item == -1) { |
91b82a5c NR |
242 | try { |
243 | item = Integer.parseInt(args[i]); | |
8b153400 NR |
244 | } catch (Exception e) { |
245 | item = -2; | |
246 | } | |
247 | } else if (page == null || item == null) { | |
248 | if (page == null && "page".equals(args[i])) { | |
249 | page = -1; | |
250 | } else if (item == null && "item".equals(args[i])) { | |
251 | item = -1; | |
252 | } else { | |
91b82a5c NR |
253 | exitCode = 255; |
254 | } | |
255 | } else { | |
256 | exitCode = 255; | |
257 | } | |
258 | break; | |
8b153400 | 259 | case TAG: |
91b82a5c NR |
260 | if (searchOn == null) { |
261 | searchOn = SupportType.valueOfAllOkUC(args[i]); | |
8b153400 | 262 | |
91b82a5c NR |
263 | if (searchOn == null) { |
264 | Instance.getTraceHandler().error( | |
265 | "Website not known: <" + args[i] + ">"); | |
266 | exitCode = 255; | |
267 | } | |
8b153400 | 268 | |
91b82a5c NR |
269 | if (BasicSearchable.getSearchable(searchOn) == null) { |
270 | Instance.getTraceHandler().error( | |
271 | "Website not supported: " + searchOn); | |
272 | exitCode = 255; | |
273 | } | |
8b153400 NR |
274 | } else if (page == null && item == null) { |
275 | if ("page".equals(args[i])) { | |
276 | page = -1; | |
277 | } else if ("item".equals(args[i])) { | |
278 | item = -1; | |
279 | } else { | |
280 | try { | |
281 | int index = Integer.parseInt(args[i]); | |
282 | tags.add(index); | |
283 | } catch (NumberFormatException e) { | |
284 | Instance.getTraceHandler().error( | |
285 | "Invalid tag index: " + args[i]); | |
286 | exitCode = 255; | |
287 | } | |
288 | } | |
289 | } else if (page != null && page == -1) { | |
290 | try { | |
291 | page = Integer.parseInt(args[i]); | |
292 | } catch (Exception e) { | |
293 | page = -2; | |
294 | } | |
295 | } else if (item != null && item == -1) { | |
296 | try { | |
297 | item = Integer.parseInt(args[i]); | |
298 | } catch (Exception e) { | |
299 | item = -2; | |
300 | } | |
301 | } else if (page == null || item == null) { | |
302 | if (page == null && "page".equals(args[i])) { | |
303 | page = -1; | |
304 | } else if (item == null && "item".equals(args[i])) { | |
305 | item = -1; | |
306 | } else { | |
307 | exitCode = 255; | |
308 | } | |
91b82a5c | 309 | } else { |
8b153400 | 310 | exitCode = 255; |
91b82a5c NR |
311 | } |
312 | break; | |
d0114000 NR |
313 | case HELP: |
314 | exitCode = 255; | |
315 | break; | |
316 | case SET_READER: | |
7de079f1 | 317 | exitCode = setReaderType(args[i]); |
c1873e56 | 318 | action = MainAction.START; |
d0114000 | 319 | break; |
333f0e7b NR |
320 | case START: |
321 | exitCode = 255; // not supposed to be selected by user | |
322 | break; | |
39c3c689 NR |
323 | case VERSION: |
324 | exitCode = 255; // no arguments for this option | |
b0e88ebd NR |
325 | break; |
326 | case SERVER: | |
5e848e6a | 327 | case STOP_SERVER: |
2070ced5 NR |
328 | if (key == null) { |
329 | key = args[i]; | |
330 | } else if (port == null) { | |
b0e88ebd NR |
331 | port = Integer.parseInt(args[i]); |
332 | } else { | |
333 | exitCode = 255; | |
334 | } | |
335 | break; | |
336 | case REMOTE: | |
2070ced5 NR |
337 | if (key == null) { |
338 | key = args[i]; | |
339 | } else if (host == null) { | |
b0e88ebd NR |
340 | host = args[i]; |
341 | } else if (port == null) { | |
342 | port = Integer.parseInt(args[i]); | |
ff05b828 | 343 | |
2070ced5 | 344 | BasicLibrary lib = new RemoteLibrary(key, host, port); |
5895a958 | 345 | lib = new CacheLibrary(Instance.getRemoteDir(host), lib); |
ff05b828 NR |
346 | |
347 | BasicReader.setDefaultLibrary(lib); | |
5e848e6a | 348 | |
b0e88ebd NR |
349 | action = MainAction.START; |
350 | } else { | |
351 | exitCode = 255; | |
352 | } | |
353 | break; | |
d0114000 NR |
354 | } |
355 | } | |
356 | ||
92fb0719 NR |
357 | final Progress mainProgress = new Progress(0, 80); |
358 | mainProgress.addProgressListener(new Progress.ProgressListener() { | |
359 | private int current = mainProgress.getMin(); | |
360 | ||
211f7ddb | 361 | @Override |
92fb0719 NR |
362 | public void progress(Progress progress, String name) { |
363 | int diff = progress.getProgress() - current; | |
364 | current += diff; | |
365 | ||
1822d603 NR |
366 | if (diff <= 0) |
367 | return; | |
368 | ||
92fb0719 NR |
369 | StringBuilder builder = new StringBuilder(); |
370 | for (int i = 0; i < diff; i++) { | |
371 | builder.append('.'); | |
372 | } | |
373 | ||
374 | System.err.print(builder.toString()); | |
375 | ||
376 | if (progress.isDone()) { | |
377 | System.err.println(""); | |
378 | } | |
379 | } | |
380 | }); | |
381 | Progress pg = new Progress(); | |
382 | mainProgress.addProgress(pg, mainProgress.getMax()); | |
383 | ||
b42117f1 NR |
384 | VersionCheck updates = VersionCheck.check(); |
385 | if (updates.isNewVersionAvailable()) { | |
386 | // Sent to syserr so not to cause problem if one tries to capture a | |
387 | // story content in text mode | |
388 | System.err | |
389 | .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases"); | |
390 | System.err.println(""); | |
391 | for (Version v : updates.getNewer()) { | |
392 | System.err.println("\tVersion " + v); | |
393 | System.err.println("\t-------------"); | |
394 | System.err.println(""); | |
91b82a5c NR |
395 | for (String it : updates.getChanges().get(v)) { |
396 | System.err.println("\t- " + it); | |
b42117f1 NR |
397 | } |
398 | System.err.println(""); | |
399 | } | |
400 | } | |
401 | ||
d0114000 NR |
402 | if (exitCode != 255) { |
403 | switch (action) { | |
404 | case IMPORT: | |
92fb0719 | 405 | exitCode = imprt(urlString, pg); |
b42117f1 | 406 | updates.ok(); // we consider it read |
d0114000 NR |
407 | break; |
408 | case EXPORT: | |
b0e88ebd | 409 | exitCode = export(luid, sourceString, target, pg); |
b42117f1 | 410 | updates.ok(); // we consider it read |
d0114000 NR |
411 | break; |
412 | case CONVERT: | |
b0e88ebd | 413 | exitCode = convert(urlString, sourceString, target, |
92fb0719 | 414 | plusInfo == null ? false : plusInfo, pg); |
b42117f1 | 415 | updates.ok(); // we consider it read |
d0114000 NR |
416 | break; |
417 | case LIST: | |
99ccbdf6 | 418 | if (BasicReader.getReader() == null) { |
62c63b07 NR |
419 | Instance.getTraceHandler() |
420 | .error(new Exception( | |
421 | "No reader type has been configured")); | |
99ccbdf6 NR |
422 | exitCode = 10; |
423 | break; | |
424 | } | |
b0e88ebd | 425 | exitCode = list(sourceString); |
d0114000 | 426 | break; |
e10b51a2 NR |
427 | case SET_SOURCE: |
428 | try { | |
429 | Instance.getLibrary().changeSource(luid, sourceString, pg); | |
430 | } catch (IOException e1) { | |
431 | Instance.getTraceHandler().error(e1); | |
432 | exitCode = 21; | |
433 | } | |
434 | break; | |
435 | case SET_TITLE: | |
436 | try { | |
437 | Instance.getLibrary().changeTitle(luid, titleString, pg); | |
438 | } catch (IOException e1) { | |
439 | Instance.getTraceHandler().error(e1); | |
440 | exitCode = 22; | |
441 | } | |
442 | break; | |
443 | case SET_AUTHOR: | |
444 | try { | |
445 | Instance.getLibrary().changeAuthor(luid, authorString, pg); | |
446 | } catch (IOException e1) { | |
447 | Instance.getTraceHandler().error(e1); | |
448 | exitCode = 23; | |
449 | } | |
450 | break; | |
d0114000 | 451 | case READ: |
99ccbdf6 | 452 | if (BasicReader.getReader() == null) { |
62c63b07 NR |
453 | Instance.getTraceHandler() |
454 | .error(new Exception( | |
455 | "No reader type has been configured")); | |
99ccbdf6 NR |
456 | exitCode = 10; |
457 | break; | |
458 | } | |
d0114000 NR |
459 | exitCode = read(luid, chapString, true); |
460 | break; | |
461 | case READ_URL: | |
99ccbdf6 | 462 | if (BasicReader.getReader() == null) { |
62c63b07 NR |
463 | Instance.getTraceHandler() |
464 | .error(new Exception( | |
465 | "No reader type has been configured")); | |
99ccbdf6 NR |
466 | exitCode = 10; |
467 | break; | |
468 | } | |
d0114000 | 469 | exitCode = read(urlString, chapString, false); |
91b82a5c NR |
470 | break; |
471 | case SEARCH: | |
8b153400 NR |
472 | page = page == null ? 1 : page; |
473 | if (page < 0) { | |
474 | Instance.getTraceHandler().error("Incorrect page number"); | |
91b82a5c NR |
475 | exitCode = 255; |
476 | break; | |
477 | } | |
8b153400 NR |
478 | |
479 | item = item == null ? 0 : item; | |
480 | if (item < 0) { | |
481 | Instance.getTraceHandler().error("Incorrect item number"); | |
482 | exitCode = 255; | |
483 | break; | |
91b82a5c | 484 | } |
8b153400 | 485 | |
91b82a5c NR |
486 | if (BasicReader.getReader() == null) { |
487 | Instance.getTraceHandler() | |
488 | .error(new Exception( | |
489 | "No reader type has been configured")); | |
490 | exitCode = 10; | |
491 | break; | |
492 | } | |
8b153400 | 493 | |
124442f1 | 494 | if (searchOn == null) { |
8b153400 NR |
495 | // TODO: do on reader!!! |
496 | for (SupportType type : SupportType.values()) { | |
497 | if (BasicSearchable.getSearchable(type) != null) { | |
498 | System.out.println(type); | |
499 | } | |
500 | } | |
124442f1 | 501 | } else if (search != null) { |
8b153400 NR |
502 | try { |
503 | BasicReader.getReader().search(searchOn, search, page, | |
f76de465 | 504 | item, true); |
8b153400 NR |
505 | } catch (IOException e1) { |
506 | Instance.getTraceHandler().error(e1); | |
507 | } | |
124442f1 NR |
508 | } else { |
509 | exitCode = 255; | |
91b82a5c | 510 | } |
8b153400 | 511 | |
91b82a5c | 512 | break; |
8b153400 | 513 | case TAG: |
91b82a5c NR |
514 | if (searchOn == null) { |
515 | exitCode = 255; | |
516 | break; | |
517 | } | |
91b82a5c | 518 | |
8b153400 NR |
519 | page = page == null ? 1 : page; |
520 | if (page < 0) { | |
521 | Instance.getTraceHandler().error("Incorrect page number"); | |
522 | exitCode = 255; | |
523 | break; | |
91b82a5c | 524 | } |
8b153400 NR |
525 | |
526 | item = item == null ? 0 : item; | |
527 | if (item < 0) { | |
528 | Instance.getTraceHandler().error("Incorrect item number"); | |
529 | exitCode = 255; | |
530 | break; | |
531 | } | |
532 | ||
91b82a5c NR |
533 | if (BasicReader.getReader() == null) { |
534 | Instance.getTraceHandler() | |
535 | .error(new Exception( | |
536 | "No reader type has been configured")); | |
537 | exitCode = 10; | |
538 | break; | |
539 | } | |
8b153400 | 540 | |
91b82a5c | 541 | try { |
8b153400 | 542 | BasicReader.getReader().searchTag(searchOn, page, item, |
f76de465 | 543 | true, tags.toArray(new Integer[] {})); |
91b82a5c NR |
544 | } catch (IOException e1) { |
545 | Instance.getTraceHandler().error(e1); | |
546 | } | |
8b153400 | 547 | |
d0114000 NR |
548 | break; |
549 | case HELP: | |
550 | syntax(true); | |
551 | exitCode = 0; | |
552 | break; | |
553 | case SET_READER: | |
b0e88ebd | 554 | exitCode = 255; |
d0114000 | 555 | break; |
39c3c689 NR |
556 | case VERSION: |
557 | System.out | |
558 | .println(String.format("Fanfix version %s" | |
9fe3f177 NR |
559 | + "%nhttps://github.com/nikiroo/fanfix/" |
560 | + "%n\tWritten by Nikiroo", | |
39c3c689 | 561 | Version.getCurrentVersion())); |
b42117f1 | 562 | updates.ok(); // we consider it read |
39c3c689 | 563 | break; |
333f0e7b | 564 | case START: |
99ccbdf6 | 565 | if (BasicReader.getReader() == null) { |
62c63b07 NR |
566 | Instance.getTraceHandler() |
567 | .error(new Exception( | |
568 | "No reader type has been configured")); | |
99ccbdf6 NR |
569 | exitCode = 10; |
570 | break; | |
571 | } | |
b0e88ebd NR |
572 | BasicReader.getReader().browse(null); |
573 | break; | |
574 | case SERVER: | |
575 | if (port == null) { | |
576 | exitCode = 255; | |
577 | break; | |
578 | } | |
579 | try { | |
62c63b07 | 580 | ServerObject server = new RemoteLibraryServer(key, port); |
22b2b942 | 581 | server.setTraceHandler(Instance.getTraceHandler()); |
edf79e5e | 582 | server.run(); |
b0e88ebd | 583 | } catch (IOException e) { |
62c63b07 | 584 | Instance.getTraceHandler().error(e); |
b0e88ebd NR |
585 | } |
586 | return; | |
5e848e6a NR |
587 | case STOP_SERVER: |
588 | if (port == null) { | |
589 | exitCode = 255; | |
590 | break; | |
591 | } | |
592 | ||
468b960b | 593 | new RemoteLibrary(key, host, port).exit(); |
5e848e6a | 594 | break; |
b0e88ebd | 595 | case REMOTE: |
99ccbdf6 | 596 | exitCode = 255; // should not be reachable (REMOTE -> START) |
333f0e7b | 597 | break; |
08fe2e33 NR |
598 | } |
599 | } | |
600 | ||
350bc060 NR |
601 | try { |
602 | Instance.getTempFiles().close(); | |
603 | } catch (IOException e) { | |
604 | Instance.getTraceHandler() | |
605 | .error(new IOException( | |
606 | "Cannot dispose of the temporary files", e)); | |
2aac79c7 NR |
607 | } |
608 | ||
08fe2e33 | 609 | if (exitCode == 255) { |
d0114000 | 610 | syntax(false); |
08fe2e33 NR |
611 | } |
612 | ||
31e28683 | 613 | System.exit(exitCode); |
08fe2e33 NR |
614 | } |
615 | ||
08fe2e33 | 616 | /** |
68e2c6d2 | 617 | * Import the given resource into the {@link LocalLibrary}. |
08fe2e33 | 618 | * |
d0114000 | 619 | * @param urlString |
08fe2e33 | 620 | * the resource to import |
92fb0719 NR |
621 | * @param pg |
622 | * the optional progress reporter | |
08fe2e33 NR |
623 | * |
624 | * @return the exit return code (0 = success) | |
625 | */ | |
92fb0719 | 626 | public static int imprt(String urlString, Progress pg) { |
08fe2e33 | 627 | try { |
3b2b638f NR |
628 | Story story = Instance.getLibrary().imprt( |
629 | BasicReader.getUrl(urlString), pg); | |
08fe2e33 NR |
630 | System.out.println(story.getMeta().getLuid() + ": \"" |
631 | + story.getMeta().getTitle() + "\" imported."); | |
632 | } catch (IOException e) { | |
62c63b07 | 633 | Instance.getTraceHandler().error(e); |
08fe2e33 NR |
634 | return 1; |
635 | } | |
636 | ||
637 | return 0; | |
638 | } | |
639 | ||
640 | /** | |
68e2c6d2 NR |
641 | * Export the {@link Story} from the {@link LocalLibrary} to the given |
642 | * target. | |
08fe2e33 | 643 | * |
73ce17ef | 644 | * @param luid |
08fe2e33 NR |
645 | * the story LUID |
646 | * @param typeString | |
647 | * the {@link OutputType} to use | |
648 | * @param target | |
649 | * the target | |
92fb0719 NR |
650 | * @param pg |
651 | * the optional progress reporter | |
08fe2e33 NR |
652 | * |
653 | * @return the exit return code (0 = success) | |
654 | */ | |
92fb0719 NR |
655 | public static int export(String luid, String typeString, String target, |
656 | Progress pg) { | |
e604986c | 657 | OutputType type = OutputType.valueOfNullOkUC(typeString, null); |
08fe2e33 | 658 | if (type == null) { |
62c63b07 NR |
659 | Instance.getTraceHandler().error( |
660 | new Exception(trans(StringId.OUTPUT_DESC, typeString))); | |
08fe2e33 NR |
661 | return 1; |
662 | } | |
663 | ||
664 | try { | |
92fb0719 | 665 | Instance.getLibrary().export(luid, type, target, pg); |
08fe2e33 | 666 | } catch (IOException e) { |
62c63b07 | 667 | Instance.getTraceHandler().error(e); |
08fe2e33 NR |
668 | return 4; |
669 | } | |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
674 | /** | |
68e2c6d2 NR |
675 | * List the stories of the given source from the {@link LocalLibrary} |
676 | * (unless NULL is passed, in which case all stories will be listed). | |
08fe2e33 | 677 | * |
b0e88ebd NR |
678 | * @param source |
679 | * the source to list the known stories of, or NULL to list all | |
333f0e7b | 680 | * stories |
08fe2e33 NR |
681 | * |
682 | * @return the exit return code (0 = success) | |
683 | */ | |
b0e88ebd | 684 | private static int list(String source) { |
f569d249 NR |
685 | List<MetaData> stories; |
686 | stories = BasicReader.getReader().getLibrary().getListBySource(source); | |
687 | ||
688 | for (MetaData story : stories) { | |
689 | String author = ""; | |
690 | if (story.getAuthor() != null && !story.getAuthor().isEmpty()) { | |
691 | author = " (" + story.getAuthor() + ")"; | |
692 | } | |
693 | ||
694 | System.out.println(story.getLuid() + ": " + story.getTitle() | |
695 | + author); | |
696 | } | |
08fe2e33 NR |
697 | return 0; |
698 | } | |
699 | ||
700 | /** | |
350bc060 | 701 | * Start the current reader for this {@link Story}. |
08fe2e33 NR |
702 | * |
703 | * @param story | |
68e2c6d2 NR |
704 | * the LUID of the {@link Story} in the {@link LocalLibrary} |
705 | * <b>or</b> the {@link Story} {@link URL} | |
d0114000 | 706 | * @param chapString |
08fe2e33 NR |
707 | * which {@link Chapter} to read (starting at 1), or NULL to get |
708 | * the {@link Story} description | |
709 | * @param library | |
710 | * TRUE if the source is the {@link Story} LUID, FALSE if it is a | |
711 | * {@link URL} | |
712 | * | |
713 | * @return the exit return code (0 = success) | |
714 | */ | |
d0114000 | 715 | private static int read(String story, String chapString, boolean library) { |
08fe2e33 | 716 | try { |
e42573a0 | 717 | Reader reader = BasicReader.getReader(); |
08fe2e33 | 718 | if (library) { |
bc2ea776 | 719 | reader.setMeta(story); |
08fe2e33 | 720 | } else { |
bc2ea776 | 721 | reader.setMeta(BasicReader.getUrl(story), null); |
08fe2e33 NR |
722 | } |
723 | ||
d0114000 NR |
724 | if (chapString != null) { |
725 | try { | |
bc2ea776 | 726 | reader.setChapter(Integer.parseInt(chapString)); |
350bc060 | 727 | reader.read(true); |
d0114000 | 728 | } catch (NumberFormatException e) { |
62c63b07 NR |
729 | Instance.getTraceHandler().error( |
730 | new IOException("Chapter number cannot be parsed: " | |
731 | + chapString, e)); | |
d0114000 NR |
732 | return 2; |
733 | } | |
08fe2e33 | 734 | } else { |
350bc060 | 735 | reader.read(true); |
08fe2e33 NR |
736 | } |
737 | } catch (IOException e) { | |
62c63b07 | 738 | Instance.getTraceHandler().error(e); |
08fe2e33 NR |
739 | return 1; |
740 | } | |
741 | ||
742 | return 0; | |
743 | } | |
744 | ||
745 | /** | |
746 | * Convert the {@link Story} into another format. | |
747 | * | |
d0114000 | 748 | * @param urlString |
08fe2e33 NR |
749 | * the source {@link Story} to convert |
750 | * @param typeString | |
751 | * the {@link OutputType} to convert to | |
d0114000 | 752 | * @param target |
08fe2e33 NR |
753 | * the target file |
754 | * @param infoCover | |
755 | * TRUE to also export the cover and info file, even if the given | |
756 | * {@link OutputType} does not usually save them | |
92fb0719 NR |
757 | * @param pg |
758 | * the optional progress reporter | |
08fe2e33 NR |
759 | * |
760 | * @return the exit return code (0 = success) | |
761 | */ | |
f7460e4c | 762 | public static int convert(String urlString, String typeString, |
92fb0719 | 763 | String target, boolean infoCover, Progress pg) { |
08fe2e33 NR |
764 | int exitCode = 0; |
765 | ||
826e4569 | 766 | Instance.getTraceHandler().trace("Convert: " + urlString); |
d0114000 | 767 | String sourceName = urlString; |
08fe2e33 | 768 | try { |
3b2b638f | 769 | URL source = BasicReader.getUrl(urlString); |
08fe2e33 NR |
770 | sourceName = source.toString(); |
771 | if (source.toString().startsWith("file://")) { | |
772 | sourceName = sourceName.substring("file://".length()); | |
773 | } | |
774 | ||
e604986c | 775 | OutputType type = OutputType.valueOfAllOkUC(typeString, null); |
08fe2e33 | 776 | if (type == null) { |
62c63b07 NR |
777 | Instance.getTraceHandler().error( |
778 | new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE, | |
779 | typeString))); | |
08fe2e33 NR |
780 | |
781 | exitCode = 2; | |
782 | } else { | |
783 | try { | |
784 | BasicSupport support = BasicSupport.getSupport(source); | |
333f0e7b | 785 | |
08fe2e33 | 786 | if (support != null) { |
350bc060 NR |
787 | Instance.getTraceHandler().trace( |
788 | "Support found: " + support.getClass()); | |
bee7dffe NR |
789 | Progress pgIn = new Progress(); |
790 | Progress pgOut = new Progress(); | |
791 | if (pg != null) { | |
792 | pg.setMax(2); | |
793 | pg.addProgress(pgIn, 1); | |
794 | pg.addProgress(pgOut, 1); | |
795 | } | |
08fe2e33 | 796 | |
0ffa4754 | 797 | Story story = support.process(pgIn); |
08fe2e33 | 798 | try { |
d0114000 | 799 | target = new File(target).getAbsolutePath(); |
925298fd NR |
800 | BasicOutput.getOutput(type, infoCover, infoCover) |
801 | .process(story, target, pgOut); | |
08fe2e33 | 802 | } catch (IOException e) { |
62c63b07 NR |
803 | Instance.getTraceHandler().error( |
804 | new IOException(trans(StringId.ERR_SAVING, | |
805 | target), e)); | |
08fe2e33 NR |
806 | exitCode = 5; |
807 | } | |
808 | } else { | |
62c63b07 NR |
809 | Instance.getTraceHandler().error( |
810 | new IOException(trans( | |
811 | StringId.ERR_NOT_SUPPORTED, source))); | |
08fe2e33 NR |
812 | |
813 | exitCode = 4; | |
814 | } | |
815 | } catch (IOException e) { | |
62c63b07 NR |
816 | Instance.getTraceHandler().error( |
817 | new IOException(trans(StringId.ERR_LOADING, | |
818 | sourceName), e)); | |
08fe2e33 NR |
819 | exitCode = 3; |
820 | } | |
821 | } | |
822 | } catch (MalformedURLException e) { | |
62c63b07 NR |
823 | Instance.getTraceHandler() |
824 | .error(new IOException(trans(StringId.ERR_BAD_URL, | |
825 | sourceName), e)); | |
08fe2e33 NR |
826 | exitCode = 1; |
827 | } | |
828 | ||
829 | return exitCode; | |
830 | } | |
831 | ||
832 | /** | |
833 | * Simple shortcut method to call {link Instance#getTrans()#getString()}. | |
834 | * | |
835 | * @param id | |
836 | * the ID to translate | |
837 | * | |
838 | * @return the translated result | |
839 | */ | |
840 | private static String trans(StringId id, Object... params) { | |
841 | return Instance.getTrans().getString(id, params); | |
842 | } | |
843 | ||
844 | /** | |
d0114000 NR |
845 | * Display the correct syntax of the program to the user to stdout, or an |
846 | * error message if the syntax used was wrong on stderr. | |
847 | * | |
848 | * @param showHelp | |
849 | * TRUE to show the syntax help, FALSE to show "syntax error" | |
08fe2e33 | 850 | */ |
d0114000 NR |
851 | private static void syntax(boolean showHelp) { |
852 | if (showHelp) { | |
853 | StringBuilder builder = new StringBuilder(); | |
854 | for (SupportType type : SupportType.values()) { | |
855 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
856 | type.getDesc())); | |
857 | builder.append('\n'); | |
858 | } | |
08fe2e33 | 859 | |
d0114000 NR |
860 | String typesIn = builder.toString(); |
861 | builder.setLength(0); | |
08fe2e33 | 862 | |
d0114000 NR |
863 | for (OutputType type : OutputType.values()) { |
864 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
4d205683 | 865 | type.getDesc(true))); |
d0114000 NR |
866 | builder.append('\n'); |
867 | } | |
08fe2e33 | 868 | |
d0114000 | 869 | String typesOut = builder.toString(); |
08fe2e33 | 870 | |
d0114000 NR |
871 | System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut)); |
872 | } else { | |
873 | System.err.println(trans(StringId.ERR_SYNTAX)); | |
874 | } | |
875 | } | |
876 | ||
877 | /** | |
878 | * Set the default reader type for this session only (it can be changed in | |
879 | * the configuration file, too, but this value will override it). | |
880 | * | |
881 | * @param readerTypeString | |
882 | * the type | |
883 | */ | |
884 | private static int setReaderType(String readerTypeString) { | |
885 | try { | |
7de079f1 NR |
886 | ReaderType readerType = ReaderType.valueOf(readerTypeString |
887 | .toUpperCase()); | |
d0114000 NR |
888 | BasicReader.setDefaultReaderType(readerType); |
889 | return 0; | |
890 | } catch (IllegalArgumentException e) { | |
62c63b07 NR |
891 | Instance.getTraceHandler().error( |
892 | new IOException("Unknown reader type: " + readerTypeString, | |
893 | e)); | |
d0114000 NR |
894 | return 1; |
895 | } | |
08fe2e33 NR |
896 | } |
897 | } |