Update nikiroo-utils, bugfixes:
[nikiroo-utils.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;
f569d249 7import java.util.List;
08fe2e33
NR
8
9import be.nikiroo.fanfix.bundles.StringId;
10import be.nikiroo.fanfix.data.Chapter;
f569d249 11import be.nikiroo.fanfix.data.MetaData;
08fe2e33 12import be.nikiroo.fanfix.data.Story;
ff05b828
NR
13import be.nikiroo.fanfix.library.BasicLibrary;
14import be.nikiroo.fanfix.library.CacheLibrary;
e42573a0
NR
15import be.nikiroo.fanfix.library.LocalLibrary;
16import be.nikiroo.fanfix.library.RemoteLibrary;
17import be.nikiroo.fanfix.library.RemoteLibraryServer;
08fe2e33
NR
18import be.nikiroo.fanfix.output.BasicOutput;
19import be.nikiroo.fanfix.output.BasicOutput.OutputType;
3727aae2 20import be.nikiroo.fanfix.reader.BasicReader;
e42573a0
NR
21import be.nikiroo.fanfix.reader.Reader;
22import be.nikiroo.fanfix.reader.Reader.ReaderType;
08fe2e33
NR
23import be.nikiroo.fanfix.supported.BasicSupport;
24import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
3b2b638f 25import be.nikiroo.utils.Progress;
62c63b07 26import be.nikiroo.utils.TraceHandler;
39c3c689 27import be.nikiroo.utils.Version;
62c63b07
NR
28import be.nikiroo.utils.serial.server.ConnectActionClientObject;
29import be.nikiroo.utils.serial.server.ServerObject;
08fe2e33
NR
30
31/**
32 * Main program entry point.
33 *
34 * @author niki
35 */
36public class Main {
d0114000 37 private enum MainAction {
5e848e6a 38 IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, SET_READER, START, VERSION, SERVER, STOP_SERVER, REMOTE,
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>
333f0e7b 64 * <li>--list ([type]): list the stories present in the library</li>
c1873e56
NR
65 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
66 * for this command</li>
39c3c689 67 * <li>--version: get the version of the program</li>
2070ced5
NR
68 * <li>--server [key] [port]: start a server on this port</li>
69 * <li>--stop-server [key] [port]: stop the running server on this port if
70 * any</li>
71 * <li>--remote [key] [host] [port]: use a the given remote library</li>
08fe2e33
NR
72 * </ul>
73 *
74 * @param args
d0114000 75 * see method description
08fe2e33
NR
76 */
77 public static void main(String[] args) {
d0114000
NR
78 String urlString = null;
79 String luid = null;
b0e88ebd 80 String sourceString = null;
d0114000
NR
81 String chapString = null;
82 String target = null;
2070ced5 83 String key = null;
333f0e7b 84 MainAction action = MainAction.START;
d0114000 85 Boolean plusInfo = null;
b0e88ebd
NR
86 String host = null;
87 Integer port = null;
73ce17ef 88
d0114000
NR
89 boolean noMoreActions = false;
90
91 int exitCode = 0;
92 for (int i = 0; exitCode == 0 && i < args.length; i++) {
93 // Action (--) handling:
94 if (!noMoreActions && args[i].startsWith("--")) {
95 if (args[i].equals("--")) {
96 noMoreActions = true;
97 } else {
98 try {
99 action = MainAction.valueOf(args[i].substring(2)
100 .toUpperCase().replace("-", "_"));
101 } catch (Exception e) {
62c63b07
NR
102 Instance.getTraceHandler().error(
103 new IllegalArgumentException("Unknown action: "
104 + args[i], e));
d0114000
NR
105 exitCode = 255;
106 }
107 }
08fe2e33 108
d0114000
NR
109 continue;
110 }
111
112 switch (action) {
113 case IMPORT:
114 if (urlString == null) {
115 urlString = args[i];
116 } else {
117 exitCode = 255;
118 }
119 break;
120 case EXPORT:
121 if (luid == null) {
122 luid = args[i];
b0e88ebd
NR
123 } else if (sourceString == null) {
124 sourceString = args[i];
d0114000
NR
125 } else if (target == null) {
126 target = args[i];
127 } else {
128 exitCode = 255;
129 }
130 break;
131 case CONVERT:
132 if (urlString == null) {
133 urlString = args[i];
b0e88ebd
NR
134 } else if (sourceString == null) {
135 sourceString = args[i];
d0114000
NR
136 } else if (target == null) {
137 target = args[i];
138 } else if (plusInfo == null) {
139 if ("+info".equals(args[i])) {
140 plusInfo = true;
141 } else {
142 exitCode = 255;
143 }
144 } else {
145 exitCode = 255;
08fe2e33 146 }
d0114000
NR
147 break;
148 case LIST:
b0e88ebd
NR
149 if (sourceString == null) {
150 sourceString = args[i];
d0114000
NR
151 } else {
152 exitCode = 255;
08fe2e33 153 }
d0114000
NR
154 break;
155 case READ:
156 if (luid == null) {
157 luid = args[i];
158 } else if (chapString == null) {
159 chapString = args[i];
160 } else {
161 exitCode = 255;
08fe2e33 162 }
d0114000
NR
163 break;
164 case READ_URL:
165 if (urlString == null) {
166 urlString = args[i];
167 } else if (chapString == null) {
168 chapString = args[i];
169 } else {
170 exitCode = 255;
08fe2e33 171 }
d0114000
NR
172 break;
173 case HELP:
174 exitCode = 255;
175 break;
176 case SET_READER:
7de079f1 177 exitCode = setReaderType(args[i]);
c1873e56 178 action = MainAction.START;
d0114000 179 break;
333f0e7b
NR
180 case START:
181 exitCode = 255; // not supposed to be selected by user
182 break;
39c3c689
NR
183 case VERSION:
184 exitCode = 255; // no arguments for this option
b0e88ebd
NR
185 break;
186 case SERVER:
5e848e6a 187 case STOP_SERVER:
2070ced5
NR
188 if (key == null) {
189 key = args[i];
190 } else if (port == null) {
b0e88ebd
NR
191 port = Integer.parseInt(args[i]);
192 } else {
193 exitCode = 255;
194 }
195 break;
196 case REMOTE:
2070ced5
NR
197 if (key == null) {
198 key = args[i];
199 } else if (host == null) {
b0e88ebd
NR
200 host = args[i];
201 } else if (port == null) {
202 port = Integer.parseInt(args[i]);
ff05b828
NR
203
204 File remoteCacheDir = Instance.getRemoteDir(host);
2070ced5 205 BasicLibrary lib = new RemoteLibrary(key, host, port);
ff05b828
NR
206 lib = new CacheLibrary(remoteCacheDir, lib);
207
208 BasicReader.setDefaultLibrary(lib);
5e848e6a 209
b0e88ebd
NR
210 action = MainAction.START;
211 } else {
212 exitCode = 255;
213 }
214 break;
d0114000
NR
215 }
216 }
217
92fb0719
NR
218 final Progress mainProgress = new Progress(0, 80);
219 mainProgress.addProgressListener(new Progress.ProgressListener() {
220 private int current = mainProgress.getMin();
221
211f7ddb 222 @Override
92fb0719
NR
223 public void progress(Progress progress, String name) {
224 int diff = progress.getProgress() - current;
225 current += diff;
226
227 StringBuilder builder = new StringBuilder();
228 for (int i = 0; i < diff; i++) {
229 builder.append('.');
230 }
231
232 System.err.print(builder.toString());
233
234 if (progress.isDone()) {
235 System.err.println("");
236 }
237 }
238 });
239 Progress pg = new Progress();
240 mainProgress.addProgress(pg, mainProgress.getMax());
241
b42117f1
NR
242 VersionCheck updates = VersionCheck.check();
243 if (updates.isNewVersionAvailable()) {
244 // Sent to syserr so not to cause problem if one tries to capture a
245 // story content in text mode
246 System.err
247 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
248 System.err.println("");
249 for (Version v : updates.getNewer()) {
250 System.err.println("\tVersion " + v);
251 System.err.println("\t-------------");
252 System.err.println("");
253 for (String item : updates.getChanges().get(v)) {
254 System.err.println("\t- " + item);
255 }
256 System.err.println("");
257 }
258 }
259
d0114000
NR
260 if (exitCode != 255) {
261 switch (action) {
262 case IMPORT:
92fb0719 263 exitCode = imprt(urlString, pg);
b42117f1 264 updates.ok(); // we consider it read
d0114000
NR
265 break;
266 case EXPORT:
b0e88ebd 267 exitCode = export(luid, sourceString, target, pg);
b42117f1 268 updates.ok(); // we consider it read
d0114000
NR
269 break;
270 case CONVERT:
b0e88ebd 271 exitCode = convert(urlString, sourceString, target,
92fb0719 272 plusInfo == null ? false : plusInfo, pg);
b42117f1 273 updates.ok(); // we consider it read
d0114000
NR
274 break;
275 case LIST:
99ccbdf6 276 if (BasicReader.getReader() == null) {
62c63b07
NR
277 Instance.getTraceHandler()
278 .error(new Exception(
279 "No reader type has been configured"));
99ccbdf6
NR
280 exitCode = 10;
281 break;
282 }
b0e88ebd 283 exitCode = list(sourceString);
d0114000
NR
284 break;
285 case READ:
99ccbdf6 286 if (BasicReader.getReader() == null) {
62c63b07
NR
287 Instance.getTraceHandler()
288 .error(new Exception(
289 "No reader type has been configured"));
99ccbdf6
NR
290 exitCode = 10;
291 break;
292 }
d0114000
NR
293 exitCode = read(luid, chapString, true);
294 break;
295 case READ_URL:
99ccbdf6 296 if (BasicReader.getReader() == null) {
62c63b07
NR
297 Instance.getTraceHandler()
298 .error(new Exception(
299 "No reader type has been configured"));
99ccbdf6
NR
300 exitCode = 10;
301 break;
302 }
d0114000
NR
303 exitCode = read(urlString, chapString, false);
304 break;
305 case HELP:
306 syntax(true);
307 exitCode = 0;
308 break;
309 case SET_READER:
b0e88ebd 310 exitCode = 255;
d0114000 311 break;
39c3c689
NR
312 case VERSION:
313 System.out
314 .println(String.format("Fanfix version %s"
315 + "\nhttps://github.com/nikiroo/fanfix/"
316 + "\n\tWritten by Nikiroo",
317 Version.getCurrentVersion()));
b42117f1 318 updates.ok(); // we consider it read
39c3c689 319 break;
333f0e7b 320 case START:
99ccbdf6 321 if (BasicReader.getReader() == null) {
62c63b07
NR
322 Instance.getTraceHandler()
323 .error(new Exception(
324 "No reader type has been configured"));
99ccbdf6
NR
325 exitCode = 10;
326 break;
327 }
b0e88ebd
NR
328 BasicReader.getReader().browse(null);
329 break;
330 case SERVER:
331 if (port == null) {
332 exitCode = 255;
333 break;
334 }
335 try {
62c63b07
NR
336 ServerObject server = new RemoteLibraryServer(key, port);
337 server.setTraceHandler(new TraceHandler(true, true, true));
b0e88ebd 338 server.start();
b0e88ebd 339 } catch (IOException e) {
62c63b07 340 Instance.getTraceHandler().error(e);
b0e88ebd
NR
341 }
342 return;
5e848e6a
NR
343 case STOP_SERVER:
344 if (port == null) {
345 exitCode = 255;
346 break;
347 }
348
349 try {
2070ced5 350 final String fkey = key;
62c63b07 351 new ConnectActionClientObject(host, port, true) {
5e848e6a
NR
352 @Override
353 public void action(Version serverVersion)
354 throws Exception {
355 try {
2070ced5 356 send(new Object[] { fkey, "EXIT" });
5e848e6a 357 } catch (Exception e) {
62c63b07 358 Instance.getTraceHandler().error(e);
5e848e6a
NR
359 }
360 }
361 }.connect();
362 } catch (IOException e) {
62c63b07 363 Instance.getTraceHandler().error(e);
5e848e6a
NR
364 }
365 break;
b0e88ebd 366 case REMOTE:
99ccbdf6 367 exitCode = 255; // should not be reachable (REMOTE -> START)
333f0e7b 368 break;
08fe2e33
NR
369 }
370 }
371
372 if (exitCode == 255) {
d0114000 373 syntax(false);
08fe2e33
NR
374 }
375
376 if (exitCode != 0) {
377 System.exit(exitCode);
378 }
379 }
380
08fe2e33 381 /**
68e2c6d2 382 * Import the given resource into the {@link LocalLibrary}.
08fe2e33 383 *
d0114000 384 * @param urlString
08fe2e33 385 * the resource to import
92fb0719
NR
386 * @param pg
387 * the optional progress reporter
08fe2e33
NR
388 *
389 * @return the exit return code (0 = success)
390 */
92fb0719 391 public static int imprt(String urlString, Progress pg) {
08fe2e33 392 try {
3b2b638f
NR
393 Story story = Instance.getLibrary().imprt(
394 BasicReader.getUrl(urlString), pg);
08fe2e33
NR
395 System.out.println(story.getMeta().getLuid() + ": \""
396 + story.getMeta().getTitle() + "\" imported.");
397 } catch (IOException e) {
62c63b07 398 Instance.getTraceHandler().error(e);
08fe2e33
NR
399 return 1;
400 }
401
402 return 0;
403 }
404
405 /**
68e2c6d2
NR
406 * Export the {@link Story} from the {@link LocalLibrary} to the given
407 * target.
08fe2e33 408 *
73ce17ef 409 * @param luid
08fe2e33
NR
410 * the story LUID
411 * @param typeString
412 * the {@link OutputType} to use
413 * @param target
414 * the target
92fb0719
NR
415 * @param pg
416 * the optional progress reporter
08fe2e33
NR
417 *
418 * @return the exit return code (0 = success)
419 */
92fb0719
NR
420 public static int export(String luid, String typeString, String target,
421 Progress pg) {
e604986c 422 OutputType type = OutputType.valueOfNullOkUC(typeString, null);
08fe2e33 423 if (type == null) {
62c63b07
NR
424 Instance.getTraceHandler().error(
425 new Exception(trans(StringId.OUTPUT_DESC, typeString)));
08fe2e33
NR
426 return 1;
427 }
428
429 try {
92fb0719 430 Instance.getLibrary().export(luid, type, target, pg);
08fe2e33 431 } catch (IOException e) {
62c63b07 432 Instance.getTraceHandler().error(e);
08fe2e33
NR
433 return 4;
434 }
435
436 return 0;
437 }
438
439 /**
68e2c6d2
NR
440 * List the stories of the given source from the {@link LocalLibrary}
441 * (unless NULL is passed, in which case all stories will be listed).
08fe2e33 442 *
b0e88ebd
NR
443 * @param source
444 * the source to list the known stories of, or NULL to list all
333f0e7b 445 * stories
08fe2e33
NR
446 *
447 * @return the exit return code (0 = success)
448 */
b0e88ebd 449 private static int list(String source) {
f569d249
NR
450 List<MetaData> stories;
451 stories = BasicReader.getReader().getLibrary().getListBySource(source);
452
453 for (MetaData story : stories) {
454 String author = "";
455 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
456 author = " (" + story.getAuthor() + ")";
457 }
458
459 System.out.println(story.getLuid() + ": " + story.getTitle()
460 + author);
461 }
08fe2e33
NR
462 return 0;
463 }
464
465 /**
466 * Start the CLI reader for this {@link Story}.
467 *
468 * @param story
68e2c6d2
NR
469 * the LUID of the {@link Story} in the {@link LocalLibrary}
470 * <b>or</b> the {@link Story} {@link URL}
d0114000 471 * @param chapString
08fe2e33
NR
472 * which {@link Chapter} to read (starting at 1), or NULL to get
473 * the {@link Story} description
474 * @param library
475 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
476 * {@link URL}
477 *
478 * @return the exit return code (0 = success)
479 */
d0114000 480 private static int read(String story, String chapString, boolean library) {
08fe2e33 481 try {
e42573a0 482 Reader reader = BasicReader.getReader();
08fe2e33 483 if (library) {
bc2ea776 484 reader.setMeta(story);
08fe2e33 485 } else {
bc2ea776 486 reader.setMeta(BasicReader.getUrl(story), null);
08fe2e33
NR
487 }
488
d0114000
NR
489 if (chapString != null) {
490 try {
bc2ea776
NR
491 reader.setChapter(Integer.parseInt(chapString));
492 reader.read();
d0114000 493 } catch (NumberFormatException e) {
62c63b07
NR
494 Instance.getTraceHandler().error(
495 new IOException("Chapter number cannot be parsed: "
496 + chapString, e));
d0114000
NR
497 return 2;
498 }
08fe2e33
NR
499 } else {
500 reader.read();
501 }
502 } catch (IOException e) {
62c63b07 503 Instance.getTraceHandler().error(e);
08fe2e33
NR
504 return 1;
505 }
506
507 return 0;
508 }
509
510 /**
511 * Convert the {@link Story} into another format.
512 *
d0114000 513 * @param urlString
08fe2e33
NR
514 * the source {@link Story} to convert
515 * @param typeString
516 * the {@link OutputType} to convert to
d0114000 517 * @param target
08fe2e33
NR
518 * the target file
519 * @param infoCover
520 * TRUE to also export the cover and info file, even if the given
521 * {@link OutputType} does not usually save them
92fb0719
NR
522 * @param pg
523 * the optional progress reporter
08fe2e33
NR
524 *
525 * @return the exit return code (0 = success)
526 */
d0114000 527 private static int convert(String urlString, String typeString,
92fb0719 528 String target, boolean infoCover, Progress pg) {
08fe2e33
NR
529 int exitCode = 0;
530
d0114000 531 String sourceName = urlString;
08fe2e33 532 try {
3b2b638f 533 URL source = BasicReader.getUrl(urlString);
08fe2e33
NR
534 sourceName = source.toString();
535 if (source.toString().startsWith("file://")) {
536 sourceName = sourceName.substring("file://".length());
537 }
538
e604986c 539 OutputType type = OutputType.valueOfAllOkUC(typeString, null);
08fe2e33 540 if (type == null) {
62c63b07
NR
541 Instance.getTraceHandler().error(
542 new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE,
543 typeString)));
08fe2e33
NR
544
545 exitCode = 2;
546 } else {
547 try {
548 BasicSupport support = BasicSupport.getSupport(source);
333f0e7b 549
08fe2e33 550 if (support != null) {
bee7dffe
NR
551 Progress pgIn = new Progress();
552 Progress pgOut = new Progress();
553 if (pg != null) {
554 pg.setMax(2);
555 pg.addProgress(pgIn, 1);
556 pg.addProgress(pgOut, 1);
557 }
08fe2e33 558
bee7dffe 559 Story story = support.process(source, pgIn);
08fe2e33 560 try {
d0114000 561 target = new File(target).getAbsolutePath();
08fe2e33 562 BasicOutput.getOutput(type, infoCover).process(
bee7dffe 563 story, target, pgOut);
08fe2e33 564 } catch (IOException e) {
62c63b07
NR
565 Instance.getTraceHandler().error(
566 new IOException(trans(StringId.ERR_SAVING,
567 target), e));
08fe2e33
NR
568 exitCode = 5;
569 }
570 } else {
62c63b07
NR
571 Instance.getTraceHandler().error(
572 new IOException(trans(
573 StringId.ERR_NOT_SUPPORTED, source)));
08fe2e33
NR
574
575 exitCode = 4;
576 }
577 } catch (IOException e) {
62c63b07
NR
578 Instance.getTraceHandler().error(
579 new IOException(trans(StringId.ERR_LOADING,
580 sourceName), e));
08fe2e33
NR
581 exitCode = 3;
582 }
583 }
584 } catch (MalformedURLException e) {
62c63b07
NR
585 Instance.getTraceHandler()
586 .error(new IOException(trans(StringId.ERR_BAD_URL,
587 sourceName), e));
08fe2e33
NR
588 exitCode = 1;
589 }
590
591 return exitCode;
592 }
593
594 /**
595 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
596 *
597 * @param id
598 * the ID to translate
599 *
600 * @return the translated result
601 */
602 private static String trans(StringId id, Object... params) {
603 return Instance.getTrans().getString(id, params);
604 }
605
606 /**
d0114000
NR
607 * Display the correct syntax of the program to the user to stdout, or an
608 * error message if the syntax used was wrong on stderr.
609 *
610 * @param showHelp
611 * TRUE to show the syntax help, FALSE to show "syntax error"
08fe2e33 612 */
d0114000
NR
613 private static void syntax(boolean showHelp) {
614 if (showHelp) {
615 StringBuilder builder = new StringBuilder();
616 for (SupportType type : SupportType.values()) {
617 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
618 type.getDesc()));
619 builder.append('\n');
620 }
08fe2e33 621
d0114000
NR
622 String typesIn = builder.toString();
623 builder.setLength(0);
08fe2e33 624
d0114000
NR
625 for (OutputType type : OutputType.values()) {
626 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
4d205683 627 type.getDesc(true)));
d0114000
NR
628 builder.append('\n');
629 }
08fe2e33 630
d0114000 631 String typesOut = builder.toString();
08fe2e33 632
d0114000
NR
633 System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut));
634 } else {
635 System.err.println(trans(StringId.ERR_SYNTAX));
636 }
637 }
638
639 /**
640 * Set the default reader type for this session only (it can be changed in
641 * the configuration file, too, but this value will override it).
642 *
643 * @param readerTypeString
644 * the type
645 */
646 private static int setReaderType(String readerTypeString) {
647 try {
7de079f1
NR
648 ReaderType readerType = ReaderType.valueOf(readerTypeString
649 .toUpperCase());
d0114000
NR
650 BasicReader.setDefaultReaderType(readerType);
651 return 0;
652 } catch (IllegalArgumentException e) {
62c63b07
NR
653 Instance.getTraceHandler().error(
654 new IOException("Unknown reader type: " + readerTypeString,
655 e));
d0114000
NR
656 return 1;
657 }
08fe2e33
NR
658 }
659}