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; | |
7 | ||
8 | import be.nikiroo.fanfix.bundles.StringId; | |
9 | import be.nikiroo.fanfix.data.Chapter; | |
10 | import be.nikiroo.fanfix.data.Story; | |
11 | import be.nikiroo.fanfix.output.BasicOutput; | |
12 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
3727aae2 | 13 | import be.nikiroo.fanfix.reader.BasicReader; |
d0114000 | 14 | import be.nikiroo.fanfix.reader.BasicReader.ReaderType; |
08fe2e33 NR |
15 | import be.nikiroo.fanfix.supported.BasicSupport; |
16 | import be.nikiroo.fanfix.supported.BasicSupport.SupportType; | |
3b2b638f | 17 | import be.nikiroo.utils.Progress; |
39c3c689 | 18 | import be.nikiroo.utils.Version; |
bf45069c | 19 | import be.nikiroo.utils.ui.UIUtils; |
08fe2e33 NR |
20 | |
21 | /** | |
22 | * Main program entry point. | |
23 | * | |
24 | * @author niki | |
25 | */ | |
26 | public class Main { | |
d0114000 | 27 | private enum MainAction { |
39c3c689 | 28 | IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, SET_READER, START, VERSION, |
d0114000 NR |
29 | } |
30 | ||
08fe2e33 NR |
31 | /** |
32 | * Main program entry point. | |
33 | * <p> | |
34 | * Known environment variables: | |
35 | * <ul> | |
d0114000 | 36 | * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode |
08fe2e33 NR |
37 | * {@link String}s when possible</li> |
38 | * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files | |
edd46289 NR |
39 | * before taking the usual ones; they will also be saved/updated into this |
40 | * path when the program starts</li> | |
d0114000 NR |
41 | * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR |
42 | * configuration value with 'true'</li> | |
43 | * </ul> | |
44 | * <p> | |
45 | * <ul> | |
46 | * <li>--import [URL]: import into library</li> | |
47 | * <li>--export [id] [output_type] [target]: export story to target</li> | |
48 | * <li>--convert [URL] [output_type] [target] (+info): convert URL into | |
49 | * target</li> | |
50 | * <li>--read [id] ([chapter number]): read the given story from the library | |
51 | * </li> | |
333f0e7b | 52 | * <li>--read-url [URL] ([chapter number]): convert on the fly and read the |
d0114000 | 53 | * story, without saving it</li> |
333f0e7b | 54 | * <li>--list ([type]): list the stories present in the library</li> |
c1873e56 NR |
55 | * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL |
56 | * for this command</li> | |
39c3c689 | 57 | * <li>--version: get the version of the program</li> |
08fe2e33 NR |
58 | * </ul> |
59 | * | |
60 | * @param args | |
d0114000 | 61 | * see method description |
08fe2e33 NR |
62 | */ |
63 | public static void main(String[] args) { | |
d0114000 NR |
64 | String urlString = null; |
65 | String luid = null; | |
66 | String typeString = null; | |
67 | String chapString = null; | |
68 | String target = null; | |
333f0e7b | 69 | MainAction action = MainAction.START; |
d0114000 | 70 | Boolean plusInfo = null; |
73ce17ef | 71 | |
d0114000 NR |
72 | boolean noMoreActions = false; |
73 | ||
74 | int exitCode = 0; | |
75 | for (int i = 0; exitCode == 0 && i < args.length; i++) { | |
76 | // Action (--) handling: | |
77 | if (!noMoreActions && args[i].startsWith("--")) { | |
78 | if (args[i].equals("--")) { | |
79 | noMoreActions = true; | |
80 | } else { | |
81 | try { | |
82 | action = MainAction.valueOf(args[i].substring(2) | |
83 | .toUpperCase().replace("-", "_")); | |
84 | } catch (Exception e) { | |
85 | Instance.syserr(new IllegalArgumentException( | |
86 | "Unknown action: " + args[i], e)); | |
87 | exitCode = 255; | |
88 | } | |
89 | } | |
08fe2e33 | 90 | |
d0114000 NR |
91 | continue; |
92 | } | |
93 | ||
94 | switch (action) { | |
95 | case IMPORT: | |
96 | if (urlString == null) { | |
97 | urlString = args[i]; | |
98 | } else { | |
99 | exitCode = 255; | |
100 | } | |
101 | break; | |
102 | case EXPORT: | |
103 | if (luid == null) { | |
104 | luid = args[i]; | |
105 | } else if (typeString == null) { | |
106 | typeString = args[i]; | |
107 | } else if (target == null) { | |
108 | target = args[i]; | |
109 | } else { | |
110 | exitCode = 255; | |
111 | } | |
112 | break; | |
113 | case CONVERT: | |
114 | if (urlString == null) { | |
115 | urlString = args[i]; | |
116 | } else if (typeString == null) { | |
117 | typeString = args[i]; | |
118 | } else if (target == null) { | |
119 | target = args[i]; | |
120 | } else if (plusInfo == null) { | |
121 | if ("+info".equals(args[i])) { | |
122 | plusInfo = true; | |
123 | } else { | |
124 | exitCode = 255; | |
125 | } | |
126 | } else { | |
127 | exitCode = 255; | |
08fe2e33 | 128 | } |
d0114000 NR |
129 | break; |
130 | case LIST: | |
131 | if (typeString == null) { | |
132 | typeString = args[i]; | |
133 | } else { | |
134 | exitCode = 255; | |
08fe2e33 | 135 | } |
d0114000 NR |
136 | break; |
137 | case READ: | |
138 | if (luid == null) { | |
139 | luid = args[i]; | |
140 | } else if (chapString == null) { | |
141 | chapString = args[i]; | |
142 | } else { | |
143 | exitCode = 255; | |
08fe2e33 | 144 | } |
d0114000 NR |
145 | break; |
146 | case READ_URL: | |
147 | if (urlString == null) { | |
148 | urlString = args[i]; | |
149 | } else if (chapString == null) { | |
150 | chapString = args[i]; | |
151 | } else { | |
152 | exitCode = 255; | |
08fe2e33 | 153 | } |
d0114000 NR |
154 | break; |
155 | case HELP: | |
156 | exitCode = 255; | |
157 | break; | |
158 | case SET_READER: | |
7de079f1 | 159 | exitCode = setReaderType(args[i]); |
c1873e56 | 160 | action = MainAction.START; |
d0114000 | 161 | break; |
333f0e7b NR |
162 | case START: |
163 | exitCode = 255; // not supposed to be selected by user | |
164 | break; | |
39c3c689 NR |
165 | case VERSION: |
166 | exitCode = 255; // no arguments for this option | |
d0114000 NR |
167 | } |
168 | } | |
169 | ||
92fb0719 NR |
170 | final Progress mainProgress = new Progress(0, 80); |
171 | mainProgress.addProgressListener(new Progress.ProgressListener() { | |
172 | private int current = mainProgress.getMin(); | |
173 | ||
174 | public void progress(Progress progress, String name) { | |
175 | int diff = progress.getProgress() - current; | |
176 | current += diff; | |
177 | ||
178 | StringBuilder builder = new StringBuilder(); | |
179 | for (int i = 0; i < diff; i++) { | |
180 | builder.append('.'); | |
181 | } | |
182 | ||
183 | System.err.print(builder.toString()); | |
184 | ||
185 | if (progress.isDone()) { | |
186 | System.err.println(""); | |
187 | } | |
188 | } | |
189 | }); | |
190 | Progress pg = new Progress(); | |
191 | mainProgress.addProgress(pg, mainProgress.getMax()); | |
192 | ||
b42117f1 NR |
193 | VersionCheck updates = VersionCheck.check(); |
194 | if (updates.isNewVersionAvailable()) { | |
195 | // Sent to syserr so not to cause problem if one tries to capture a | |
196 | // story content in text mode | |
197 | System.err | |
198 | .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases"); | |
199 | System.err.println(""); | |
200 | for (Version v : updates.getNewer()) { | |
201 | System.err.println("\tVersion " + v); | |
202 | System.err.println("\t-------------"); | |
203 | System.err.println(""); | |
204 | for (String item : updates.getChanges().get(v)) { | |
205 | System.err.println("\t- " + item); | |
206 | } | |
207 | System.err.println(""); | |
208 | } | |
209 | } | |
210 | ||
d0114000 NR |
211 | if (exitCode != 255) { |
212 | switch (action) { | |
213 | case IMPORT: | |
92fb0719 | 214 | exitCode = imprt(urlString, pg); |
b42117f1 | 215 | updates.ok(); // we consider it read |
d0114000 NR |
216 | break; |
217 | case EXPORT: | |
92fb0719 | 218 | exitCode = export(luid, typeString, target, pg); |
b42117f1 | 219 | updates.ok(); // we consider it read |
d0114000 NR |
220 | break; |
221 | case CONVERT: | |
222 | exitCode = convert(urlString, typeString, target, | |
92fb0719 | 223 | plusInfo == null ? false : plusInfo, pg); |
b42117f1 | 224 | updates.ok(); // we consider it read |
d0114000 NR |
225 | break; |
226 | case LIST: | |
227 | exitCode = list(typeString); | |
228 | break; | |
229 | case READ: | |
230 | exitCode = read(luid, chapString, true); | |
231 | break; | |
232 | case READ_URL: | |
233 | exitCode = read(urlString, chapString, false); | |
234 | break; | |
235 | case HELP: | |
236 | syntax(true); | |
237 | exitCode = 0; | |
238 | break; | |
239 | case SET_READER: | |
d0114000 | 240 | break; |
39c3c689 NR |
241 | case VERSION: |
242 | System.out | |
243 | .println(String.format("Fanfix version %s" | |
244 | + "\nhttps://github.com/nikiroo/fanfix/" | |
245 | + "\n\tWritten by Nikiroo", | |
246 | Version.getCurrentVersion())); | |
b42117f1 | 247 | updates.ok(); // we consider it read |
39c3c689 | 248 | break; |
333f0e7b | 249 | case START: |
c1873e56 | 250 | //BasicReader.setDefaultReaderType(ReaderType.LOCAL); |
333f0e7b NR |
251 | BasicReader.getReader().start(null); |
252 | break; | |
08fe2e33 NR |
253 | } |
254 | } | |
255 | ||
256 | if (exitCode == 255) { | |
d0114000 | 257 | syntax(false); |
08fe2e33 NR |
258 | } |
259 | ||
260 | if (exitCode != 0) { | |
261 | System.exit(exitCode); | |
262 | } | |
263 | } | |
264 | ||
08fe2e33 NR |
265 | /** |
266 | * Import the given resource into the {@link Library}. | |
267 | * | |
d0114000 | 268 | * @param urlString |
08fe2e33 | 269 | * the resource to import |
92fb0719 NR |
270 | * @param pg |
271 | * the optional progress reporter | |
08fe2e33 NR |
272 | * |
273 | * @return the exit return code (0 = success) | |
274 | */ | |
92fb0719 | 275 | public static int imprt(String urlString, Progress pg) { |
08fe2e33 | 276 | try { |
3b2b638f NR |
277 | Story story = Instance.getLibrary().imprt( |
278 | BasicReader.getUrl(urlString), pg); | |
08fe2e33 NR |
279 | System.out.println(story.getMeta().getLuid() + ": \"" |
280 | + story.getMeta().getTitle() + "\" imported."); | |
281 | } catch (IOException e) { | |
282 | Instance.syserr(e); | |
283 | return 1; | |
284 | } | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
289 | /** | |
290 | * Export the {@link Story} from the {@link Library} to the given target. | |
291 | * | |
73ce17ef | 292 | * @param luid |
08fe2e33 NR |
293 | * the story LUID |
294 | * @param typeString | |
295 | * the {@link OutputType} to use | |
296 | * @param target | |
297 | * the target | |
92fb0719 NR |
298 | * @param pg |
299 | * the optional progress reporter | |
08fe2e33 NR |
300 | * |
301 | * @return the exit return code (0 = success) | |
302 | */ | |
92fb0719 NR |
303 | public static int export(String luid, String typeString, String target, |
304 | Progress pg) { | |
08fe2e33 NR |
305 | OutputType type = OutputType.valueOfNullOkUC(typeString); |
306 | if (type == null) { | |
307 | Instance.syserr(new Exception(trans(StringId.OUTPUT_DESC, | |
308 | typeString))); | |
309 | return 1; | |
310 | } | |
311 | ||
312 | try { | |
92fb0719 | 313 | Instance.getLibrary().export(luid, type, target, pg); |
08fe2e33 NR |
314 | } catch (IOException e) { |
315 | Instance.syserr(e); | |
316 | return 4; | |
317 | } | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
322 | /** | |
323 | * List the stories of the given type from the {@link Library} (unless NULL | |
324 | * is passed, in which case all stories will be listed). | |
325 | * | |
0efd25e3 | 326 | * @param type |
333f0e7b NR |
327 | * the type to list the known stories of, or NULL to list all |
328 | * stories | |
08fe2e33 NR |
329 | * |
330 | * @return the exit return code (0 = success) | |
331 | */ | |
333f0e7b | 332 | private static int list(String type) { |
3727aae2 | 333 | BasicReader.getReader().start(type); |
08fe2e33 NR |
334 | return 0; |
335 | } | |
336 | ||
337 | /** | |
338 | * Start the CLI reader for this {@link Story}. | |
339 | * | |
340 | * @param story | |
341 | * the LUID of the {@link Story} in the {@link Library} <b>or</b> | |
342 | * the {@link Story} {@link URL} | |
d0114000 | 343 | * @param chapString |
08fe2e33 NR |
344 | * which {@link Chapter} to read (starting at 1), or NULL to get |
345 | * the {@link Story} description | |
346 | * @param library | |
347 | * TRUE if the source is the {@link Story} LUID, FALSE if it is a | |
348 | * {@link URL} | |
349 | * | |
350 | * @return the exit return code (0 = success) | |
351 | */ | |
d0114000 | 352 | private static int read(String story, String chapString, boolean library) { |
08fe2e33 | 353 | try { |
3727aae2 | 354 | BasicReader reader = BasicReader.getReader(); |
08fe2e33 | 355 | if (library) { |
92fb0719 | 356 | reader.setStory(story, null); |
08fe2e33 | 357 | } else { |
3b2b638f | 358 | reader.setStory(BasicReader.getUrl(story), null); |
08fe2e33 NR |
359 | } |
360 | ||
d0114000 NR |
361 | if (chapString != null) { |
362 | try { | |
363 | reader.read(Integer.parseInt(chapString)); | |
364 | } catch (NumberFormatException e) { | |
365 | Instance.syserr(new IOException( | |
366 | "Chapter number cannot be parsed: " + chapString, e)); | |
367 | return 2; | |
368 | } | |
08fe2e33 NR |
369 | } else { |
370 | reader.read(); | |
371 | } | |
372 | } catch (IOException e) { | |
373 | Instance.syserr(e); | |
374 | return 1; | |
375 | } | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | /** | |
381 | * Convert the {@link Story} into another format. | |
382 | * | |
d0114000 | 383 | * @param urlString |
08fe2e33 NR |
384 | * the source {@link Story} to convert |
385 | * @param typeString | |
386 | * the {@link OutputType} to convert to | |
d0114000 | 387 | * @param target |
08fe2e33 NR |
388 | * the target file |
389 | * @param infoCover | |
390 | * TRUE to also export the cover and info file, even if the given | |
391 | * {@link OutputType} does not usually save them | |
92fb0719 NR |
392 | * @param pg |
393 | * the optional progress reporter | |
08fe2e33 NR |
394 | * |
395 | * @return the exit return code (0 = success) | |
396 | */ | |
d0114000 | 397 | private static int convert(String urlString, String typeString, |
92fb0719 | 398 | String target, boolean infoCover, Progress pg) { |
08fe2e33 NR |
399 | int exitCode = 0; |
400 | ||
d0114000 | 401 | String sourceName = urlString; |
08fe2e33 | 402 | try { |
3b2b638f | 403 | URL source = BasicReader.getUrl(urlString); |
08fe2e33 NR |
404 | sourceName = source.toString(); |
405 | if (source.toString().startsWith("file://")) { | |
406 | sourceName = sourceName.substring("file://".length()); | |
407 | } | |
408 | ||
409 | OutputType type = OutputType.valueOfAllOkUC(typeString); | |
410 | if (type == null) { | |
411 | Instance.syserr(new IOException(trans( | |
412 | StringId.ERR_BAD_OUTPUT_TYPE, typeString))); | |
413 | ||
414 | exitCode = 2; | |
415 | } else { | |
416 | try { | |
417 | BasicSupport support = BasicSupport.getSupport(source); | |
333f0e7b | 418 | |
08fe2e33 | 419 | if (support != null) { |
bee7dffe NR |
420 | Progress pgIn = new Progress(); |
421 | Progress pgOut = new Progress(); | |
422 | if (pg != null) { | |
423 | pg.setMax(2); | |
424 | pg.addProgress(pgIn, 1); | |
425 | pg.addProgress(pgOut, 1); | |
426 | } | |
08fe2e33 | 427 | |
bee7dffe | 428 | Story story = support.process(source, pgIn); |
08fe2e33 | 429 | try { |
d0114000 | 430 | target = new File(target).getAbsolutePath(); |
08fe2e33 | 431 | BasicOutput.getOutput(type, infoCover).process( |
bee7dffe | 432 | story, target, pgOut); |
08fe2e33 NR |
433 | } catch (IOException e) { |
434 | Instance.syserr(new IOException(trans( | |
d0114000 | 435 | StringId.ERR_SAVING, target), e)); |
08fe2e33 NR |
436 | exitCode = 5; |
437 | } | |
438 | } else { | |
439 | Instance.syserr(new IOException(trans( | |
440 | StringId.ERR_NOT_SUPPORTED, source))); | |
441 | ||
442 | exitCode = 4; | |
443 | } | |
444 | } catch (IOException e) { | |
445 | Instance.syserr(new IOException(trans(StringId.ERR_LOADING, | |
446 | sourceName), e)); | |
447 | exitCode = 3; | |
448 | } | |
449 | } | |
450 | } catch (MalformedURLException e) { | |
451 | Instance.syserr(new IOException(trans(StringId.ERR_BAD_URL, | |
452 | sourceName), e)); | |
453 | exitCode = 1; | |
454 | } | |
455 | ||
456 | return exitCode; | |
457 | } | |
458 | ||
459 | /** | |
460 | * Simple shortcut method to call {link Instance#getTrans()#getString()}. | |
461 | * | |
462 | * @param id | |
463 | * the ID to translate | |
464 | * | |
465 | * @return the translated result | |
466 | */ | |
467 | private static String trans(StringId id, Object... params) { | |
468 | return Instance.getTrans().getString(id, params); | |
469 | } | |
470 | ||
471 | /** | |
d0114000 NR |
472 | * Display the correct syntax of the program to the user to stdout, or an |
473 | * error message if the syntax used was wrong on stderr. | |
474 | * | |
475 | * @param showHelp | |
476 | * TRUE to show the syntax help, FALSE to show "syntax error" | |
08fe2e33 | 477 | */ |
d0114000 NR |
478 | private static void syntax(boolean showHelp) { |
479 | if (showHelp) { | |
480 | StringBuilder builder = new StringBuilder(); | |
481 | for (SupportType type : SupportType.values()) { | |
482 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
483 | type.getDesc())); | |
484 | builder.append('\n'); | |
485 | } | |
08fe2e33 | 486 | |
d0114000 NR |
487 | String typesIn = builder.toString(); |
488 | builder.setLength(0); | |
08fe2e33 | 489 | |
d0114000 NR |
490 | for (OutputType type : OutputType.values()) { |
491 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
4d205683 | 492 | type.getDesc(true))); |
d0114000 NR |
493 | builder.append('\n'); |
494 | } | |
08fe2e33 | 495 | |
d0114000 | 496 | String typesOut = builder.toString(); |
08fe2e33 | 497 | |
d0114000 NR |
498 | System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut)); |
499 | } else { | |
500 | System.err.println(trans(StringId.ERR_SYNTAX)); | |
501 | } | |
502 | } | |
503 | ||
504 | /** | |
505 | * Set the default reader type for this session only (it can be changed in | |
506 | * the configuration file, too, but this value will override it). | |
507 | * | |
508 | * @param readerTypeString | |
509 | * the type | |
510 | */ | |
511 | private static int setReaderType(String readerTypeString) { | |
512 | try { | |
7de079f1 NR |
513 | ReaderType readerType = ReaderType.valueOf(readerTypeString |
514 | .toUpperCase()); | |
d0114000 NR |
515 | BasicReader.setDefaultReaderType(readerType); |
516 | return 0; | |
517 | } catch (IllegalArgumentException e) { | |
518 | Instance.syserr(new IOException("Unknown reader type: " | |
519 | + readerTypeString, e)); | |
520 | return 1; | |
521 | } | |
08fe2e33 NR |
522 | } |
523 | } |