+ final Progress mainProgress = new Progress(0, 80);
+ mainProgress.addProgressListener(new Progress.ProgressListener() {
+ private int current = mainProgress.getMin();
+
+ @Override
+ public void progress(Progress progress, String name) {
+ int diff = progress.getProgress() - current;
+ current += diff;
+
+ if (diff <= 0)
+ return;
+
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < diff; i++) {
+ builder.append('.');
+ }
+
+ System.err.print(builder.toString());
+
+ if (progress.isDone()) {
+ System.err.println("");
+ }
+ }
+ });
+ Progress pg = new Progress();
+ mainProgress.addProgress(pg, mainProgress.getMax());
+
+ VersionCheck updates = checkUpdates();
+
+ if (exitCode == 0) {
+ switch (action) {
+ case IMPORT:
+ if (updates != null) {
+ // we consider it read
+ Instance.getInstance().setVersionChecked();
+ }
+
+ try {
+ exitCode = imprt(BasicReader.getUrl(urlString), pg);
+ } catch (MalformedURLException e) {
+ Instance.getInstance().getTraceHandler().error(e);
+ exitCode = 1;
+ }
+
+ break;
+ case EXPORT:
+ if (updates != null) {
+ // we consider it read
+ Instance.getInstance().setVersionChecked();
+ }
+
+ OutputType exportType = OutputType.valueOfNullOkUC(sourceString, null);
+ if (exportType == null) {
+ Instance.getInstance().getTraceHandler().error(new Exception(trans(StringId.OUTPUT_DESC, sourceString)));
+ exitCode = 1;
+ break;
+ }
+
+ exitCode = export(luid, exportType, target, pg);
+
+ break;
+ case CONVERT:
+ if (updates != null) {
+ // we consider it read
+ Instance.getInstance().setVersionChecked();
+ }
+
+ OutputType convertType = OutputType.valueOfAllOkUC(sourceString, null);
+ if (convertType == null) {
+ Instance.getInstance().getTraceHandler()
+ .error(new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE, sourceString)));
+
+ exitCode = 2;
+ break;
+ }
+
+ exitCode = convert(urlString, convertType, target,
+ plusInfo == null ? false : plusInfo, pg);
+
+ break;
+ case LIST:
+ exitCode = list(sourceString);
+ break;
+ case SET_SOURCE:
+ try {
+ Instance.getInstance().getLibrary().changeSource(luid, sourceString, pg);
+ } catch (IOException e1) {
+ Instance.getInstance().getTraceHandler().error(e1);
+ exitCode = 21;
+ }
+ break;
+ case SET_TITLE:
+ try {
+ Instance.getInstance().getLibrary().changeTitle(luid, titleString, pg);
+ } catch (IOException e1) {
+ Instance.getInstance().getTraceHandler().error(e1);
+ exitCode = 22;
+ }
+ break;
+ case SET_AUTHOR:
+ try {
+ Instance.getInstance().getLibrary().changeAuthor(luid, authorString, pg);
+ } catch (IOException e1) {
+ Instance.getInstance().getTraceHandler().error(e1);
+ exitCode = 23;
+ }
+ break;
+ case READ:
+ if (luid == null || luid.isEmpty()) {
+ syntax(false);
+ exitCode = 255;
+ break;
+ }
+
+ try {
+ Integer chap = null;
+ if (chapString != null) {
+ try {
+ chap = Integer.parseInt(chapString);
+ } catch (NumberFormatException e) {
+ Instance.getInstance().getTraceHandler().error(new IOException(
+ "Chapter number cannot be parsed: " + chapString, e));
+ exitCode = 2;
+ break;
+ }
+ }
+
+ BasicLibrary lib = Instance.getInstance().getLibrary();
+ exitCode = read(lib.getStory(luid, null), chap);
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler()
+ .error(new IOException("Failed to read book", e));
+ exitCode = 2;
+ }
+
+ break;
+ case READ_URL:
+ if (urlString == null || urlString.isEmpty()) {
+ syntax(false);
+ exitCode = 255;
+ break;
+ }
+
+ try {
+ Integer chap = null;
+ if (chapString != null) {
+ try {
+ chap = Integer.parseInt(chapString);
+ } catch (NumberFormatException e) {
+ Instance.getInstance().getTraceHandler().error(new IOException(
+ "Chapter number cannot be parsed: " + chapString, e));
+ exitCode = 2;
+ break;
+ }
+ }
+
+ BasicSupport support = BasicSupport
+ .getSupport(BasicReader.getUrl(urlString));
+ if (support == null) {
+ Instance.getInstance().getTraceHandler()
+ .error("URL not supported: " + urlString);
+ exitCode = 2;
+ break;
+ }
+
+ exitCode = read(support.process(null), chap);
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler()
+ .error(new IOException("Failed to read book", e));
+ exitCode = 2;
+ }
+
+ break;
+ case SEARCH:
+ page = page == null ? 1 : page;
+ if (page < 0) {
+ Instance.getInstance().getTraceHandler().error("Incorrect page number");
+ exitCode = 255;
+ break;
+ }
+
+ item = item == null ? 0 : item;
+ if (item < 0) {
+ Instance.getInstance().getTraceHandler().error("Incorrect item number");
+ exitCode = 255;
+ break;
+ }
+
+ if (searchOn == null) {
+ try {
+ search();
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler().error(e);
+ exitCode = 1;
+ }
+ } else if (search != null) {
+ try {
+ searchKeywords(searchOn, search, page, item);
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler().error(e);
+ exitCode = 20;
+ }
+ } else {
+ exitCode = 255;
+ }
+
+ break;
+ case SEARCH_TAG:
+ if (searchOn == null) {
+ exitCode = 255;
+ break;
+ }
+
+ page = page == null ? 1 : page;
+ if (page < 0) {
+ Instance.getInstance().getTraceHandler().error("Incorrect page number");
+ exitCode = 255;
+ break;
+ }
+
+ item = item == null ? 0 : item;
+ if (item < 0) {
+ Instance.getInstance().getTraceHandler().error("Incorrect item number");
+ exitCode = 255;
+ break;
+ }
+
+ try {
+ searchTags(searchOn, page, item,
+ tags.toArray(new Integer[] {}));
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler().error(e);
+ }
+
+ break;
+ case HELP:
+ syntax(true);
+ exitCode = 0;
+ break;
+ case VERSION:
+ if (updates != null) {
+ // we consider it read
+ Instance.getInstance().setVersionChecked();
+ }
+
+ System.out
+ .println(String.format("Fanfix version %s"
+ + "%nhttps://github.com/nikiroo/fanfix/"
+ + "%n\tWritten by Nikiroo",
+ Version.getCurrentVersion()));
+ break;
+ case START:
+ try {
+ start();
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler().error(e);
+ exitCode = 66;
+ }
+ break;
+ case SERVER:
+ try {
+ startServer();
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler().error(e);
+ }
+
+ break;
+ case STOP_SERVER:
+ // Can be given via "--remote XX XX XX"
+ if (key == null)
+ key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY);
+ if (port == null)
+ port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT);
+
+ if (port == null) {
+ System.err.println("No port given nor configured in the config file");
+ exitCode = 15;
+ break;
+ }
+ try {
+ stopServer(key, host, port);
+ } catch (SSLException e) {
+ Instance.getInstance().getTraceHandler().error(
+ "Bad access key for remote library");
+ exitCode = 43;
+ } catch (IOException e) {
+ Instance.getInstance().getTraceHandler().error(e);
+ exitCode = 44;
+ }
+
+ break;
+ case REMOTE:
+ exitCode = 255; // should not be reachable (REMOTE -> START)
+ break;
+ }