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
.library
.LocalLibrary
;
12 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
13 import be
.nikiroo
.fanfix
.library
.RemoteLibraryServer
;
14 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
16 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
17 import be
.nikiroo
.fanfix
.reader
.Reader
;
18 import be
.nikiroo
.fanfix
.reader
.Reader
.ReaderType
;
19 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
20 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
21 import be
.nikiroo
.utils
.Progress
;
22 import be
.nikiroo
.utils
.Version
;
23 import be
.nikiroo
.utils
.serial
.Server
;
26 * Main program entry point.
31 private enum MainAction
{
32 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
, VERSION
, SERVER
, REMOTE
,
36 * Main program entry point.
38 * Known environment variables:
40 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
41 * {@link String}s when possible</li>
42 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
43 * before taking the usual ones; they will also be saved/updated into this
44 * path when the program starts</li>
45 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
46 * configuration value with 'true'</li>
50 * <li>--import [URL]: import into library</li>
51 * <li>--export [id] [output_type] [target]: export story to target</li>
52 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
54 * <li>--read [id] ([chapter number]): read the given story from the library
56 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
57 * story, without saving it</li>
58 * <li>--list ([type]): list the stories present in the library</li>
59 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
60 * for this command</li>
61 * <li>--version: get the version of the program</li>
62 * <li>--server [port]: start a server on this port</li>
63 * <li>--remote [host] [port]: use a the given remote library</li>
67 * see method description
69 public static void main(String
[] args
) {
70 String urlString
= null;
72 String sourceString
= null;
73 String chapString
= null;
75 MainAction action
= MainAction
.START
;
76 Boolean plusInfo
= null;
80 boolean noMoreActions
= false;
83 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
84 // Action (--) handling:
85 if (!noMoreActions
&& args
[i
].startsWith("--")) {
86 if (args
[i
].equals("--")) {
90 action
= MainAction
.valueOf(args
[i
].substring(2)
91 .toUpperCase().replace("-", "_"));
92 } catch (Exception e
) {
93 Instance
.syserr(new IllegalArgumentException(
94 "Unknown action: " + args
[i
], e
));
104 if (urlString
== null) {
113 } else if (sourceString
== null) {
114 sourceString
= args
[i
];
115 } else if (target
== null) {
122 if (urlString
== null) {
124 } else if (sourceString
== null) {
125 sourceString
= args
[i
];
126 } else if (target
== null) {
128 } else if (plusInfo
== null) {
129 if ("+info".equals(args
[i
])) {
139 if (sourceString
== null) {
140 sourceString
= args
[i
];
148 } else if (chapString
== null) {
149 chapString
= args
[i
];
155 if (urlString
== null) {
157 } else if (chapString
== null) {
158 chapString
= args
[i
];
167 exitCode
= setReaderType(args
[i
]);
168 action
= MainAction
.START
;
171 exitCode
= 255; // not supposed to be selected by user
174 exitCode
= 255; // no arguments for this option
178 port
= Integer
.parseInt(args
[i
]);
186 } else if (port
== null) {
187 port
= Integer
.parseInt(args
[i
]);
189 .setDefaultLibrary(new RemoteLibrary(host
, port
));
190 action
= MainAction
.START
;
198 final Progress mainProgress
= new Progress(0, 80);
199 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
200 private int current
= mainProgress
.getMin();
202 public void progress(Progress progress
, String name
) {
203 int diff
= progress
.getProgress() - current
;
206 StringBuilder builder
= new StringBuilder();
207 for (int i
= 0; i
< diff
; i
++) {
211 System
.err
.print(builder
.toString());
213 if (progress
.isDone()) {
214 System
.err
.println("");
218 Progress pg
= new Progress();
219 mainProgress
.addProgress(pg
, mainProgress
.getMax());
221 VersionCheck updates
= VersionCheck
.check();
222 if (updates
.isNewVersionAvailable()) {
223 // Sent to syserr so not to cause problem if one tries to capture a
224 // story content in text mode
226 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
227 System
.err
.println("");
228 for (Version v
: updates
.getNewer()) {
229 System
.err
.println("\tVersion " + v
);
230 System
.err
.println("\t-------------");
231 System
.err
.println("");
232 for (String item
: updates
.getChanges().get(v
)) {
233 System
.err
.println("\t- " + item
);
235 System
.err
.println("");
239 if (exitCode
!= 255) {
242 exitCode
= imprt(urlString
, pg
);
243 updates
.ok(); // we consider it read
246 exitCode
= export(luid
, sourceString
, target
, pg
);
247 updates
.ok(); // we consider it read
250 exitCode
= convert(urlString
, sourceString
, target
,
251 plusInfo
== null ?
false : plusInfo
, pg
);
252 updates
.ok(); // we consider it read
255 exitCode
= list(sourceString
);
258 exitCode
= read(luid
, chapString
, true);
261 exitCode
= read(urlString
, chapString
, false);
272 .println(String
.format("Fanfix version %s"
273 + "\nhttps://github.com/nikiroo/fanfix/"
274 + "\n\tWritten by Nikiroo",
275 Version
.getCurrentVersion()));
276 updates
.ok(); // we consider it read
279 BasicReader
.getReader().browse(null);
287 Server server
= new RemoteLibraryServer(port
);
289 System
.out
.println("Remote server started on: " + port
);
290 } catch (IOException e
) {
300 if (exitCode
== 255) {
305 System
.exit(exitCode
);
310 * Import the given resource into the {@link LocalLibrary}.
313 * the resource to import
315 * the optional progress reporter
317 * @return the exit return code (0 = success)
319 public static int imprt(String urlString
, Progress pg
) {
321 Story story
= Instance
.getLibrary().imprt(
322 BasicReader
.getUrl(urlString
), pg
);
323 System
.out
.println(story
.getMeta().getLuid() + ": \""
324 + story
.getMeta().getTitle() + "\" imported.");
325 } catch (IOException e
) {
334 * Export the {@link Story} from the {@link LocalLibrary} to the given
340 * the {@link OutputType} to use
344 * the optional progress reporter
346 * @return the exit return code (0 = success)
348 public static int export(String luid
, String typeString
, String target
,
350 OutputType type
= OutputType
.valueOfNullOkUC(typeString
);
352 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
358 Instance
.getLibrary().export(luid
, type
, target
, pg
);
359 } catch (IOException e
) {
368 * List the stories of the given source from the {@link LocalLibrary}
369 * (unless NULL is passed, in which case all stories will be listed).
372 * the source to list the known stories of, or NULL to list all
375 * @return the exit return code (0 = success)
377 private static int list(String source
) {
378 BasicReader
.getReader().browse(source
);
383 * Start the CLI reader for this {@link Story}.
386 * the LUID of the {@link Story} in the {@link LocalLibrary}
387 * <b>or</b> the {@link Story} {@link URL}
389 * which {@link Chapter} to read (starting at 1), or NULL to get
390 * the {@link Story} description
392 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
395 * @return the exit return code (0 = success)
397 private static int read(String story
, String chapString
, boolean library
) {
399 Reader reader
= BasicReader
.getReader();
401 reader
.setStory(story
, null);
403 reader
.setStory(BasicReader
.getUrl(story
), null);
406 if (chapString
!= null) {
408 reader
.read(Integer
.parseInt(chapString
));
409 } catch (NumberFormatException e
) {
410 Instance
.syserr(new IOException(
411 "Chapter number cannot be parsed: " + chapString
, e
));
417 } catch (IOException e
) {
426 * Convert the {@link Story} into another format.
429 * the source {@link Story} to convert
431 * the {@link OutputType} to convert to
435 * TRUE to also export the cover and info file, even if the given
436 * {@link OutputType} does not usually save them
438 * the optional progress reporter
440 * @return the exit return code (0 = success)
442 private static int convert(String urlString
, String typeString
,
443 String target
, boolean infoCover
, Progress pg
) {
446 String sourceName
= urlString
;
448 URL source
= BasicReader
.getUrl(urlString
);
449 sourceName
= source
.toString();
450 if (source
.toString().startsWith("file://")) {
451 sourceName
= sourceName
.substring("file://".length());
454 OutputType type
= OutputType
.valueOfAllOkUC(typeString
);
456 Instance
.syserr(new IOException(trans(
457 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
462 BasicSupport support
= BasicSupport
.getSupport(source
);
464 if (support
!= null) {
465 Progress pgIn
= new Progress();
466 Progress pgOut
= new Progress();
469 pg
.addProgress(pgIn
, 1);
470 pg
.addProgress(pgOut
, 1);
473 Story story
= support
.process(source
, pgIn
);
475 target
= new File(target
).getAbsolutePath();
476 BasicOutput
.getOutput(type
, infoCover
).process(
477 story
, target
, pgOut
);
478 } catch (IOException e
) {
479 Instance
.syserr(new IOException(trans(
480 StringId
.ERR_SAVING
, target
), e
));
484 Instance
.syserr(new IOException(trans(
485 StringId
.ERR_NOT_SUPPORTED
, source
)));
489 } catch (IOException e
) {
490 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
495 } catch (MalformedURLException e
) {
496 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
505 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
508 * the ID to translate
510 * @return the translated result
512 private static String
trans(StringId id
, Object
... params
) {
513 return Instance
.getTrans().getString(id
, params
);
517 * Display the correct syntax of the program to the user to stdout, or an
518 * error message if the syntax used was wrong on stderr.
521 * TRUE to show the syntax help, FALSE to show "syntax error"
523 private static void syntax(boolean showHelp
) {
525 StringBuilder builder
= new StringBuilder();
526 for (SupportType type
: SupportType
.values()) {
527 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
529 builder
.append('\n');
532 String typesIn
= builder
.toString();
533 builder
.setLength(0);
535 for (OutputType type
: OutputType
.values()) {
536 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
537 type
.getDesc(true)));
538 builder
.append('\n');
541 String typesOut
= builder
.toString();
543 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
545 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
550 * Set the default reader type for this session only (it can be changed in
551 * the configuration file, too, but this value will override it).
553 * @param readerTypeString
556 private static int setReaderType(String readerTypeString
) {
558 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
560 BasicReader
.setDefaultReaderType(readerType
);
562 } catch (IllegalArgumentException e
) {
563 Instance
.syserr(new IOException("Unknown reader type: "
564 + readerTypeString
, e
));