/*
-h	help
-v	version
-c	configure permanent options
-C	show permanent options
-r	skip permanent options for program invokation
-u	unconfigure (forget) permanent options

-t ft	input file type (png, jpeg, netpbm)
-l name	output file type, configured in .bmeps (eps/pdf)
-f n-m	frames (i.e. from tiff)
-o opt  options
-m      make style
*/
#define B2MAIN_C	1
#include "bmepsi.h"

#line 17 "b2main.ctr"



#ifndef VERSNUMB
#define VERSNUMB "2.0.3"
#endif


static char *long_options[] = {
  /*   0 */ "h$elp",
  /*   1 */ "v$ersion",
  /*   2 */ "conf$igure",
  /*   3 */ "unconf$igure",
  /*   4 */ "res$et",
  /*   5 */ "sh$ow-configuration",
  /*   6 */ "sil$ently",
  /*   7 */ "lang$uage",
  /*   8 */ "opt$ion",
  /*   9 */ "f$rames",
  /*  10 */ "t$ype",
  NULL
};

static char str_ps2[] = { "ps2" };
static char str_on[] = { "on" };
static char str_off[] = { "off" };
static char pk_lang[] = { "/language" };
static char pk_make[] = { "/make" };



static char empty_string[] = { "" };

/*
	Check whether or not to run silently.
	Set *rs (run silently) and *rf (run as filter).
*/
static
void
silence_check DK_P4(int,argc, char **,argv, int *,rs, int *,rf)
{
  int i; char *ptr, **lfdptr;
  int myrf = 1, myrs = 0;
  lfdptr = argv; lfdptr++; i = 1;
  
  while(i < argc) {
    ptr = *lfdptr;
    if(*ptr == '-') {
      ptr++;
      if(*ptr == '-') {
        ptr++;
	switch(dkstr_array_abbr(long_options, ptr, '$', 0)) {
	  case 6: myrs = 1; break;
	}
      } else {
        switch(*ptr) {
	  case 't': case 'l': case 'f': case 'o':
	  {
	    ptr++;
	    if(!(*ptr)) {
	      lfdptr++; i++;
	    }
	  } break;
	  case 's': {
	    myrs = 1;
	  } break;
	}
      }
    } else {
      myrf = 0;
    }
    lfdptr++; i++;
  }
  if(rs) { *rs = myrs; }
  if(rf) { *rf = myrf; }
  
}


static
void
apply_arguments DK_P2(BJ *,bj, char *,dl)
{
  char *ptr, **myargv, **lfdptr, *vptr, *ovptr;
  int have_configured = 0, myargc, i;
  
  myargc = dkapp_get_argc(bj->a);
  myargv = dkapp_get_argv(bj->a);
  lfdptr = myargv; lfdptr++; i = 1;
  while(i < myargc) {
    ptr = *lfdptr;
    if(*ptr == '-') {
      ptr++;
      switch(*ptr) {
        case '-': {
	  ptr++;
	  vptr = dkstr_chr(ptr, '=');
	  ovptr = vptr;
	  if(vptr) { *(vptr++) = '\0'; }
	  switch(dkstr_array_abbr(long_options, ptr, '$', 0)) {
	    case 0: {	/* help */
	      bj->cmd |= BMEPS_CMD_HELP;
	    } break;
	    case 1: {	/* version */
	      bj->cmd |= BMEPS_CMD_VERSION;
	    } break;
	    case 2: {	/* configure */
	      bj->cmd |= BMEPS_CMD_CONFIGURE;
	    } break;
	    case 3: {	/* unconfigure */
	      bj->cmd |= BMEPS_CMD_UNCONFIGURE;
	    } break;
	    case 4: {	/* reset */
	      bj->mm = 0x00;
	      bmeps_bo_get_configuration(bj, NULL);
	    } break;
	    case 5: {	/* show-configuration */
	      bj->cmd |= BMEPS_CMD_SHOWCONF;
	    } break;
	    case 6: {	/* silently */
	    } break;
	    case 7: {	/* language */
	      if(vptr) {
	        if(!have_configured) {
		  bj->lang = vptr;
		  have_configured = 1;
		  bmeps_bo_get_configuration(bj, vptr);
		} else {
		  bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 49);
		  bj->exval = 1;
		}
	      }
	    } break;
	    case 8: {	/* option */
	      if(vptr) {
	        if(!have_configured) {
		  have_configured = 1;
		  bmeps_bo_get_configuration(bj, NULL);
		}
		bmeps_bo_add_line(bj, bj->bo, vptr);
	      }
	    } break;
	    case 9: {	/* frames */
	      if(vptr) {
	        long l; int ok = 0;
	        char *ptr1, *ptr2;
	        ptr1 = dkstr_start(vptr, NULL);
	        if(ptr1) {
	          ptr2 = dkstr_chr(ptr1, '-');
	          if(ptr2) {
	            *ptr2 = '\0';
	          }
	          if(sscanf(ptr1, "%ld", &l) == 1) {
	            if(l > 0L) {
	              bj->frame_s = bj->frame_e = l - 1L;
	              ok = 1;
		      bj->frame_c = 0x01;
		      
	            }
	          }
	          if(ptr2) {
	            *(ptr2++) = '-';
	            if(ok) {
	              if(sscanf(ptr2, "%ld", &l) == 1) {
		        if(l > 0L) {
		          if(l > bj->frame_s) {
		            bj->frame_e = l - 1L;
		            
		          }
		        }
		      }
	            }
	            ptr2++;
	          }
	        }
	      }
	    } break;
	    case 10: {	/* type */
	      if(vptr) {
	        bj->it = dkbif_get_type(vptr);
	      }
	    } break;
	  }
	  if(ovptr) { *ovptr = '='; }
	} break;
	case 'a': {
	  bj->af = 0x01;
	} break;
        case 'h': {
	  bj->cmd |= BMEPS_CMD_HELP;
	} break;
        case 'v': {
	  bj->cmd |= BMEPS_CMD_VERSION;
	} break;
        case 'c': {
	  bj->cmd |= BMEPS_CMD_CONFIGURE;
	} break;
        case 'u': {
	  bj->cmd |= BMEPS_CMD_UNCONFIGURE;
	} break;
        case 'r': {
	  bj->mm = 0x00;
	  bmeps_bo_get_configuration(bj, NULL);
	} break;
        case 'C': {
	  bj->cmd |= BMEPS_CMD_SHOWCONF;
	} break;
        case 'l': {
	  ptr++;
	  if(!(*ptr)) {
	    lfdptr++; i++;
	    if(i < myargc) {
	      ptr = *lfdptr;
	    }
	  }
	  if(ptr) {
	    if(!have_configured) {
	      bj->lang = ptr;
	      have_configured = 1;
	      bmeps_bo_get_configuration(bj, ptr);
	    } else {
	      bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 49);
	      bj->exval = 1;
	    }
	  }
	} break;
        case 'o': {
	  ptr++;
	  if(!(*ptr)) {
	    lfdptr++; i++;
	    if(i < myargc) {
	      ptr = *lfdptr;
	    }
	  }
	  if(ptr) {
	    if(!have_configured) {
	      have_configured = 1;
	      bmeps_bo_get_configuration(bj, NULL);
	    }
	    bmeps_bo_add_line(bj, bj->bo, ptr);
	  }
	} break;
	case 'f': {
	  ptr++;
	  if(!(*ptr)) {
	    lfdptr++; i++;
	    if(i < myargc) {
	      ptr = *lfdptr;
	    }
	  }
	  if(ptr) {
	    long l; int ok = 0;
	    char *ptr1, *ptr2;
	    ptr1 = dkstr_start(ptr, NULL);
	    if(ptr1) {
	      ptr2 = dkstr_chr(ptr1, '-');
	      if(ptr2) {
	        *ptr2 = '\0';
	      }
	      if(sscanf(ptr1, "%ld", &l) == 1) {
	        if(l > 0L) {
	          bj->frame_s = bj->frame_e = l - 1L;
	          ok = 1;
		  bj->frame_c = 0x01;
		  
	        }
	      }
	      if(ptr2) {
	        *(ptr2++) = '-';
	        if(ok) {
	          if(sscanf(ptr2, "%ld", &l) == 1) {
		    if(l > 0L) {
		      if(l > bj->frame_s) {
		        bj->frame_e = l - 1L;
		        
		      }
		    }
		  }
	        }
	        ptr2++;
	      }
	    }
	  }
	} break;
        case 'm': {
	  ptr++;
	  if(*ptr == '-') {
	    bj->mm = 0x0;
	  } else {
	    bj->mm = 0x1;
	  }
	} break;
        case 's': {
	} break;
	case 't': {
	  ptr++;
	  if(!(*ptr)) {
	    lfdptr++; i++;
	    if(i < myargc) {
	      ptr = *lfdptr;
	    }
	  }
	  if(ptr) {	
	    bj->it = dkbif_get_type(ptr);
	    
	  }
	} break;
	default: {
	  bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 50, 51, ptr);
	  bj->exval = 1;
	} break;
      }
    } else {
      if(bj->o1) {
	bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 52);
	bj->exval = 1;
      } else {
        if(bj->i1) { bj->o1 = ptr; }
	else { bj->i1 = ptr; }
      }
    }
    lfdptr++; i++;
  }
  if(!have_configured) {
    bmeps_bo_get_configuration(bj, NULL);
    bj->lang = dl;
  }
  
}


static char *help_text[] = {
"Usage:",
"------",
"",
"bmeps [-l <language>] [ <inputfile> [ <outputfile> ] ] [<options>]",
"",
"Options:",
"<language>          Output language",
"-o <option-setting>    Configuration setting",
"-m                     Make style",
"-f <frame-range>       Start and end frame",
"-t <file-type>         Input file type when processing standard input",
NULL
};

static char help_file_name[] = { "bmeps.txt" };

static char *output_type_names[] = {
  "EPS", "PDF", "BB (bounding box)"
};
static char *eps_levels[] = { "1", "2", "3" };
static char *pdf_levels[] = { "1.2", "1.3", "1.4" };

static char *version_text[] = {
"bmeps, version " VERSNUMB " (SCCS=1.10)",
"Copyright (C) 2006 Dipl.-Ing. D. Krause",
"http://bmeps.sourceforge.net/",
"Supported input file types: PNG"
#if DK_HAVE_JPEGLIB_H
" JPEG"
#endif
#if DK_HAVE_PNM_H
" NetPBM"
#endif
#if DK_HAVE_TIFF_H
" TIFF"
#endif
"",
"",
"Redistribution and use in source and binary forms, with or without",
"modification, are permitted provided that the following conditions are met:",
"* Redistributions of source code must retain the above copyright notice, this",
"  list of conditions and the following disclaimer.",
"* Redistributions in binary form must reproduce the above copyright notice,",
"  this list of conditions and the following disclaimer in the documentation",
"  and/or other materials provided with the distribution.",
"* Neither the name of the Dirk Krause nor the names of other contributors may",
"  be used to endorse or promote products derived from this software without",
"  specific prior written permission.",
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"",
"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE",
"IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE",
"ARE DISCLAIMED.",
"IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY",
"DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES",
"(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;",
"LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND",
"ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT",
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS",
"SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.",
NULL
};

static
void
show_version DK_P1(BJ *,bj)
{
  char **xptr;
  
  xptr = version_text;
  while(*xptr) {
    fputs(*(xptr++), stdout); fputc('\n', stdout);
  }
  fputc('\n', stdout);
  xptr = dklic_get();
  fputs("Libraries used:\n\n", stdout);
#if DK_HAVE_PNG_H
  fputs("libpng       The official PNG reference library\n", stdout);
  fputs("             http://www.libpng.org/pub/png/libpng.html\n", stdout);
#endif
#if DK_HAVE_JPEGLIB_H
  fputs("jpegsrc      The Independent JPEG Group's free JPEG software\n", stdout);
  fputs("             http://www.ijg.org/\n", stdout);
#endif
#if DK_HAVE_PNM_H
  fputs("netpbm       The image conversion toolkit\n", stdout);
  fputs("             http://netpbm.sourceforge.net/\n", stdout);
#endif
#if DK_HAVE_TIFF_H
  fputs("libtiff      The TIFF library.\n", stdout);
  fputs("             http://www.remotesensing.org/libtiff/\n", stdout);
#endif
  while(*xptr) {
    fputs(*(xptr++), stdout); fputc('\n', stdout);
  }
  fputc('\n', stdout);
  
}



static
void
show_help DK_P1(BJ *,bj)
{
  
  dkapp_help(bj->a, help_file_name, help_text);
}



static
size_t
max_str_length DK_P3(BJ *,bj, size_t, s1, size_t,s2)
{
  size_t back = 0, i = 0, sz = 0;
  for(i = s1; i <= s2; i++) {
    sz = strlen((bj->msg)[i]);
    if(sz > back) back = sz;
  }
  return back;
}



static
void
fputs_formatted DK_P3(char *,s, FILE *,f, size_t,maxl)
{
  size_t sz, i;
  fputs(s, f);
  sz = strlen(s);
  if(sz < maxl) {
    for(i = sz; i < maxl; i++) fputc(' ', f);
  }
}



static
void
write_configuration DK_P1(BJ *,bj)
{
  
  dkapp_set_pref(bj->a, pk_lang, (bj->lang ? bj->lang : empty_string));
  dkapp_set_pref(bj->a, pk_make, (((bj->mm) ? str_on : str_off)));
  
}



static
void
show_colored_output DK_P2(BJ *,bj, size_t, maxl)
{
  fputs_formatted((bj->msg)[8], stdout, maxl);
  fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_COLOR_OUTPUT) ? 3 : 4], stdout);
  fputc('\n', stdout);
}


static
void
show_details DK_P1(BJ *,bj)
{
  size_t sz, i;
  fputc('\n', stdout);
  fputs((bj->msg)[24], stdout); fputc('\n', stdout);
  sz = strlen((bj->msg)[24]);
  for(i = 0; i < sz; i++) fputc('-', stdout);
  fputc('\n', stdout);
}


static char *encoding_types[] = {
  "ascii85", "run-length", "lzw", "dct", "flate"
};

static
void
show_encodings DK_P2(BJ *,bj, size_t,maxl)
{
  int found_data = 0;
  fputs_formatted((bj->msg)[9], stdout, maxl);
  if(((bj->bo)->enc) & BMEPS_ENCODING_ASCII85) {
    if(found_data) fputc(',', stdout);
    fputs(encoding_types[0], stdout); found_data = 1;
  }
  if(((bj->bo)->enc) & BMEPS_ENCODING_RUNLENGTH) {
    if(found_data) fputc(',', stdout);
    fputs(encoding_types[1], stdout); found_data = 1;
  }
  if(((bj->bo)->enc) & BMEPS_ENCODING_DCT) {
    if(found_data) fputc(',', stdout);
    fputs(encoding_types[3], stdout); found_data = 1;
  }
  if(((bj->bo)->enc) & BMEPS_ENCODING_FLATE) {
    if(found_data) fputc(',', stdout);
    fputs(encoding_types[4], stdout); found_data = 1;
  }
  fputc('\n', stdout);
}



static
void
show_interpolation DK_P2(BJ *,bj, size_t,maxl)
{
  fputs_formatted((bj->msg)[22], stdout, maxl);
  fputs((bj->msg)[((bj->bo)->opt & BMEPS_OPT_INTERPOLATE) ? 3 : 4], stdout);
  fputc('\n', stdout);
}



static
void
show_alpha_channel_mixing DK_P2(BJ *,bj, size_t,maxl)
{
  char colorbuffer[256];
  fputs_formatted((bj->msg)[11], stdout, maxl);
  fputs((bj->msg)[((bj->bo)->opt & BMEPS_OPT_ALPHA_MIX) ? 3 : 4], stdout);
  fputc('\n', stdout);
  if((bj->bo)->opt & BMEPS_OPT_ALPHA_MIX) {
    
    fputs_formatted((bj->msg)[25], stdout, maxl);
    
    sprintf(
      colorbuffer,
      "r=%lg g=%lg b=%lg",
      (bj->bo)->dbgr, (bj->bo)->dbgg, (bj->bo)->dbgb
    );
    
    fputs(colorbuffer, stdout);
    
    fputc('\n', stdout);
    
    fputs_formatted((bj->msg)[12], stdout, maxl);
    
    fputs((bj->msg)[((bj->bo)->opt & BMEPS_OPT_PREFER_SPEC_BG) ? 3 : 4], stdout);
    
    fputc('\n', stdout);
  }
  fputs_formatted((bj->msg)[13], stdout, maxl);
  fputs((bj->msg)[((bj->bo)->opt & BMEPS_OPT_IMAGE_MASK) ? 3 : 4], stdout);
  fputc('\n', stdout);
  if((bj->bo)->opt & BMEPS_OPT_IMAGE_MASK) {
    fputs_formatted((bj->msg)[14], stdout, maxl);
    sprintf(colorbuffer, "%lg", (bj->bo)->mtl);
    fputs(colorbuffer, stdout);
    fputc('\n', stdout);
  }
}



static
void
show_resolution_and_media_size DK_P2(BJ *,bj, size_t, maxl)
{
  char colorbuffer[256];
  if((bj->bo)->opt & BMEPS_OPT_PAPER_SIZE) {
    fputs_formatted((bj->msg)[21], stdout, maxl);
    sprintf(
      colorbuffer, 
      "(%lg %lg)...(%lg %lg) / (%lg %lg)...(%lg %lg)",
      ((bj->bo)->ms).bx0,
      ((bj->bo)->ms).by0,
      ((bj->bo)->ms).bx1,
      ((bj->bo)->ms).by1,
      ((bj->bo)->ms).px0,
      ((bj->bo)->ms).py0,
      ((bj->bo)->ms).px1,
      ((bj->bo)->ms).py1
    );
    fputs(colorbuffer, stdout);
    fputc('\n', stdout);
  } else {
    fputs_formatted((bj->msg)[15], stdout, maxl);
    fputs((bj->msg)[((bj->bo)->opt & BMEPS_OPT_RESOLUTION) ? 3 : 4], stdout);
    fputc('\n', stdout);
  }
}


static
void
show_configuration_eps DK_P2(BJ *,bj, size_t, maxl)
{
  fputs_formatted((bj->msg)[6], stdout, maxl);
  switch((bj->bo)->l) {
    case BMEPS_PS_LEVEL_1: {
      fputs(eps_levels[0], stdout);
    } break;
    case BMEPS_PS_LEVEL_2: {
      fputs(eps_levels[1], stdout);
    } break;
    case BMEPS_PS_LEVEL_3: {
      fputs(eps_levels[2], stdout);
    } break;
  }
  fputc('\n', stdout);
  fputs_formatted((bj->msg)[23], stdout, maxl);
  fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_DRAFT) ? 3 : 4], stdout);
  fputc('\n', stdout);
  if((bj->bo)->l >= BMEPS_PS_LEVEL_2) {
    show_colored_output(bj, maxl);
    if(((bj->bo)->opt) & BMEPS_OPT_COLOR_OUTPUT) {
      fputs_formatted((bj->msg)[10], stdout, maxl);
      fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_SEPARATED_DATA) ? 3 : 4], stdout);
      fputc('\n', stdout);
    }
    fputs_formatted((bj->msg)[19], stdout, maxl);
    fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_OPERATOR_DICTIONARY) ? 3 : 4], stdout);
    fputc('\n', stdout);
    if((bj->bo)->opt & BMEPS_OPT_OPERATOR_DICTIONARY) {
      show_interpolation(bj, maxl);
    }
    show_encodings(bj, maxl);
    show_alpha_channel_mixing(bj, maxl);
  }
  show_resolution_and_media_size(bj, maxl);
  fputs_formatted((bj->msg)[16], stdout, maxl);
  fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_SHOWPAGE) ? 3 : 4], stdout);
  fputc('\n', stdout);
  if((bj->bo)->l >= BMEPS_PS_LEVEL_2) {
    fputs_formatted((bj->msg)[17], stdout, maxl);
    fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_DICTIONARY) ? 3 : 4], stdout);
    fputc('\n', stdout);
    fputs_formatted((bj->msg)[18], stdout, maxl);
    fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_VMRECLAIM) ? 3 : 4], stdout);
    fputc('\n', stdout);
  }
}



static
void
show_configuration_pdf DK_P2(BJ *,bj, size_t, maxl)
{
  int ta;	/* transfer alpha channel */
  ta = 0;
  fputs_formatted((bj->msg)[6], stdout, maxl);
  switch((bj->bo)->l) {
    case BMEPS_PDF_LEVEL_12: {
      fputs(pdf_levels[0], stdout);
    } break;
    case BMEPS_PDF_LEVEL_13: {
      fputs(pdf_levels[1], stdout);
    } break;
    case BMEPS_PDF_LEVEL_14: {
      fputs(pdf_levels[2], stdout);
    } break;
  }
  fputc('\n', stdout);
  fputs_formatted((bj->msg)[23], stdout, maxl);
  fputs((bj->msg)[(((bj->bo)->opt) & BMEPS_OPT_DRAFT) ? 3 : 4], stdout);
  fputc('\n', stdout);
  show_colored_output(bj, maxl);
  show_encodings(bj, maxl);
  show_interpolation(bj, maxl);
  if((bj->bo)->l >= BMEPS_PDF_LEVEL_14) {
    if((bj->bo)->opt & BMEPS_OPT_TRANSFER_ALPHA) {
      ta = 1;
    }
  }
  fputs_formatted((bj->msg)[20], stdout, maxl);
  fputs((bj->msg)[(ta) ? 3 : 4], stdout);
  fputc('\n', stdout);
  if(!ta) {
    show_alpha_channel_mixing(bj, maxl);
  }
  show_resolution_and_media_size(bj, maxl);
}



static
void
show_configuration DK_P1(BJ *,bj)
{
  char *str;
  size_t sz, i;
  
  fputc('\n', stdout);
  str = (bj->msg)[0];
  fputs(str, stdout); fputc('\n', stdout);
  sz = strlen(str);
  for(i = 0; i < sz; i++) fputc('-', stdout); fputc('\n', stdout);
  sz = 1 + max_str_length(bj, 1, 2);
  fputs_formatted((bj->msg)[1], stdout, sz);
  fputs(((bj->lang) ? (bj->lang) : empty_string), stdout);
  fputc('\n', stdout);
  fputs_formatted((bj->msg)[2], stdout, sz);
  fputs((bj->msg)[(bj->mm) ? 3 : 4], stdout);
  fputc('\n', stdout);
  show_details(bj);
  sz = 1 + max_str_length(bj, 5, 23);
  fputs_formatted((bj->msg)[5], stdout, sz);
  switch((bj->bo)->ot) {
    case BMEPS_OUTPUT_TYPE_PDF: {
      fputs(output_type_names[1], stdout);
    } break;
    case BMEPS_OUTPUT_TYPE_BB: {
      fputs(output_type_names[2], stdout);
    } break;
    default: {
      fputs(output_type_names[0], stdout);
    } break;
  }
  fputc('\n', stdout);
  switch((bj->bo)->ot) {
    case BMEPS_OUTPUT_TYPE_PDF: {
      show_configuration_pdf(bj, sz);
    } break;
    case BMEPS_OUTPUT_TYPE_BB: {
    } break;
    default: {
      show_configuration_eps(bj, sz);
    } break;
  }
  
}



static
void
run_for_app_options DK_P1(BJ *,bj)
{
  
    if((bj->cmd) & BMEPS_CMD_HELP) {
      show_version(bj);
      show_help(bj);
    } else {
      if((bj->cmd) & BMEPS_CMD_VERSION) {
        show_version(bj);
      }
      if((bj->cmd) & BMEPS_CMD_UNCONFIGURE) {
        dkapp_unconfigure(bj->a);
      } else {
        if((bj->cmd) & BMEPS_CMD_CONFIGURE) {
	  write_configuration(bj);
	}
      }
      if((bj->cmd) & (BMEPS_CMD_CONFIGURE | BMEPS_CMD_SHOWCONF)) {
        show_configuration(bj);
      }
    }
  
}



static
int
transfer_file_contents DK_P3(BJ *,bj, FILE *,fi, FILE *,fo)
{
  int back = 1, write_failed = 0;
  char buffer[4096]; size_t s1, s2; int cc;
  while(cc) {
    s1 = fread((void *)buffer, 1, sizeof(buffer), fi);
    if(s1 > 0) {
      if(fo) {
        s2 = fwrite((void *)buffer, 1, s1, fo);
        if(s2 < s1) {
          cc = 0; back = 0;
	  write_failed = 1;
        }
      }
    } else {
      cc = 0;
    }
  }
  if(write_failed) {
    bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 53);
  }
  return back;
}



static
void
run_for_two_real_filenames DK_P3(BJ *,bj, char *,in, char *,on)
{
  FILE *fpi = NULL, *fpo = NULL;
  
  if(bj->it == DKBIF_TYPE_UNKNOWN) bj->it = dkbif_get_type(in);
  fpi = dkapp_fopen(bj->a, in, "rb");
  if(fpi) {
    fpo = dkapp_fopen(bj->a, on, "wb");
    if(fpo) {
      bj->inf = fpi;
      bj->outf = fpo;
      bj->infname = in;
      bmeps_convert(bj);
      fclose(fpo); fpo = NULL;
    } else {
      bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 38, 39, on);
      bj->exval = 1;
    }
    fclose(fpi); fpi = NULL;
  } else {
    bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 36, 37, in);
    bj->exval = 1;
  }
  
}



/*
  The filenames are not expanded yet.
*/
static
void
run_for_two_filenames DK_P1(BJ *,bj)
{
  dk_fne_t *fnei, *fneo;
  char *iname = NULL, *oname = NULL;
  
  if(dksf_must_expand_filename(bj->i1)) {
    fnei = dkfne_open(bj->i1, 1, 0);
    if(fnei) {
      iname = dkfne_get_one(fnei);
      if(iname) {
        if(dksf_must_expand_filename(bj->o1)) {
	  fneo = dkfne_open(bj->o1, 1, 0);
	  if(fneo) {
	    oname = dkfne_get_one(fneo);
	    if(oname) {
	      run_for_two_real_filenames(bj, iname, oname);
	    } else {
	      bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 43, 44, bj->o1);
	      bj->exval = 1;
	    }
	    dkfne_close(fneo); fneo = NULL;
	  } else {
	    bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 40);
	    bj->exval = 1;
	  }
	} else {
	  run_for_two_real_filenames(bj, iname, bj->o1);
	}
      } else {
	bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 41, 42, bj->i1);
	bj->exval = 1;
      }
      dkfne_close(fnei);
    } else {
      bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 40);
      bj->exval = 1;
    }
  } else {
    if(dksf_must_expand_filename(bj->o1)) {
      fneo = dkfne_open(bj->o1, 1, 0);
      if(fneo) {
        oname = dkfne_get_one(fneo);
	if(oname) {
	  run_for_two_real_filenames(bj, bj->i1, oname);
	} else {
	  bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 43, 44, bj->o1);
	  bj->exval = 1;
	}
        dkfne_close(fneo); fneo = NULL;
      } else {
	bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 40);
	bj->exval = 1;
      }
    } else {
      run_for_two_real_filenames(bj, bj->i1, bj->o1);
    }
  }
  
}



/* File name is expanded */
static
void
run_for_one_real_filename DK_P2(BJ *,bj, char *,n)
{
  FILE *fp;
  
  if(bj->it == DKBIF_TYPE_UNKNOWN) bj->it = dkbif_get_type(n);
  fp = dkapp_fopen(bj->a, n, "rb");
  if(fp) {
    bj->inf = fp;
    bj->infname = n;
    bmeps_convert(bj);
    fclose(fp); fp = NULL;
  } else {
    bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 36, 37, n);
    bj->exval = 1;
  }
  
}


/* File name is not yet expanded */
static
void
run_for_one_filename DK_P1(BJ *,bj)
{
  dk_fne_t *fne = NULL;
  char *in;
  
  if(dksf_must_expand_filename(bj->i1)) {
    fne = dkfne_open(bj->i1, 1, 0);
    if(fne) {
      in = dkfne_get_one(fne);
      if(in) {
        run_for_one_real_filename(bj, in);
      } else {
	bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 41, 42, bj->i1);
	bj->exval = 1;
      }
      dkfne_close(fne); fne = NULL;
    } else {
      bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 40);
      bj->exval = 1;
    }
  } else {
    run_for_one_real_filename(bj, bj->i1);
  }
  
}



static
int
compare_strings DK_P3(void *,l, void *,r, int,cr)
{
  int back = 0;
  if(l) {
    if(r) {
      back = strcmp((char *)l, (char *)r);
    } else back = 1;
  } else {
    if(r) back = -1;
  }
  return back;
}



static
unsigned char
compare_modification_time DK_P2(char *,d, char *,s)
{
  char *td, *ts;
  dk_stat_t statd, stats;
  unsigned char back = 0x01;
  
  if(dkstat_get(&statd, d)) {
    if(dkstat_get(&stats, s)) {
      td = dkstat_mtime(&statd);
      ts = dkstat_mtime(&stats);
      
      switch(strcmp(td, ts)) {
        case 1: back = 0x00; break;
      }
    }
  }
  
  return back;
}


static char sep[] = {
#if DK_HAVE_FEATURE_BACKSLASH
"\\"
#else
"/"
#endif
};



/* bj->i1 is a directory name */
static
void
run_for_directory DK_P1(BJ *,bj)
{
  dk_storage_t *s; dk_storage_iterator_t *si;
  char *namei = NULL, *nameo = NULL, *n, *c, *fn, *ots; int t, onok;
  dk_dir_t *d; long mpl;
  unsigned char mr;
  
  s = dksto_open(0);
  if(s) {
    si = dksto_it_open(s);
    if(si) {
      mpl = dksf_get_maxpathlen();
      namei = dk_new(char,mpl); nameo = dk_new(char,mpl);
      if((namei) && (nameo)) {
        d = dkdir_open(bj->i1);
	if(d) {
	  while(dkdir_next(d)) {
	    n = dkdir_get_shortname(d);
	    if(n) {
	      t = dkbif_get_type(n);
	      if(t != DKBIF_TYPE_UNKNOWN) {
		if(dkbif_can_handle(t)) {
		  c = dkstr_dup(n);
		  if(c) {
		    if(!dksto_add(s, (void *)c)) {
		      bmeps_tool_error_memory(bj); bj->exval = 1;
		      dk_delete(c); c = NULL;
		    }
		  } else {
		    bmeps_tool_error_memory(bj); bj->exval = 1;
		  }
		}
	      }
	    } else {
	      bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 56);
	      bj->exval = 1;
	    }
	  }
          dkdir_close(d); d = NULL;
	} else {
	  bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 54, 55, bj->i1);
	  bj->exval = 1;
	}
        dksto_it_reset(si);
        while((fn = (char *)dksto_it_next(si)) != NULL) {
          /* TODO: Complete */
          
	  if((strlen(bj->i1) + strlen(fn) + 1) < (size_t)mpl) {
	    sprintf(namei, "%s%s%s", bj->i1, sep, fn);
	    strcpy(nameo, namei);
	    onok = 0; ots = bmeps_tool_suffix_for_output_type((bj->bo)->ot);
	    c = dksf_get_file_type_dot(nameo);
	    if(c) {
	      *c = '\0';
	      if((strlen(nameo) + strlen(ots)) < (size_t)mpl) {
	        strcpy(c, ots); onok = 1;
	      }
	    } else {
	      if((strlen(nameo) + strlen(ots)) < (size_t)mpl) {
	        strcat(nameo, ots); onok = 1;
	      }
	    }
	    if(onok) {
	      
	      mr = 0x01;
	      if(bj->mm) {
	        mr = compare_modification_time(nameo, namei);
	      }
	      if(mr) {
	        run_for_two_real_filenames(bj, namei, nameo);
	      }
	    } else {
	      bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 47, 48, fn);
	      bj->exval = 1;
	    }
	  } else {
	    bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 45, 46, fn);
	    bj->exval = 1;
	  }
        }
      } else {
	bmeps_tool_error_memory(bj); bj->exval = 1;
      }
      if(namei) { dk_delete(namei); namei = NULL; }
      if(nameo) { dk_delete(nameo); nameo = NULL; }
      /* release file names */
      dksto_it_reset(si);
      while((fn = (char *)dksto_it_next(si)) != NULL) {
        dk_delete(fn);
      }
      dksto_it_close(si); si = NULL;
    } else {
      bmeps_tool_error_memory(bj); bj->exval = 1;
    }
    dksto_close(s); s = NULL;
  } else {
    bmeps_tool_error_memory(bj); bj->exval = 1;
  }
  
}


static char *suffixes[] = { ".pdf", ".eps", ".bb" };

static
void
run_for_filenames DK_P1(BJ *,bj)
{
  FILE *fp = NULL; int res;
  char *auto_file_name = NULL, *ptr = NULL;
  size_t lgt;
  
  if(bj->o1) {		
    run_for_two_filenames(bj);
  } else {
    dksf_set_file_binary(stdout);
    bj->outf = stdout;
    if(bj->i1) {
      if(dksf_must_expand_filename(bj->i1)) {
        run_for_one_filename(bj);
      } else {
        if(dksf_is_directory(bj->i1)) {
	  run_for_directory(bj);
	} else {
	  if(bj->af) {
	    lgt = 5 + strlen(bj->i1);
	    auto_file_name = dk_new(char,lgt);
	    if(auto_file_name) {
	      strcpy(auto_file_name, bj->i1);
	      ptr = dksf_get_file_type_dot(auto_file_name);
	      if(ptr) { *ptr = '\0'; }
	      if(bj->bo) {
	        switch((bj->bo)->ot) {
	          case BMEPS_OUTPUT_TYPE_PDF: {
	            strcat(auto_file_name, suffixes[0]);
	          } break;
	          case BMEPS_OUTPUT_TYPE_EPS: {
	            strcat(auto_file_name, suffixes[1]);
	          } break;
	          default: {
	            strcat(auto_file_name, suffixes[2]);
	          } break;
		}
		bj->o1 = auto_file_name;
		bmeps_tool_msg_3(bj, DK_LOG_LEVEL_INFO, 79, 80, auto_file_name);
		run_for_two_filenames(bj);
	      }
	      bj->o1 = NULL;
	      dk_delete(auto_file_name);
	      auto_file_name = NULL;
	    } else {
	      bmeps_tool_error_memory(bj); bj->exval = 1;
	    }
	  } else {
	    run_for_one_real_filename(bj, bj->i1);
	  }
	}
      }
    } else {		
      if(bj->it != DKBIF_TYPE_UNKNOWN) {
        dksf_set_file_binary(stdin);
	fp = dkapp_fopen(bj->a, bj->tmp1, "wb");
	if(fp) {
	  res = transfer_file_contents(bj, stdin, fp);
	  fclose(fp); fp = NULL;
	  if(res) {
	    bj->inf = dkapp_fopen(bj->a, bj->tmp1, "rb");
	    if(bj->inf) {
	      bj->outf = stdout;
	      bj->infname = NULL;
	      bmeps_convert(bj);
	      fclose(bj->inf); bj->inf = NULL;
	    } else {
	      bmeps_tool_msg_3(bj, DK_LOG_LEVEL_ERROR, 34, 35, bj->tmp1);
	      bj->exval = 1;
	    }
	  }
	  /* remove temporary file */
	  dksf_remove_file(bj->tmp1);
	} else {
	  (void)transfer_file_contents(bj, stdin, NULL);
	}
      } else {		
	bmeps_tool_msg_1(bj, DK_LOG_LEVEL_ERROR, 33);
	bj->exval = 1;
      }
    }
  }
  
}



static
void
do_real_work DK_P1(BJ *,bj)
{
  char *bptr, *p1, *p2, *p3, *p4, *p5, *p6, *p7;
  size_t blen;
  
  blen = (size_t)dksf_get_maxpathlen();
  bptr = dk_new(char,blen);
  if(bptr) {
    p1 = p2 = p3 = NULL;
    if(dkapp_tmpnam(bj->a, bptr, blen)) {
      p1 = dkstr_dup(bptr);
    }
    if(dkapp_tmpnam(bj->a, bptr, blen)) {
      p2 = dkstr_dup(bptr);
    }
    if(dkapp_tmpnam(bj->a, bptr, blen)) {
      p3 = dkstr_dup(bptr);
    }
    if(dkapp_tmpnam(bj->a, bptr, blen)) {
      p4 = dkstr_dup(bptr);
    }
    if(dkapp_tmpnam(bj->a, bptr, blen)) {
      p5 = dkstr_dup(bptr);
    }
    if(dkapp_tmpnam(bj->a, bptr, blen)) {
      p6 = dkstr_dup(bptr);
    }
    if(dkapp_tmpnam(bj->a, bptr, blen)) {
      p7 = dkstr_dup(bptr);
    }
    if((p1) && (p2) && (p3) && (p4) && (p5) && (p6) && (p7)) {
      bj->tmp1 = p1; bj->tmp2 = p2; bj->tmp3 = p3; bj->tmp4 = p4;
      bj->tmp5 = p5; bj->tmp6 = p6; bj->tmp7 = p7;
      run_for_filenames(bj);
    } else {
      bmeps_tool_error_memory(bj);
      bj->exval = 1;
    }
    if(p1) { dk_delete(p1); }
    if(p2) { dk_delete(p2); }
    if(p3) { dk_delete(p3); }
    if(p4) { dk_delete(p4); }
    if(p5) { dk_delete(p5); }
    if(p6) { dk_delete(p6); }
    if(p7) { dk_delete(p7); }
    dk_delete(bptr); bptr = NULL;
  } else {
    bmeps_tool_error_memory(bj);
    bj->exval = 1;
  }
  
}



static
void
run_with_strings DK_P1(BJ *,bj)
{
  char namebuffer[NAME_BUFFER_SIZE], *l;
  
  if(dkapp_get_pref(bj->a, pk_make, namebuffer, sizeof(namebuffer), 0)) {
    if(dkstr_is_bool(namebuffer)) {
      if(dkstr_is_on(namebuffer)) {
        bj->mm = 0x01;
      } else {
        bj->mm = 0x00;
      }
    }
  }
  l = NULL;
  if(bmeps_bo_get_pref_lang(bj, namebuffer, sizeof(namebuffer))) {
    l = namebuffer;
  }
  apply_arguments(bj, l);
  if(!((bj->cmd) & BMEPS_CMD_ERROR)) {
    if((bj->cmd) & BMEPS_CMD_APP) {
      run_for_app_options(bj);
      bj->exval = 1;
    } else {
      do_real_work(bj);
    }
  }
  
}



static
void
run DK_P1(BJ *,bj)
{
  size_t sz, i;
  dk_string_finder_t *fp, *fpptr; PCHAR *pp, *ppptr;
  char buffer[32], *x;
  int ok = 0;
  
  sz = bmeps_get_num();
  
  fp = dk_new(dk_string_finder_t,(sz+1));
  if(fp) {				
    pp = dk_new(PCHAR,sz);
    if(pp) {				
      fpptr = fp; ppptr = pp;
      ok = 1;
      for(i = 0; i < sz; i++) {
        sprintf(buffer, "/m/%03lu", (unsigned long)i);
	fpptr->key = dkstr_dup(buffer);
	if(!(fpptr->key)) {		
	  ok = 0;
	}
        fpptr->value_pointer = ppptr;
	fpptr->default_value = bmeps_str_get(i);
        fpptr++; ppptr++;
      }
      fp[sz].key = NULL;
      fp[sz].value_pointer = NULL;
      fp[sz].default_value = NULL;
      if(ok) {				
        dkapp_find_multi(bj->a, fp, "bmeps");
	bj->msg = pp;
	run_with_strings(bj);
	bj->msg = NULL;
      } else {
	bmeps_tool_error_memory(bj);
	bj->exval = 1;
      }
      fpptr = fp;
      for(i = 0; i < sz; i++) {
        if(fpptr->key) {		
	  x = fpptr->key; dk_delete(x);
	}
	fpptr->key = NULL;
	
#line 1364 "b2main.ctr"
	
#line 1365 "b2main.ctr"
	
#line 1366 "b2main.ctr"
	
#line 1367 "b2main.ctr"
	
#line 1368 "b2main.ctr"
	
#line 1369 "b2main.ctr"
	
#line 1370 "b2main.ctr"
	
#line 1371 "b2main.ctr"
	
#line 1372 "b2main.ctr"
	
#line 1373 "b2main.ctr"
	
#line 1374 "b2main.ctr"
	fpptr->value_pointer = NULL;
	
#line 1376 "b2main.ctr"
	
#line 1377 "b2main.ctr"
	
#line 1378 "b2main.ctr"
	
#line 1379 "b2main.ctr"
	
#line 1380 "b2main.ctr"
	
#line 1381 "b2main.ctr"
	
#line 1382 "b2main.ctr"
	
#line 1383 "b2main.ctr"
	
#line 1384 "b2main.ctr"
	fpptr->default_value = NULL;
        fpptr++;
      }
      dk_delete(pp);
    } else {				
      bmeps_tool_error_memory(bj);
      bj->exval = 1;
    }
    dk_delete(fp);
  } else {				
    bmeps_tool_error_memory(bj);
    bj->exval = 1;
  }
  
}



static BO bo;
static BJ bj;


#if DK_HAVE_PROTOTYPES
int main(int argc, char *argv[])
#else
int main(argc, argv) int argc; char *argv[];
#endif
{
  int rs = 0, rf = 0; 
  dk_app_t *a = NULL;
  
#line 1414 "b2main.ctr"

  
  bj.exval = 0;
  silence_check(argc, argv, &rs, &rf);
  a = dkapp_open_ext1(argc, argv, GROUPNAME, SYSCONFDIR, rs, rf);
  if(a) {
    bmeps_bj_init(&bj);
    bj.bo = &bo;
    bj.a  = a;
    run(&bj);
    dkapp_close(a); a = NULL;
  } else {
    if(!rs) {
      fprintf(stderr, "Not enough memory (RAM/swap)!\n"); fflush(stderr);
    }
    bj.exval = 1;
  }
  
  
#line 1432 "b2main.ctr"

  exit(bj.exval); return bj.exval;
}


#ifndef LINT
static char sccs_id[] = {
"@(#)b2main.ctr 1.10 01/03/07\tbmeps2"
};
#endif

