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
.BasicLibrary
;
12 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
13 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
14 import be
.nikiroo
.fanfix
.library
.RemoteLibrary
;
15 import be
.nikiroo
.fanfix
.library
.RemoteLibraryServer
;
16 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
17 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
18 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
19 import be
.nikiroo
.fanfix
.reader
.Reader
;
20 import be
.nikiroo
.fanfix
.reader
.Reader
.ReaderType
;
21 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
22 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
23 import be
.nikiroo
.utils
.Progress
;
24 import be
.nikiroo
.utils
.Version
;
25 import be
.nikiroo
.utils
.serial
.ConnectActionClient
;
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
, STOP_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>--stop-server [port]: stop the running server on this port if any</li>
67 * <li>--remote [host] [port]: use a the given remote library</li>
71 * see method description
73 public static void main(String
[] args
) {
74 String urlString
= null;
76 String sourceString
= null;
77 String chapString
= null;
79 MainAction action
= MainAction
.START
;
80 Boolean plusInfo
= null;
84 boolean noMoreActions
= false;
87 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
88 // Action (--) handling:
89 if (!noMoreActions
&& args
[i
].startsWith("--")) {
90 if (args
[i
].equals("--")) {
94 action
= MainAction
.valueOf(args
[i
].substring(2)
95 .toUpperCase().replace("-", "_"));
96 } catch (Exception e
) {
97 Instance
.syserr(new IllegalArgumentException(
98 "Unknown action: " + args
[i
], e
));
108 if (urlString
== null) {
117 } else if (sourceString
== null) {
118 sourceString
= args
[i
];
119 } else if (target
== null) {
126 if (urlString
== null) {
128 } else if (sourceString
== null) {
129 sourceString
= args
[i
];
130 } else if (target
== null) {
132 } else if (plusInfo
== null) {
133 if ("+info".equals(args
[i
])) {
143 if (sourceString
== null) {
144 sourceString
= args
[i
];
152 } else if (chapString
== null) {
153 chapString
= args
[i
];
159 if (urlString
== null) {
161 } else if (chapString
== null) {
162 chapString
= args
[i
];
171 exitCode
= setReaderType(args
[i
]);
172 action
= MainAction
.START
;
175 exitCode
= 255; // not supposed to be selected by user
178 exitCode
= 255; // no arguments for this option
183 port
= Integer
.parseInt(args
[i
]);
191 } else if (port
== null) {
192 port
= Integer
.parseInt(args
[i
]);
194 File remoteCacheDir
= Instance
.getRemoteDir(host
);
195 BasicLibrary lib
= new RemoteLibrary(host
, port
);
196 lib
= new CacheLibrary(remoteCacheDir
, lib
);
198 BasicReader
.setDefaultLibrary(lib
);
200 action
= MainAction
.START
;
208 final Progress mainProgress
= new Progress(0, 80);
209 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
210 private int current
= mainProgress
.getMin();
213 public void progress(Progress progress
, String name
) {
214 int diff
= progress
.getProgress() - current
;
217 StringBuilder builder
= new StringBuilder();
218 for (int i
= 0; i
< diff
; i
++) {
222 System
.err
.print(builder
.toString());
224 if (progress
.isDone()) {
225 System
.err
.println("");
229 Progress pg
= new Progress();
230 mainProgress
.addProgress(pg
, mainProgress
.getMax());
232 VersionCheck updates
= VersionCheck
.check();
233 if (updates
.isNewVersionAvailable()) {
234 // Sent to syserr so not to cause problem if one tries to capture a
235 // story content in text mode
237 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
238 System
.err
.println("");
239 for (Version v
: updates
.getNewer()) {
240 System
.err
.println("\tVersion " + v
);
241 System
.err
.println("\t-------------");
242 System
.err
.println("");
243 for (String item
: updates
.getChanges().get(v
)) {
244 System
.err
.println("\t- " + item
);
246 System
.err
.println("");
250 if (exitCode
!= 255) {
253 exitCode
= imprt(urlString
, pg
);
254 updates
.ok(); // we consider it read
257 exitCode
= export(luid
, sourceString
, target
, pg
);
258 updates
.ok(); // we consider it read
261 exitCode
= convert(urlString
, sourceString
, target
,
262 plusInfo
== null ?
false : plusInfo
, pg
);
263 updates
.ok(); // we consider it read
266 if (BasicReader
.getReader() == null) {
267 Instance
.syserr(new Exception(
268 "No reader type has been configured"));
272 exitCode
= list(sourceString
);
275 if (BasicReader
.getReader() == null) {
276 Instance
.syserr(new Exception(
277 "No reader type has been configured"));
281 exitCode
= read(luid
, chapString
, true);
284 if (BasicReader
.getReader() == null) {
285 Instance
.syserr(new Exception(
286 "No reader type has been configured"));
290 exitCode
= read(urlString
, chapString
, false);
301 .println(String
.format("Fanfix version %s"
302 + "\nhttps://github.com/nikiroo/fanfix/"
303 + "\n\tWritten by Nikiroo",
304 Version
.getCurrentVersion()));
305 updates
.ok(); // we consider it read
308 if (BasicReader
.getReader() == null) {
309 Instance
.syserr(new Exception(
310 "No reader type has been configured"));
314 BasicReader
.getReader().browse(null);
322 Server server
= new RemoteLibraryServer(port
);
324 System
.out
.println("Remote server started on: " + port
);
325 } catch (IOException e
) {
336 new ConnectActionClient(host
, port
, true) {
338 public void action(Version serverVersion
)
341 send(new Object
[] { "EXIT" });
342 } catch (Exception e
) {
347 } catch (IOException e
) {
352 exitCode
= 255; // should not be reachable (REMOTE -> START)
357 if (exitCode
== 255) {
362 System
.exit(exitCode
);
367 * Import the given resource into the {@link LocalLibrary}.
370 * the resource to import
372 * the optional progress reporter
374 * @return the exit return code (0 = success)
376 public static int imprt(String urlString
, Progress pg
) {
378 Story story
= Instance
.getLibrary().imprt(
379 BasicReader
.getUrl(urlString
), pg
);
380 System
.out
.println(story
.getMeta().getLuid() + ": \""
381 + story
.getMeta().getTitle() + "\" imported.");
382 } catch (IOException e
) {
391 * Export the {@link Story} from the {@link LocalLibrary} to the given
397 * the {@link OutputType} to use
401 * the optional progress reporter
403 * @return the exit return code (0 = success)
405 public static int export(String luid
, String typeString
, String target
,
407 OutputType type
= OutputType
.valueOfNullOkUC(typeString
, null);
409 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
415 Instance
.getLibrary().export(luid
, type
, target
, pg
);
416 } catch (IOException e
) {
425 * List the stories of the given source from the {@link LocalLibrary}
426 * (unless NULL is passed, in which case all stories will be listed).
429 * the source to list the known stories of, or NULL to list all
432 * @return the exit return code (0 = success)
434 private static int list(String source
) {
435 BasicReader
.getReader().browse(source
);
440 * Start the CLI reader for this {@link Story}.
443 * the LUID of the {@link Story} in the {@link LocalLibrary}
444 * <b>or</b> the {@link Story} {@link URL}
446 * which {@link Chapter} to read (starting at 1), or NULL to get
447 * the {@link Story} description
449 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
452 * @return the exit return code (0 = success)
454 private static int read(String story
, String chapString
, boolean library
) {
456 Reader reader
= BasicReader
.getReader();
458 reader
.setMeta(story
);
460 reader
.setMeta(BasicReader
.getUrl(story
), null);
463 if (chapString
!= null) {
465 reader
.setChapter(Integer
.parseInt(chapString
));
467 } catch (NumberFormatException e
) {
468 Instance
.syserr(new IOException(
469 "Chapter number cannot be parsed: " + chapString
, e
));
475 } catch (IOException e
) {
484 * Convert the {@link Story} into another format.
487 * the source {@link Story} to convert
489 * the {@link OutputType} to convert to
493 * TRUE to also export the cover and info file, even if the given
494 * {@link OutputType} does not usually save them
496 * the optional progress reporter
498 * @return the exit return code (0 = success)
500 private static int convert(String urlString
, String typeString
,
501 String target
, boolean infoCover
, Progress pg
) {
504 String sourceName
= urlString
;
506 URL source
= BasicReader
.getUrl(urlString
);
507 sourceName
= source
.toString();
508 if (source
.toString().startsWith("file://")) {
509 sourceName
= sourceName
.substring("file://".length());
512 OutputType type
= OutputType
.valueOfAllOkUC(typeString
, null);
514 Instance
.syserr(new IOException(trans(
515 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
520 BasicSupport support
= BasicSupport
.getSupport(source
);
522 if (support
!= null) {
523 Progress pgIn
= new Progress();
524 Progress pgOut
= new Progress();
527 pg
.addProgress(pgIn
, 1);
528 pg
.addProgress(pgOut
, 1);
531 Story story
= support
.process(source
, pgIn
);
533 target
= new File(target
).getAbsolutePath();
534 BasicOutput
.getOutput(type
, infoCover
).process(
535 story
, target
, pgOut
);
536 } catch (IOException e
) {
537 Instance
.syserr(new IOException(trans(
538 StringId
.ERR_SAVING
, target
), e
));
542 Instance
.syserr(new IOException(trans(
543 StringId
.ERR_NOT_SUPPORTED
, source
)));
547 } catch (IOException e
) {
548 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
553 } catch (MalformedURLException e
) {
554 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
563 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
566 * the ID to translate
568 * @return the translated result
570 private static String
trans(StringId id
, Object
... params
) {
571 return Instance
.getTrans().getString(id
, params
);
575 * Display the correct syntax of the program to the user to stdout, or an
576 * error message if the syntax used was wrong on stderr.
579 * TRUE to show the syntax help, FALSE to show "syntax error"
581 private static void syntax(boolean showHelp
) {
583 StringBuilder builder
= new StringBuilder();
584 for (SupportType type
: SupportType
.values()) {
585 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
587 builder
.append('\n');
590 String typesIn
= builder
.toString();
591 builder
.setLength(0);
593 for (OutputType type
: OutputType
.values()) {
594 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
595 type
.getDesc(true)));
596 builder
.append('\n');
599 String typesOut
= builder
.toString();
601 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
603 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
608 * Set the default reader type for this session only (it can be changed in
609 * the configuration file, too, but this value will override it).
611 * @param readerTypeString
614 private static int setReaderType(String readerTypeString
) {
616 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
618 BasicReader
.setDefaultReaderType(readerType
);
620 } catch (IllegalArgumentException e
) {
621 Instance
.syserr(new IOException("Unknown reader type: "
622 + readerTypeString
, e
));