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