1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
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
;
13 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
14 import be
.nikiroo
.fanfix
.reader
.BasicReader
.ReaderType
;
15 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
16 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
17 import be
.nikiroo
.utils
.Progress
;
18 import be
.nikiroo
.utils
.Version
;
19 import be
.nikiroo
.utils
.serial
.Server
;
22 * Main program entry point.
27 private enum MainAction
{
28 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
, VERSION
, SERVER
, REMOTE
,
32 * Main program entry point.
34 * Known environment variables:
36 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
37 * {@link String}s when possible</li>
38 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
39 * before taking the usual ones; they will also be saved/updated into this
40 * path when the program starts</li>
41 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
42 * configuration value with 'true'</li>
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
50 * <li>--read [id] ([chapter number]): read the given story from the library
52 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
53 * story, without saving it</li>
54 * <li>--list ([type]): list the stories present in the library</li>
55 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
56 * for this command</li>
57 * <li>--version: get the version of the program</li>
58 * <li>--server [port]: start a server on this port</li>
59 * <li>--remote [host] [port]: use a the given remote library</li>
63 * see method description
65 public static void main(String
[] args
) {
66 String urlString
= null;
68 String sourceString
= null;
69 String chapString
= null;
71 MainAction action
= MainAction
.START
;
72 Boolean plusInfo
= null;
76 boolean noMoreActions
= false;
79 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
80 // Action (--) handling:
81 if (!noMoreActions
&& args
[i
].startsWith("--")) {
82 if (args
[i
].equals("--")) {
86 action
= MainAction
.valueOf(args
[i
].substring(2)
87 .toUpperCase().replace("-", "_"));
88 } catch (Exception e
) {
89 Instance
.syserr(new IllegalArgumentException(
90 "Unknown action: " + args
[i
], e
));
100 if (urlString
== null) {
109 } else if (sourceString
== null) {
110 sourceString
= args
[i
];
111 } else if (target
== null) {
118 if (urlString
== null) {
120 } else if (sourceString
== null) {
121 sourceString
= args
[i
];
122 } else if (target
== null) {
124 } else if (plusInfo
== null) {
125 if ("+info".equals(args
[i
])) {
135 if (sourceString
== null) {
136 sourceString
= args
[i
];
144 } else if (chapString
== null) {
145 chapString
= args
[i
];
151 if (urlString
== null) {
153 } else if (chapString
== null) {
154 chapString
= args
[i
];
163 exitCode
= setReaderType(args
[i
]);
164 action
= MainAction
.START
;
167 exitCode
= 255; // not supposed to be selected by user
170 exitCode
= 255; // no arguments for this option
174 port
= Integer
.parseInt(args
[i
]);
182 } else if (port
== null) {
183 port
= Integer
.parseInt(args
[i
]);
185 .setDefaultLibrary(new RemoteLibrary(host
, port
));
186 action
= MainAction
.START
;
194 final Progress mainProgress
= new Progress(0, 80);
195 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
196 private int current
= mainProgress
.getMin();
198 public void progress(Progress progress
, String name
) {
199 int diff
= progress
.getProgress() - current
;
202 StringBuilder builder
= new StringBuilder();
203 for (int i
= 0; i
< diff
; i
++) {
207 System
.err
.print(builder
.toString());
209 if (progress
.isDone()) {
210 System
.err
.println("");
214 Progress pg
= new Progress();
215 mainProgress
.addProgress(pg
, mainProgress
.getMax());
217 VersionCheck updates
= VersionCheck
.check();
218 if (updates
.isNewVersionAvailable()) {
219 // Sent to syserr so not to cause problem if one tries to capture a
220 // story content in text mode
222 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
223 System
.err
.println("");
224 for (Version v
: updates
.getNewer()) {
225 System
.err
.println("\tVersion " + v
);
226 System
.err
.println("\t-------------");
227 System
.err
.println("");
228 for (String item
: updates
.getChanges().get(v
)) {
229 System
.err
.println("\t- " + item
);
231 System
.err
.println("");
235 if (exitCode
!= 255) {
238 exitCode
= imprt(urlString
, pg
);
239 updates
.ok(); // we consider it read
242 exitCode
= export(luid
, sourceString
, target
, pg
);
243 updates
.ok(); // we consider it read
246 exitCode
= convert(urlString
, sourceString
, target
,
247 plusInfo
== null ?
false : plusInfo
, pg
);
248 updates
.ok(); // we consider it read
251 exitCode
= list(sourceString
);
254 exitCode
= read(luid
, chapString
, true);
257 exitCode
= read(urlString
, chapString
, false);
268 .println(String
.format("Fanfix version %s"
269 + "\nhttps://github.com/nikiroo/fanfix/"
270 + "\n\tWritten by Nikiroo",
271 Version
.getCurrentVersion()));
272 updates
.ok(); // we consider it read
275 BasicReader
.getReader().browse(null);
283 Server server
= new RemoteLibraryServer(port
);
285 System
.out
.println("Remote server started on: " + port
);
286 } catch (IOException e
) {
296 if (exitCode
== 255) {
301 System
.exit(exitCode
);
306 * Import the given resource into the {@link LocalLibrary}.
309 * the resource to import
311 * the optional progress reporter
313 * @return the exit return code (0 = success)
315 public static int imprt(String urlString
, Progress pg
) {
317 Story story
= Instance
.getLibrary().imprt(
318 BasicReader
.getUrl(urlString
), pg
);
319 System
.out
.println(story
.getMeta().getLuid() + ": \""
320 + story
.getMeta().getTitle() + "\" imported.");
321 } catch (IOException e
) {
330 * Export the {@link Story} from the {@link LocalLibrary} to the given
336 * the {@link OutputType} to use
340 * the optional progress reporter
342 * @return the exit return code (0 = success)
344 public static int export(String luid
, String typeString
, String target
,
346 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
348 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
354 Instance
.getLibrary().export(luid
, type
, target
, pg
);
355 } catch (IOException e
) {
364 * List the stories of the given source from the {@link LocalLibrary}
365 * (unless NULL is passed, in which case all stories will be listed).
368 * the source to list the known stories of, or NULL to list all
371 * @return the exit return code (0 = success)
373 private static int list(String source
) {
374 BasicReader
.getReader().browse(source
);
379 * Start the CLI reader for this {@link Story}.
382 * the LUID of the {@link Story} in the {@link LocalLibrary}
383 * <b>or</b> the {@link Story} {@link URL}
385 * which {@link Chapter} to read (starting at 1), or NULL to get
386 * the {@link Story} description
388 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
391 * @return the exit return code (0 = success)
393 private static int read(String story
, String chapString
, boolean library
) {
395 BasicReader reader
= BasicReader
.getReader();
397 reader
.setStory(story
, null);
399 reader
.setStory(BasicReader
.getUrl(story
), null);
402 if (chapString
!= null) {
404 reader
.read(Integer
.parseInt(chapString
));
405 } catch (NumberFormatException e
) {
406 Instance
.syserr(new IOException(
407 "Chapter number cannot be parsed: " + chapString
, e
));
413 } catch (IOException e
) {
422 * Convert the {@link Story} into another format.
425 * the source {@link Story} to convert
427 * the {@link OutputType} to convert to
431 * TRUE to also export the cover and info file, even if the given
432 * {@link OutputType} does not usually save them
434 * the optional progress reporter
436 * @return the exit return code (0 = success)
438 private static int convert(String urlString
, String typeString
,
439 String target
, boolean infoCover
, Progress pg
) {
442 String sourceName
= urlString
;
444 URL source
= BasicReader
.getUrl(urlString
);
445 sourceName
= source
.toString();
446 if (source
.toString().startsWith("file://")) {
447 sourceName
= sourceName
.substring("file://".length());
450 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
452 Instance
.syserr(new IOException(trans(
453 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
458 BasicSupport support
= BasicSupport
.getSupport(source
);
460 if (support
!= null) {
461 Progress pgIn
= new Progress();
462 Progress pgOut
= new Progress();
465 pg
.addProgress(pgIn
, 1);
466 pg
.addProgress(pgOut
, 1);
469 Story story
= support
.process(source
, pgIn
);
471 target
= new File(target
).getAbsolutePath();
472 BasicOutput
.getOutput(type
, infoCover
).process(
473 story
, target
, pgOut
);
474 } catch (IOException e
) {
475 Instance
.syserr(new IOException(trans(
476 StringId
.ERR_SAVING
, target
), e
));
480 Instance
.syserr(new IOException(trans(
481 StringId
.ERR_NOT_SUPPORTED
, source
)));
485 } catch (IOException e
) {
486 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
491 } catch (MalformedURLException e
) {
492 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
501 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
504 * the ID to translate
506 * @return the translated result
508 private static String
trans(StringId id
, Object
... params
) {
509 return Instance
.getTrans().getString(id
, params
);
513 * Display the correct syntax of the program to the user to stdout, or an
514 * error message if the syntax used was wrong on stderr.
517 * TRUE to show the syntax help, FALSE to show "syntax error"
519 private static void syntax(boolean showHelp
) {
521 StringBuilder builder
= new StringBuilder();
522 for (SupportType type
: SupportType
.values()) {
523 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
525 builder
.append('\n');
528 String typesIn
= builder
.toString();
529 builder
.setLength(0);
531 for (OutputType type
: OutputType
.values()) {
532 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
533 type
.getDesc(true)));
534 builder
.append('\n');
537 String typesOut
= builder
.toString();
539 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
541 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
546 * Set the default reader type for this session only (it can be changed in
547 * the configuration file, too, but this value will override it).
549 * @param readerTypeString
552 private static int setReaderType(String readerTypeString
) {
554 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
556 BasicReader
.setDefaultReaderType(readerType
);
558 } catch (IllegalArgumentException e
) {
559 Instance
.syserr(new IOException("Unknown reader type: "
560 + readerTypeString
, e
));