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> |
d0114000 NR |
55 | * <li>--set-reader [reader type]: set the reader type to CLI or LOCAL for |
56 | * 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]); |
d0114000 | 160 | break; |
333f0e7b NR |
161 | case START: |
162 | exitCode = 255; // not supposed to be selected by user | |
163 | break; | |
39c3c689 NR |
164 | case VERSION: |
165 | exitCode = 255; // no arguments for this option | |
d0114000 NR |
166 | } |
167 | } | |
168 | ||
92fb0719 NR |
169 | final Progress mainProgress = new Progress(0, 80); |
170 | mainProgress.addProgressListener(new Progress.ProgressListener() { | |
171 | private int current = mainProgress.getMin(); | |
172 | ||
173 | public void progress(Progress progress, String name) { | |
174 | int diff = progress.getProgress() - current; | |
175 | current += diff; | |
176 | ||
177 | StringBuilder builder = new StringBuilder(); | |
178 | for (int i = 0; i < diff; i++) { | |
179 | builder.append('.'); | |
180 | } | |
181 | ||
182 | System.err.print(builder.toString()); | |
183 | ||
184 | if (progress.isDone()) { | |
185 | System.err.println(""); | |
186 | } | |
187 | } | |
188 | }); | |
189 | Progress pg = new Progress(); | |
190 | mainProgress.addProgress(pg, mainProgress.getMax()); | |
191 | ||
d0114000 NR |
192 | if (exitCode != 255) { |
193 | switch (action) { | |
194 | case IMPORT: | |
92fb0719 | 195 | exitCode = imprt(urlString, pg); |
d0114000 NR |
196 | break; |
197 | case EXPORT: | |
92fb0719 | 198 | exitCode = export(luid, typeString, target, pg); |
d0114000 NR |
199 | break; |
200 | case CONVERT: | |
201 | exitCode = convert(urlString, typeString, target, | |
92fb0719 | 202 | plusInfo == null ? false : plusInfo, pg); |
d0114000 NR |
203 | break; |
204 | case LIST: | |
205 | exitCode = list(typeString); | |
206 | break; | |
207 | case READ: | |
208 | exitCode = read(luid, chapString, true); | |
209 | break; | |
210 | case READ_URL: | |
211 | exitCode = read(urlString, chapString, false); | |
212 | break; | |
213 | case HELP: | |
214 | syntax(true); | |
215 | exitCode = 0; | |
216 | break; | |
217 | case SET_READER: | |
d0114000 | 218 | break; |
39c3c689 NR |
219 | case VERSION: |
220 | System.out | |
221 | .println(String.format("Fanfix version %s" | |
222 | + "\nhttps://github.com/nikiroo/fanfix/" | |
223 | + "\n\tWritten by Nikiroo", | |
224 | Version.getCurrentVersion())); | |
225 | break; | |
333f0e7b | 226 | case START: |
b4dc6ab5 | 227 | UIUtils.setLookAndFeel(); |
333f0e7b NR |
228 | BasicReader.setDefaultReaderType(ReaderType.LOCAL); |
229 | BasicReader.getReader().start(null); | |
230 | break; | |
08fe2e33 NR |
231 | } |
232 | } | |
233 | ||
234 | if (exitCode == 255) { | |
d0114000 | 235 | syntax(false); |
08fe2e33 NR |
236 | } |
237 | ||
238 | if (exitCode != 0) { | |
239 | System.exit(exitCode); | |
240 | } | |
241 | } | |
242 | ||
08fe2e33 NR |
243 | /** |
244 | * Import the given resource into the {@link Library}. | |
245 | * | |
d0114000 | 246 | * @param urlString |
08fe2e33 | 247 | * the resource to import |
92fb0719 NR |
248 | * @param pg |
249 | * the optional progress reporter | |
08fe2e33 NR |
250 | * |
251 | * @return the exit return code (0 = success) | |
252 | */ | |
92fb0719 | 253 | public static int imprt(String urlString, Progress pg) { |
08fe2e33 | 254 | try { |
3b2b638f NR |
255 | Story story = Instance.getLibrary().imprt( |
256 | BasicReader.getUrl(urlString), pg); | |
08fe2e33 NR |
257 | System.out.println(story.getMeta().getLuid() + ": \"" |
258 | + story.getMeta().getTitle() + "\" imported."); | |
259 | } catch (IOException e) { | |
260 | Instance.syserr(e); | |
261 | return 1; | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Export the {@link Story} from the {@link Library} to the given target. | |
269 | * | |
73ce17ef | 270 | * @param luid |
08fe2e33 NR |
271 | * the story LUID |
272 | * @param typeString | |
273 | * the {@link OutputType} to use | |
274 | * @param target | |
275 | * the target | |
92fb0719 NR |
276 | * @param pg |
277 | * the optional progress reporter | |
08fe2e33 NR |
278 | * |
279 | * @return the exit return code (0 = success) | |
280 | */ | |
92fb0719 NR |
281 | public static int export(String luid, String typeString, String target, |
282 | Progress pg) { | |
08fe2e33 NR |
283 | OutputType type = OutputType.valueOfNullOkUC(typeString); |
284 | if (type == null) { | |
285 | Instance.syserr(new Exception(trans(StringId.OUTPUT_DESC, | |
286 | typeString))); | |
287 | return 1; | |
288 | } | |
289 | ||
290 | try { | |
92fb0719 | 291 | Instance.getLibrary().export(luid, type, target, pg); |
08fe2e33 NR |
292 | } catch (IOException e) { |
293 | Instance.syserr(e); | |
294 | return 4; | |
295 | } | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | /** | |
301 | * List the stories of the given type from the {@link Library} (unless NULL | |
302 | * is passed, in which case all stories will be listed). | |
303 | * | |
304 | * @param typeString | |
333f0e7b NR |
305 | * the type to list the known stories of, or NULL to list all |
306 | * stories | |
08fe2e33 NR |
307 | * |
308 | * @return the exit return code (0 = success) | |
309 | */ | |
333f0e7b | 310 | private static int list(String type) { |
3727aae2 | 311 | BasicReader.getReader().start(type); |
08fe2e33 NR |
312 | return 0; |
313 | } | |
314 | ||
315 | /** | |
316 | * Start the CLI reader for this {@link Story}. | |
317 | * | |
318 | * @param story | |
319 | * the LUID of the {@link Story} in the {@link Library} <b>or</b> | |
320 | * the {@link Story} {@link URL} | |
d0114000 | 321 | * @param chapString |
08fe2e33 NR |
322 | * which {@link Chapter} to read (starting at 1), or NULL to get |
323 | * the {@link Story} description | |
324 | * @param library | |
325 | * TRUE if the source is the {@link Story} LUID, FALSE if it is a | |
326 | * {@link URL} | |
327 | * | |
328 | * @return the exit return code (0 = success) | |
329 | */ | |
d0114000 | 330 | private static int read(String story, String chapString, boolean library) { |
08fe2e33 | 331 | try { |
3727aae2 | 332 | BasicReader reader = BasicReader.getReader(); |
08fe2e33 | 333 | if (library) { |
92fb0719 | 334 | reader.setStory(story, null); |
08fe2e33 | 335 | } else { |
3b2b638f | 336 | reader.setStory(BasicReader.getUrl(story), null); |
08fe2e33 NR |
337 | } |
338 | ||
d0114000 NR |
339 | if (chapString != null) { |
340 | try { | |
341 | reader.read(Integer.parseInt(chapString)); | |
342 | } catch (NumberFormatException e) { | |
343 | Instance.syserr(new IOException( | |
344 | "Chapter number cannot be parsed: " + chapString, e)); | |
345 | return 2; | |
346 | } | |
08fe2e33 NR |
347 | } else { |
348 | reader.read(); | |
349 | } | |
350 | } catch (IOException e) { | |
351 | Instance.syserr(e); | |
352 | return 1; | |
353 | } | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
358 | /** | |
359 | * Convert the {@link Story} into another format. | |
360 | * | |
d0114000 | 361 | * @param urlString |
08fe2e33 NR |
362 | * the source {@link Story} to convert |
363 | * @param typeString | |
364 | * the {@link OutputType} to convert to | |
d0114000 | 365 | * @param target |
08fe2e33 NR |
366 | * the target file |
367 | * @param infoCover | |
368 | * TRUE to also export the cover and info file, even if the given | |
369 | * {@link OutputType} does not usually save them | |
92fb0719 NR |
370 | * @param pg |
371 | * the optional progress reporter | |
08fe2e33 NR |
372 | * |
373 | * @return the exit return code (0 = success) | |
374 | */ | |
d0114000 | 375 | private static int convert(String urlString, String typeString, |
92fb0719 | 376 | String target, boolean infoCover, Progress pg) { |
08fe2e33 NR |
377 | int exitCode = 0; |
378 | ||
d0114000 | 379 | String sourceName = urlString; |
08fe2e33 | 380 | try { |
3b2b638f | 381 | URL source = BasicReader.getUrl(urlString); |
08fe2e33 NR |
382 | sourceName = source.toString(); |
383 | if (source.toString().startsWith("file://")) { | |
384 | sourceName = sourceName.substring("file://".length()); | |
385 | } | |
386 | ||
387 | OutputType type = OutputType.valueOfAllOkUC(typeString); | |
388 | if (type == null) { | |
389 | Instance.syserr(new IOException(trans( | |
390 | StringId.ERR_BAD_OUTPUT_TYPE, typeString))); | |
391 | ||
392 | exitCode = 2; | |
393 | } else { | |
394 | try { | |
395 | BasicSupport support = BasicSupport.getSupport(source); | |
333f0e7b | 396 | |
08fe2e33 | 397 | if (support != null) { |
bee7dffe NR |
398 | Progress pgIn = new Progress(); |
399 | Progress pgOut = new Progress(); | |
400 | if (pg != null) { | |
401 | pg.setMax(2); | |
402 | pg.addProgress(pgIn, 1); | |
403 | pg.addProgress(pgOut, 1); | |
404 | } | |
08fe2e33 | 405 | |
bee7dffe | 406 | Story story = support.process(source, pgIn); |
08fe2e33 | 407 | try { |
d0114000 | 408 | target = new File(target).getAbsolutePath(); |
08fe2e33 | 409 | BasicOutput.getOutput(type, infoCover).process( |
bee7dffe | 410 | story, target, pgOut); |
08fe2e33 NR |
411 | } catch (IOException e) { |
412 | Instance.syserr(new IOException(trans( | |
d0114000 | 413 | StringId.ERR_SAVING, target), e)); |
08fe2e33 NR |
414 | exitCode = 5; |
415 | } | |
416 | } else { | |
417 | Instance.syserr(new IOException(trans( | |
418 | StringId.ERR_NOT_SUPPORTED, source))); | |
419 | ||
420 | exitCode = 4; | |
421 | } | |
422 | } catch (IOException e) { | |
423 | Instance.syserr(new IOException(trans(StringId.ERR_LOADING, | |
424 | sourceName), e)); | |
425 | exitCode = 3; | |
426 | } | |
427 | } | |
428 | } catch (MalformedURLException e) { | |
429 | Instance.syserr(new IOException(trans(StringId.ERR_BAD_URL, | |
430 | sourceName), e)); | |
431 | exitCode = 1; | |
432 | } | |
433 | ||
434 | return exitCode; | |
435 | } | |
436 | ||
437 | /** | |
438 | * Simple shortcut method to call {link Instance#getTrans()#getString()}. | |
439 | * | |
440 | * @param id | |
441 | * the ID to translate | |
442 | * | |
443 | * @return the translated result | |
444 | */ | |
445 | private static String trans(StringId id, Object... params) { | |
446 | return Instance.getTrans().getString(id, params); | |
447 | } | |
448 | ||
449 | /** | |
d0114000 NR |
450 | * Display the correct syntax of the program to the user to stdout, or an |
451 | * error message if the syntax used was wrong on stderr. | |
452 | * | |
453 | * @param showHelp | |
454 | * TRUE to show the syntax help, FALSE to show "syntax error" | |
08fe2e33 | 455 | */ |
d0114000 NR |
456 | private static void syntax(boolean showHelp) { |
457 | if (showHelp) { | |
458 | StringBuilder builder = new StringBuilder(); | |
459 | for (SupportType type : SupportType.values()) { | |
460 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
461 | type.getDesc())); | |
462 | builder.append('\n'); | |
463 | } | |
08fe2e33 | 464 | |
d0114000 NR |
465 | String typesIn = builder.toString(); |
466 | builder.setLength(0); | |
08fe2e33 | 467 | |
d0114000 NR |
468 | for (OutputType type : OutputType.values()) { |
469 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
4d205683 | 470 | type.getDesc(true))); |
d0114000 NR |
471 | builder.append('\n'); |
472 | } | |
08fe2e33 | 473 | |
d0114000 | 474 | String typesOut = builder.toString(); |
08fe2e33 | 475 | |
d0114000 NR |
476 | System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut)); |
477 | } else { | |
478 | System.err.println(trans(StringId.ERR_SYNTAX)); | |
479 | } | |
480 | } | |
481 | ||
482 | /** | |
483 | * Set the default reader type for this session only (it can be changed in | |
484 | * the configuration file, too, but this value will override it). | |
485 | * | |
486 | * @param readerTypeString | |
487 | * the type | |
488 | */ | |
489 | private static int setReaderType(String readerTypeString) { | |
490 | try { | |
7de079f1 NR |
491 | ReaderType readerType = ReaderType.valueOf(readerTypeString |
492 | .toUpperCase()); | |
d0114000 NR |
493 | BasicReader.setDefaultReaderType(readerType); |
494 | return 0; | |
495 | } catch (IllegalArgumentException e) { | |
496 | Instance.syserr(new IOException("Unknown reader type: " | |
497 | + readerTypeString, e)); | |
498 | return 1; | |
499 | } | |
08fe2e33 NR |
500 | } |
501 | } |