+static enum outmodekind o_outmode_kind;
+static const char *o_outmode_str= 0;
+
+static enum mode o_mode= mode_all;
+static char *o_screenshot_fn;
+static const char *o_serv_pctb, *o_serv_dict_fetch, *o_serv_dict_submit;
+
+const char *o_resolver= "./dictionary-manager";
+FILE *screenshot_file;
+const char *o_ocean, *o_pirate;
+int o_quiet;
+
+enum flags o_flags= ff_dict_fetch|ff_dict_submit|ff_dict_pirate;
+
+static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
+static void vbadusage(const char *fmt, va_list al) {
+ fputs("bad usage: ",stderr);
+ vfprintf(stderr,fmt,al);
+ fputc('\n',stderr);
+ exit(12);
+}
+DEFINE_VWRAPPERF(static, badusage, NORET);
+
+static void open_screenshot_file(const char *mode) {
+ screenshot_file= fopen(o_screenshot_fn, mode);
+ if (!screenshot_file)
+ fatal("could not open screenshots file `%s': %s",
+ o_screenshot_fn, strerror(errno));
+}
+
+static void run_analysis(void) {
+ FILE *tf;
+
+ sysassert( tf= tmpfile() );
+ progress("running recognition...");
+ analyse(tf);
+
+ if (o_flags & ff_upload) {
+ if (npages<=1)
+ fatal("Recognition successful, but refusing to upload partial data\n"
+ " (--single-page specified). Specify an output mode?");
+ }
+
+ sysassert( fseek(tf,0,SEEK_SET) == 0);
+
+ progress_log("processing results (--%s)...", o_outmode_str);
+ pid_t processor;
+ sysassert( (processor= fork()) != -1 );
+
+ if (!processor) {
+ sysassert( dup2(fileno(tf),0) ==0 );
+ EXECLP_HELPER("commod-results-processor", o_outmode_str, (char*)0);
+ }
+
+ waitpid_check_exitstatus(processor, "output processor/uploader");
+ fclose(tf);
+ progress_log("all complete.");
+}
+
+void fetch_with_rsync(const char *stem) {
+ pid_t fetcher;
+
+ sysassert( (fetcher= fork()) != -1 );
+ if (!fetcher) {
+ const char *rsync= getenv("YPPSC_PCTB_RSYNC");
+ if (!rsync) rsync= "rsync";
+
+ const char *src= getenv("YPPSC_PCTB_DICT_UPDATE");
+ char *remote= masprintf("%s/master-%s.txt", src, stem);
+ char *local= masprintf("#master-%s#.txt", stem);
+ if (DEBUGP(rsync))
+ fprintf(stderr,"executing rsync to fetch %s to %s\n",remote,local);
+ execlp(rsync, "rsync",
+ DEBUGP(rsync) ? "-zvLt" : "-zLt",
+ "--",remote,local,(char*)0);
+ sysassert(!"exec rsync failed");
+ }
+
+ waitpid_check_exitstatus(fetcher, "rsync");
+}
+
+static void set_server(const char *envname, const char *defprotocol,
+ const char *defvalue, const char *defvalue_test,
+ const char *userspecified,
+ int enable) {
+ const char *value;
+
+ if (!enable) { value= "0"; goto ok; }
+
+ if (userspecified)
+ value= userspecified;
+ else if ((value= getenv(envname)))
+ ;
+ else if (o_flags & ff_testservers)
+ value= defvalue_test;
+ else
+ value= defvalue;
+
+ if (value[0]=='/' || (value[0]=='.' && value[1]=='/'))
+ /* absolute or relative pathname - or anyway, something with no hostname */
+ goto ok;
+
+ const char *colon= strchr(value, ':');
+ const char *slash= strchr(value, '/');
+
+ if (colon && (!slash || colon < slash))
+ /* colon before the first slash, if any */
+ /* rsync :: protocol specification - anyway, adding scheme:// won't help */
+ goto ok;
+
+ int vallen= strlen(value);
+
+ value= masprintf("%s%s%s", defprotocol, value,
+ vallen && value[vallen-1]=='/' ? "" : "/");
+
+ ok:
+ sysassert(! setenv(envname,value,1) );
+}
+
+int main(int argc, char **argv) {
+ const char *arg;
+
+ sysassert( setlocale(LC_MESSAGES,"") );
+ sysassert( setlocale(LC_CTYPE,"en_GB.UTF-8") ||
+ setlocale(LC_CTYPE,"en.UTF-8") );
+
+#define ARGVAL ((*++argv) ? *argv : \
+ (badusage("missing value for option %s",arg),(char*)0))
+
+#define IS(s) (!strcmp(arg,(s)))
+
+ while ((arg=*++argv)) {
+ if (IS("--find-window-only")) o_mode= mode_findwindow;
+ else if (IS("--screenshot-only")) o_mode= mode_screenshot;
+ else if (IS("--show-charset")) o_mode= mode_showcharset;
+ else if (IS("--analyse-only") ||
+ IS("--same")) o_mode= mode_analyse;
+ else if (IS("--everything")) o_mode= mode_all;
+ else if (IS("--find-island")) o_flags |= ffs_printisland;
+ else if (IS("--single-page")) o_flags |= ff_singlepage;
+ else if (IS("--quiet")) o_quiet= 1;
+ else if (IS("--edit-charset")) o_flags |= ff_editcharset;
+ else if (IS("--test-servers")) o_flags |= ff_testservers;
+ else if (IS("--dict-local-only")) o_flags &= ~ffs_dict;
+ else if (IS("--dict-read-only")) o_flags &= (~ffs_dict | ff_dict_fetch);
+ else if (IS("--dict-anon")) o_flags &= ~ff_dict_pirate;
+ else if (IS("--dict-submit")) o_flags |= ff_dict_fetch|ff_dict_submit;
+ else if (IS("--raw-tsv")) o_outmode_kind= omk_raw;
+ else if (IS("--upload")) o_outmode_kind= omk_upload;
+ else if (IS("--arbitrage") ||
+ IS("--tsv") ||
+ IS("--best-prices")) o_outmode_kind=omk_str,
+ o_outmode_str=arg+2;
+
+ else if (IS("--screenshot-file")) o_screenshot_fn= ARGVAL;
+ else if (IS("--pctb-server")) o_serv_pctb= ARGVAL;
+ else if (IS("--dict-submit-server")) o_serv_dict_submit= ARGVAL;
+ else if (IS("--dict-update-server")) o_serv_dict_fetch= ARGVAL;
+ else if (IS("--ocean")) o_ocean= ARGVAL;
+ else if (IS("--pirate")) o_pirate= ARGVAL;
+#define DF(f) \
+ else if (IS("-D" #f)) \
+ debug_flags |= dbg_##f;
+ DEBUG_FLAG_LIST
+#undef DF
+ else if (IS("--window-id")) {
+ char *ep;
+ unsigned long windowid= strtoul(ARGVAL,&ep,0);
+ if (*ep) badusage("invalid window id");
+ set_yppclient_window(windowid);
+ } else
+ badusage("unknown option `%s'",arg);
+ }