/*
Copyright (c) 2004-2005, Dirk Krause
All rights reserved.

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 
  opyright 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
  its 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.
*/


/* {{{ include files */

#include "dkfig.h"


#line 43 "fig2vect.ctr"


/* }}} include files */




/* {{{ version number */

#ifndef VERSNUMB
#define VERSNUMB "1.1.8"
#endif
/* static char versnumb[] = { VERSNUMB }; */

/* }}} version number */




/* {{{ system configuration directory */

#ifndef SYSCONFDIR
#if DK_HAVE_FEATURE_BACKSLASH
#define SYSCONFDIR "\\ETC"
#else
#define SYSCONFDIR "/etc"
#endif
#endif
static char sysconfdir[] = { SYSCONFDIR };

/* }}} system configuration directory */



/* {{{ package name */

#ifndef PACKAGNAME
#define PACKAGENAME "fig2vect"
#endif
static char packagename[] = { PACKAGENAME };

/* }}} package name */


/* {{{ functions */



/* {{{ silence_check */


/* ********************************************************************* */
/* *                                                                   * */
/* *   Check whether the application must run silently                 * */
/* *   (--silent option, no log messages to stdout/stderr)             * */
/* *   or as a filter (no output file name, output goes                * */
/* *   to stdout, no log messages to stdout).                          * */
/* *                                                                   * */
/* ********************************************************************* */

#line 98 "fig2vect.ctr"


static void
silence_check DK_P4(int,argc,char **,argv,int *,rs,int *,rf)
{
  int isrs = 0; int isrf = 0;
  int i; int filenames;
  char *fn1, *ptr, **lfdptr;
  
  lfdptr = argv; lfdptr++; i = 1; filenames = 0; fn1 = NULL;
  while(i < argc) {
    ptr = *lfdptr;
    if(*ptr == '-') {
      ptr++;
      switch(*ptr) {
        case 's': isrs = 1; break;
	case 'l':
	case 'o': {
	  ptr++;
	  if(!(*ptr)) {
	    lfdptr++; i++;
	  }
	} break;
      }
    } else {
      filenames++;
      if(!fn1) fn1 = ptr;
    }
    lfdptr++; i++;
  }
  switch(filenames) {
    case 0: {
      isrf = 1;
    } break;
    case 1: {
      if(!dksf_is_directory(fn1)) {
        isrf = 1;
      }
    } break;
  }
  if(isrs) if(rs) *rs = isrs;
  if(isrf) if(rf) *rf = isrf;
  
}
/* }}} silence_check */



/* {{{ print_version */


/* ********************************************************************* */
/* *                                                                   * */
/* *   Print the version number.                                       * */
/* *                                                                   * */
/* ********************************************************************* */

#line 150 "fig2vect.ctr"


static char *version_text[] = {
  "fig2vect, version " VERSNUMB " (SCCS=1.105)",
  "Copyright (C) 2004 Dipl.-Ing. D. Krause",
"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
print_version DK_P0()
{
  char **xptr;
  
  xptr = version_text;
  while(*xptr) {
    fputs(*(xptr++), stdout); fputc('\n', stdout);
  }
  fputc('\n', stdout);
  xptr = dklic_get();
  fputs("Libraries used:\n", stdout);
#if HAVE_PNG_H
  fputs("libpng       the official PNG reference library\n", stdout);
#endif
#if HAVE_JPEGLIB_H
  fputs("jpegsrc      The Independent JPEG Group's free JPEG software\n", stdout);
#endif
#if HAVE_PNM_H
  fputs("netpbm       The image conversion toolkit\n", stdout);
#endif
  while(*xptr) {
    fputs(*(xptr++), stdout); fputc('\n', stdout);
  }
  fputc('\n', stdout);
  
}
/* }}} print_version */



/* {{{ print_help */


/* ********************************************************************* */
/* *                                                                   * */
/* *   Print help text.                                                * */
/* *                                                                   * */
/* ********************************************************************* */

#line 214 "fig2vect.ctr"


static char *help_text[] = {
  "fig2vect - XFig to vector graphics converter",
  "",
  "fig2vect [-l <language>] [-o <option>...] [-m] <directory>",
  "fig2vect [-l <language>] [-o <option>...] [ <inputfile> [<outputfile>] ]",
  "",
  "-l language",
  "      chooses the output language, must be defined in fig2vect.cfg",
  "",
  "-o option",
  "      passes the option to the output driver",
  "",
  "-m",
  "      enables make style when running on a directory",
  "",
  NULL
};

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

static void
print_help DK_P1(dk_app_t *,a)
{
  
  print_version();
  dkapp_help(a, help_file_name, help_text);
  
}
/* }}} print_help */



/* {{{ run_the_program */


/* ********************************************************************* */
/* *                                                                   * */
/* *   Decide whether to                                               * */
/* *   - run normally                                                  * */
/* *   - print help and/or version                                     * */
/* *   - configure/unconfigure the applicaton.                         * */
/* *                                                                   * */
/* ********************************************************************* */

#line 255 "fig2vect.ctr"


int
run_the_program DK_P1(dk_app_t *,app)
{
  int back = 0;
  dk_fig_conversion *c = NULL;
  int action, xargc; char **xargv;
  
  c = dkfig_co_new(app);
  if(c) {
    xargc = dkapp_get_argc(app);
    xargv = dkapp_get_argv(app);
    action = dkfig_co_apply_args(c, xargc, xargv);
    if(!(action & DKFIG_WHAT_ERROR)) {
      if(action & DKFIG_WHAT_HELP) {		
        print_help(app);
	back = 1;
      } else {					
        if(action & DKFIG_WHAT_UNCONFIGURE) {	
	  dkapp_unconfigure(app);
	  back = 1;
	} else {				
	  if(action & DKFIG_WHAT_CONFIGURE) {	
	    back = dkfig_co_configure_app(c);
	  } else {				
	    if(action &  DKFIG_WHAT_SHOWCONF) {	
	      dkfig_co_showconf_app(c);
	      back = 1;
	    } else {				
	      if(action & DKFIG_WHAT_VERSION) {	
	        print_version();
		back = 1;
	      } else {				
	        back = dkfig_co_run(c);
	      }
	    }
	  }
	}
      }
    }
    if(action & DKFIG_WHAT_ERROR) {
      back = 0;
    }
    dkfig_co_delete(c); c = NULL;
  } else {
    dkapp_err_memory(app, sizeof(dk_fig_conversion),1);
  }
  
  return back;
}
/* }}} run_the_program */



/* {{{ main */


/* ********************************************************************* */
/* *                                                                   * */
/* *   Main program.                                                   * */
/* *   Create application structure, initialize it and                 * */
/* *   invoke run_the_program().                                       * */
/* *                                                                   * */
/* ********************************************************************* */

#line 316 "fig2vect.ctr"


#if DK_HAVE_PROTOTYPES
int
main(int argc, char *argv[])
#else
int
main(argc, argv)
  int argc;
  char *argv[];
#endif
{
  int exval = 0;	/* program exit status */
  int rs = 0;		/* run silently */
  int rf = 0;		/* run as filter */
  dk_app_t *app = NULL;
#ifdef RUNTIMELIMITSECS
#if DK_HAVE_ALARM
  alarm(RUNTIMELIMITSECS);
#endif
#endif
  
#line 337 "fig2vect.ctr"

  
  silence_check(argc, argv, &rs, &rf);
#if DK_HAVE_TZSET
  tzset();
#endif
  app = dkapp_open_ext1(argc, argv, packagename, sysconfdir, rs, rf);
  if (app) {
    exval = run_the_program(app);
    dkapp_set_source_lineno(app, 0UL);
    dkapp_close(app); app = NULL;
  } else {
    if (!rs) {
       fprintf(stderr, "ERROR: Not enough memory (RAM)!\n");
       fflush(stderr);
    }
  }
  exval = (exval ? 0 : 1);
  
  
#line 356 "fig2vect.ctr"

#ifdef RUNTIMELIMITSECS
#if DK_HAVE_ALARM
  alarm(0);
#endif
#endif
  exit(exval); return exval;
}
/* }}} main */

/* }}} functions */



/* {{{ SCCS ID */
#ifndef LINT
static char sccs_id[] = {
"@(#)fig2vect.ctr 1.114 05/29/07\t(krause) - fig2vect"
};
#endif
/* }}} */
