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 [key] [port]: start a server on this port</li>
66 * <li>--stop-server [key] [port]: stop the running server on this port if
68 * <li>--remote [key] [host] [port]: use a the given remote library</li>
72 * see method description
74 public static void main(String
[] args
) {
75 String urlString
= null;
77 String sourceString
= null;
78 String chapString
= null;
81 MainAction action
= MainAction
.START
;
82 Boolean plusInfo
= null;
86 boolean noMoreActions
= false;
89 for (int i
= 0; exitCode
== 0 && i
< args
.length
; i
++) {
90 // Action (--) handling:
91 if (!noMoreActions
&& args
[i
].startsWith("--")) {
92 if (args
[i
].equals("--")) {
96 action
= MainAction
.valueOf(args
[i
].substring(2)
97 .toUpperCase().replace("-", "_"));
98 } catch (Exception e
) {
99 Instance
.syserr(new IllegalArgumentException(
100 "Unknown action: " + args
[i
], e
));
110 if (urlString
== null) {
119 } else if (sourceString
== null) {
120 sourceString
= args
[i
];
121 } else if (target
== null) {
128 if (urlString
== null) {
130 } else if (sourceString
== null) {
131 sourceString
= args
[i
];
132 } else if (target
== null) {
134 } else if (plusInfo
== null) {
135 if ("+info".equals(args
[i
])) {
145 if (sourceString
== null) {
146 sourceString
= args
[i
];
154 } else if (chapString
== null) {
155 chapString
= args
[i
];
161 if (urlString
== null) {
163 } else if (chapString
== null) {
164 chapString
= args
[i
];
173 exitCode
= setReaderType(args
[i
]);
174 action
= MainAction
.START
;
177 exitCode
= 255; // not supposed to be selected by user
180 exitCode
= 255; // no arguments for this option
186 } else if (port
== null) {
187 port
= Integer
.parseInt(args
[i
]);
195 } else if (host
== null) {
197 } else if (port
== null) {
198 port
= Integer
.parseInt(args
[i
]);
200 File remoteCacheDir
= Instance
.getRemoteDir(host
);
201 BasicLibrary lib
= new RemoteLibrary(key
, host
, port
);
202 lib
= new CacheLibrary(remoteCacheDir
, lib
);
204 BasicReader
.setDefaultLibrary(lib
);
206 action
= MainAction
.START
;
214 final Progress mainProgress
= new Progress(0, 80);
215 mainProgress
.addProgressListener(new Progress
.ProgressListener() {
216 private int current
= mainProgress
.getMin();
219 public void progress(Progress progress
, String name
) {
220 int diff
= progress
.getProgress() - current
;
223 StringBuilder builder
= new StringBuilder();
224 for (int i
= 0; i
< diff
; i
++) {
228 System
.err
.print(builder
.toString());
230 if (progress
.isDone()) {
231 System
.err
.println("");
235 Progress pg
= new Progress();
236 mainProgress
.addProgress(pg
, mainProgress
.getMax());
238 VersionCheck updates
= VersionCheck
.check();
239 if (updates
.isNewVersionAvailable()) {
240 // Sent to syserr so not to cause problem if one tries to capture a
241 // story content in text mode
243 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
244 System
.err
.println("");
245 for (Version v
: updates
.getNewer()) {
246 System
.err
.println("\tVersion " + v
);
247 System
.err
.println("\t-------------");
248 System
.err
.println("");
249 for (String item
: updates
.getChanges().get(v
)) {
250 System
.err
.println("\t- " + item
);
252 System
.err
.println("");
256 if (exitCode
!= 255) {
259 exitCode
= imprt(urlString
, pg
);
260 updates
.ok(); // we consider it read
263 exitCode
= export(luid
, sourceString
, target
, pg
);
264 updates
.ok(); // we consider it read
267 exitCode
= convert(urlString
, sourceString
, target
,
268 plusInfo
== null ?
false : plusInfo
, pg
);
269 updates
.ok(); // we consider it read
272 if (BasicReader
.getReader() == null) {
273 Instance
.syserr(new Exception(
274 "No reader type has been configured"));
278 exitCode
= list(sourceString
);
281 if (BasicReader
.getReader() == null) {
282 Instance
.syserr(new Exception(
283 "No reader type has been configured"));
287 exitCode
= read(luid
, chapString
, true);
290 if (BasicReader
.getReader() == null) {
291 Instance
.syserr(new Exception(
292 "No reader type has been configured"));
296 exitCode
= read(urlString
, chapString
, false);
307 .println(String
.format("Fanfix version %s"
308 + "\nhttps://github.com/nikiroo/fanfix/"
309 + "\n\tWritten by Nikiroo",
310 Version
.getCurrentVersion()));
311 updates
.ok(); // we consider it read
314 if (BasicReader
.getReader() == null) {
315 Instance
.syserr(new Exception(
316 "No reader type has been configured"));
320 BasicReader
.getReader().browse(null);
328 Server server
= new RemoteLibraryServer(key
, port
);
330 System
.out
.println("Remote server started on: " + port
);
331 } catch (IOException e
) {
342 final String fkey
= key
;
343 new ConnectActionClient(host
, port
, true) {
345 public void action(Version serverVersion
)
348 send(new Object
[] { fkey
, "EXIT" });
349 } catch (Exception e
) {
354 } catch (IOException e
) {
359 exitCode
= 255; // should not be reachable (REMOTE -> START)
364 if (exitCode
== 255) {
369 System
.exit(exitCode
);
374 * Import the given resource into the {@link LocalLibrary}.
377 * the resource to import
379 * the optional progress reporter
381 * @return the exit return code (0 = success)
383 public static int imprt(String urlString
, Progress pg
) {
385 Story story
= Instance
.getLibrary().imprt(
386 BasicReader
.getUrl(urlString
), pg
);
387 System
.out
.println(story
.getMeta().getLuid() + ": \""
388 + story
.getMeta().getTitle() + "\" imported.");
389 } catch (IOException e
) {
398 * Export the {@link Story} from the {@link LocalLibrary} to the given
404 * the {@link OutputType} to use
408 * the optional progress reporter
410 * @return the exit return code (0 = success)
412 public static int export(String luid
, String typeString
, String target
,
414 OutputType type
= OutputType
.valueOfNullOkUC(typeString
, null);
416 Instance
.syserr(new Exception(trans(StringId
.OUTPUT_DESC
,
422 Instance
.getLibrary().export(luid
, type
, target
, pg
);
423 } catch (IOException e
) {
432 * List the stories of the given source from the {@link LocalLibrary}
433 * (unless NULL is passed, in which case all stories will be listed).
436 * the source to list the known stories of, or NULL to list all
439 * @return the exit return code (0 = success)
441 private static int list(String source
) {
442 BasicReader
.getReader().browse(source
);
447 * Start the CLI reader for this {@link Story}.
450 * the LUID of the {@link Story} in the {@link LocalLibrary}
451 * <b>or</b> the {@link Story} {@link URL}
453 * which {@link Chapter} to read (starting at 1), or NULL to get
454 * the {@link Story} description
456 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
459 * @return the exit return code (0 = success)
461 private static int read(String story
, String chapString
, boolean library
) {
463 Reader reader
= BasicReader
.getReader();
465 reader
.setMeta(story
);
467 reader
.setMeta(BasicReader
.getUrl(story
), null);
470 if (chapString
!= null) {
472 reader
.setChapter(Integer
.parseInt(chapString
));
474 } catch (NumberFormatException e
) {
475 Instance
.syserr(new IOException(
476 "Chapter number cannot be parsed: " + chapString
, e
));
482 } catch (IOException e
) {
491 * Convert the {@link Story} into another format.
494 * the source {@link Story} to convert
496 * the {@link OutputType} to convert to
500 * TRUE to also export the cover and info file, even if the given
501 * {@link OutputType} does not usually save them
503 * the optional progress reporter
505 * @return the exit return code (0 = success)
507 private static int convert(String urlString
, String typeString
,
508 String target
, boolean infoCover
, Progress pg
) {
511 String sourceName
= urlString
;
513 URL source
= BasicReader
.getUrl(urlString
);
514 sourceName
= source
.toString();
515 if (source
.toString().startsWith("file://")) {
516 sourceName
= sourceName
.substring("file://".length());
519 OutputType type
= OutputType
.valueOfAllOkUC(typeString
, null);
521 Instance
.syserr(new IOException(trans(
522 StringId
.ERR_BAD_OUTPUT_TYPE
, typeString
)));
527 BasicSupport support
= BasicSupport
.getSupport(source
);
529 if (support
!= null) {
530 Progress pgIn
= new Progress();
531 Progress pgOut
= new Progress();
534 pg
.addProgress(pgIn
, 1);
535 pg
.addProgress(pgOut
, 1);
538 Story story
= support
.process(source
, pgIn
);
540 target
= new File(target
).getAbsolutePath();
541 BasicOutput
.getOutput(type
, infoCover
).process(
542 story
, target
, pgOut
);
543 } catch (IOException e
) {
544 Instance
.syserr(new IOException(trans(
545 StringId
.ERR_SAVING
, target
), e
));
549 Instance
.syserr(new IOException(trans(
550 StringId
.ERR_NOT_SUPPORTED
, source
)));
554 } catch (IOException e
) {
555 Instance
.syserr(new IOException(trans(StringId
.ERR_LOADING
,
560 } catch (MalformedURLException e
) {
561 Instance
.syserr(new IOException(trans(StringId
.ERR_BAD_URL
,
570 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
573 * the ID to translate
575 * @return the translated result
577 private static String
trans(StringId id
, Object
... params
) {
578 return Instance
.getTrans().getString(id
, params
);
582 * Display the correct syntax of the program to the user to stdout, or an
583 * error message if the syntax used was wrong on stderr.
586 * TRUE to show the syntax help, FALSE to show "syntax error"
588 private static void syntax(boolean showHelp
) {
590 StringBuilder builder
= new StringBuilder();
591 for (SupportType type
: SupportType
.values()) {
592 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
594 builder
.append('\n');
597 String typesIn
= builder
.toString();
598 builder
.setLength(0);
600 for (OutputType type
: OutputType
.values()) {
601 builder
.append(trans(StringId
.ERR_SYNTAX_TYPE
, type
.toString(),
602 type
.getDesc(true)));
603 builder
.append('\n');
606 String typesOut
= builder
.toString();
608 System
.out
.println(trans(StringId
.HELP_SYNTAX
, typesIn
, typesOut
));
610 System
.err
.println(trans(StringId
.ERR_SYNTAX
));
615 * Set the default reader type for this session only (it can be changed in
616 * the configuration file, too, but this value will override it).
618 * @param readerTypeString
621 private static int setReaderType(String readerTypeString
) {
623 ReaderType readerType
= ReaderType
.valueOf(readerTypeString
625 BasicReader
.setDefaultReaderType(readerType
);
627 } catch (IllegalArgumentException e
) {
628 Instance
.syserr(new IOException("Unknown reader type: "
629 + readerTypeString
, e
));