1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
8 import be
.nikiroo
.fanfix
.bundles
.Config
;
9 import be
.nikiroo
.fanfix
.bundles
.StringId
;
10 import be
.nikiroo
.fanfix
.data
.Chapter
;
11 import be
.nikiroo
.fanfix
.data
.Story
;
12 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
13 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
14 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
15 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
16 import be
.nikiroo
.fanfix
.library
.RemoteLibraryServer
;
17 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
18 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
19 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
20 import be
.nikiroo
.fanfix
.reader
.Reader
;
21 import be
.nikiroo
.fanfix
.reader
.Reader
.ReaderType
;
22 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
23 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
24 import be
.nikiroo
.utils
.Progress
;
25 import be
.nikiroo
.utils
.Version
;
26 import be
.nikiroo
.utils
.serial
.Server
;
29 * Main program entry point.
34 private enum MainAction
{
35 IMPORT
, EXPORT
, CONVERT
, READ
, READ_URL
, LIST
, HELP
, SET_READER
, START
, VERSION
, SERVER
, REMOTE
,
39 * Main program entry point.
41 * Known environment variables:
43 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
44 * {@link String}s when possible</li>
45 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
46 * before taking the usual ones; they will also be saved/updated into this
47 * path when the program starts</li>
48 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
49 * configuration value with 'true'</li>
53 * <li>--import [URL]: import into library</li>
54 * <li>--export [id] [output_type] [target]: export story to target</li>
55 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
57 * <li>--read [id] ([chapter number]): read the given story from the library
59 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
60 * story, without saving it</li>
61 * <li>--list ([type]): list the stories present in the library</li>
62 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
63 * for this command</li>
64 * <li>--version: get the version of the program</li>
65 * <li>--server [port]: start a server on this port</li>
66 * <li>--remote [host] [port]: use a the given remote library</li>
70 * see method description
72 public static void main(String
[] args
) {
73 String urlString
= null;
75 String sourceString
= null;
76 String chapString
= null;
78 MainAction action
= MainAction
.START
;
79 Boolean plusInfo
= null;
83 boolean noMoreActions
= false;
86 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
87 // Action (--) handling:
88 if (!noMoreActions
&& args
[i
].startsWith("--")) {
89 if (args
[i
].equals("--")) {
93 action
= MainAction
.valueOf(args
[i
].substring(2)
94 .toUpperCase().replace("-", "_"));
95 } catch (Exception e
) {
96 Instance
.syserr(new IllegalArgumentException(
97 "Unknown action: " + args
[i
], e
));
107 if (urlString
== null) {
116 } else if (sourceString
== null) {
117 sourceString
= args
[i
];
118 } else if (target
== null) {
125 if (urlString
== null) {
127 } else if (sourceString
== null) {
128 sourceString
= args
[i
];
129 } else if (target
== null) {
131 } else if (plusInfo
== null) {
132 if ("+info".equals(args
[i
])) {
142 if (sourceString
== null) {
143 sourceString
= args
[i
];
151 } else if (chapString
== null) {
152 chapString
= args
[i
];
158 if (urlString
== null) {
160 } else if (chapString
== null) {
161 chapString
= args
[i
];
170 exitCode
= setReaderType(args
[i
]);
171 action
= MainAction
.START
;
174 exitCode
= 255; // not supposed to be selected by user
177 exitCode
= 255; // no arguments for this option
181 port
= Integer
.parseInt(args
[i
]);
189 } else if (port
== null) {
190 port
= Integer
.parseInt(args
[i
]);
192 File remoteCacheDir
= Instance
.getRemoteDir(host
);
193 BasicLibrary lib
= new RemoteLibrary(host
, port
);
194 lib
= new CacheLibrary(remoteCacheDir
, lib
);
196 BasicReader
.setDefaultLibrary(lib
);
198 action
= MainAction
.START
;
206 final Progress mainProgress
= new Progress(0, 80);
207 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
208 private int current
= mainProgress
.getMin();
211 public void progress(Progress progress
, String name
) {
212 int diff
= progress
.getProgress() - current
;
215 StringBuilder builder
= new StringBuilder();
216 for (int i
= 0; i
< diff
; i
++) {
220 System
.err
.print(builder
.toString());
222 if (progress
.isDone()) {
223 System
.err
.println("");
227 Progress pg
= new Progress();
228 mainProgress
.addProgress(pg
, mainProgress
.getMax());
230 VersionCheck updates
= VersionCheck
.check();
231 if (updates
.isNewVersionAvailable()) {
232 // Sent to syserr so not to cause problem if one tries to capture a
233 // story content in text mode
235 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
236 System
.err
.println("");
237 for (Version v
: updates
.getNewer()) {
238 System
.err
.println("\tVersion " + v
);
239 System
.err
.println("\t-------------");
240 System
.err
.println("");
241 for (String item
: updates
.getChanges().get(v
)) {
242 System
.err
.println("\t- " + item
);
244 System
.err
.println("");
248 if (exitCode
!= 255) {
251 exitCode
= imprt(urlString
, pg
);
252 updates
.ok(); // we consider it read
255 exitCode
= export(luid
, sourceString
, target
, pg
);
256 updates
.ok(); // we consider it read
259 exitCode
= convert(urlString
, sourceString
, target
,
260 plusInfo
== null ?
false : plusInfo
, pg
);
261 updates
.ok(); // we consider it read
264 if (BasicReader
.getReader() == null) {
265 Instance
.syserr(new Exception(
266 "No reader type has been configured"));
270 exitCode
= list(sourceString
);
273 if (BasicReader
.getReader() == null) {
274 Instance
.syserr(new Exception(
275 "No reader type has been configured"));
279 exitCode
= read(luid
, chapString
, true);
282 if (BasicReader
.getReader() == null) {
283 Instance
.syserr(new Exception(
284 "No reader type has been configured"));
288 exitCode
= read(urlString
, chapString
, false);
299 .println(String
.format("Fanfix version %s"
300 + "\nhttps://github.com/nikiroo/fanfix/"
301 + "\n\tWritten by Nikiroo",
302 Version
.getCurrentVersion()));
303 updates
.ok(); // we consider it read
306 if (BasicReader
.getReader() == null) {
307 Instance
.syserr(new Exception(
308 "No reader type has been configured"));
312 BasicReader
.getReader().browse(null);
320 Server server
= new RemoteLibraryServer(port
);
322 System
.out
.println("Remote server started on: " + port
);
323 } catch (IOException e
) {
328 exitCode
= 255; // should not be reachable (REMOTE -> START)
333 if (exitCode
== 255) {
338 System
.exit(exitCode
);
343 * Import the given resource into the {@link LocalLibrary}.
346 * the resource to import
348 * the optional progress reporter
350 * @return the exit return code (0 = success)
352 public static int imprt(String urlString
, Progress pg
) {
354 Story story
= Instance
.getLibrary().imprt(
355 BasicReader
.getUrl(urlString
), pg
);
356 System
.out
.println(story
.getMeta().getLuid() + ": \""
357 + story
.getMeta().getTitle() + "\" imported.");
358 } catch (IOException e
) {
367 * Export the {@link Story} from the {@link LocalLibrary} to the given
373 * the {@link OutputType} to use
377 * the optional progress reporter
379 * @return the exit return code (0 = success)
381 public static int export(String luid
, String typeString
, String target
,
383 OutputType type
= OutputType
.valueOfNullOkUC(typeString
, null);
385 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
391 Instance
.getLibrary().export(luid
, type
, target
, pg
);
392 } catch (IOException e
) {
401 * List the stories of the given source from the {@link LocalLibrary}
402 * (unless NULL is passed, in which case all stories will be listed).
405 * the source to list the known stories of, or NULL to list all
408 * @return the exit return code (0 = success)
410 private static int list(String source
) {
411 BasicReader
.getReader().browse(source
);
416 * Start the CLI reader for this {@link Story}.
419 * the LUID of the {@link Story} in the {@link LocalLibrary}
420 * <b>or</b> the {@link Story} {@link URL}
422 * which {@link Chapter} to read (starting at 1), or NULL to get
423 * the {@link Story} description
425 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
428 * @return the exit return code (0 = success)
430 private static int read(String story
, String chapString
, boolean library
) {
432 Reader reader
= BasicReader
.getReader();
434 reader
.setMeta(story
);
436 reader
.setMeta(BasicReader
.getUrl(story
), null);
439 if (chapString
!= null) {
441 reader
.setChapter(Integer
.parseInt(chapString
));
443 } catch (NumberFormatException e
) {
444 Instance
.syserr(new IOException(
445 "Chapter number cannot be parsed: " + chapString
, e
));
451 } catch (IOException e
) {
460 * Convert the {@link Story} into another format.
463 * the source {@link Story} to convert
465 * the {@link OutputType} to convert to
469 * TRUE to also export the cover and info file, even if the given
470 * {@link OutputType} does not usually save them
472 * the optional progress reporter
474 * @return the exit return code (0 = success)
476 private static int convert(String urlString
, String typeString
,
477 String target
, boolean infoCover
, Progress pg
) {
480 String sourceName
= urlString
;
482 URL source
= BasicReader
.getUrl(urlString
);
483 sourceName
= source
.toString();
484 if (source
.toString().startsWith("file://")) {
485 sourceName
= sourceName
.substring("file://".length());
488 OutputType type
= OutputType
.valueOfAllOkUC(typeString
, null);
490 Instance
.syserr(new IOException(trans(
491 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
496 BasicSupport support
= BasicSupport
.getSupport(source
);
498 if (support
!= null) {
499 Progress pgIn
= new Progress();
500 Progress pgOut
= new Progress();
503 pg
.addProgress(pgIn
, 1);
504 pg
.addProgress(pgOut
, 1);
507 Story story
= support
.process(source
, pgIn
);
509 target
= new File(target
).getAbsolutePath();
510 BasicOutput
.getOutput(type
, infoCover
).process(
511 story
, target
, pgOut
);
512 } catch (IOException e
) {
513 Instance
.syserr(new IOException(trans(
514 StringId
.ERR_SAVING
, target
), e
));
518 Instance
.syserr(new IOException(trans(
519 StringId
.ERR_NOT_SUPPORTED
, source
)));
523 } catch (IOException e
) {
524 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
529 } catch (MalformedURLException e
) {
530 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
539 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
542 * the ID to translate
544 * @return the translated result
546 private static String
trans(StringId id
, Object
... params
) {
547 return Instance
.getTrans().getString(id
, params
);
551 * Display the correct syntax of the program to the user to stdout, or an
552 * error message if the syntax used was wrong on stderr.
555 * TRUE to show the syntax help, FALSE to show "syntax error"
557 private static void syntax(boolean showHelp
) {
559 StringBuilder builder
= new StringBuilder();
560 for (SupportType type
: SupportType
.values()) {
561 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
563 builder
.append('\n');
566 String typesIn
= builder
.toString();
567 builder
.setLength(0);
569 for (OutputType type
: OutputType
.values()) {
570 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
571 type
.getDesc(true)));
572 builder
.append('\n');
575 String typesOut
= builder
.toString();
577 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
579 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
584 * Set the default reader type for this session only (it can be changed in
585 * the configuration file, too, but this value will override it).
587 * @param readerTypeString
590 private static int setReaderType(String readerTypeString
) {
592 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
594 BasicReader
.setDefaultReaderType(readerType
);
596 } catch (IllegalArgumentException e
) {
597 Instance
.syserr(new IOException("Unknown reader type: "
598 + readerTypeString
, e
));