/*  filo -- manipulate a `rolodex'-like database
 *  Copyright (C) 1994 David Deaven, Jennifer Corkill
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: settings.c,v 1.4 1996/06/02 02:48:35 deaven Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dmd/memory.h"
#include "dmd/tools.h"
#include "filo.h"

static char *usage =
  "usage: %s [options] [regexp], where options are\n"
  "\t-i         Ignore case in searches\n"
  "\t-e         Write PostScript for an envelope\n"
  "\t-k         Write PostScript for an envelope kit (8.5x11 paper)\n"
  "\t-b         No bar code\n"
  "\t-d         Make a directory\n"
  "\t-t         Use TeX directory format\n"
  "\t-p         Use PostScript directory format\n"
  "\t-o fn      output file\n"
  "\t-f fn      Use filodex file `fn'\n"
  "\t-r fn      Return PS-file (\"-\" for none)\n"
  "\t-s fn      Stamp PS_file (\"-\" for none)\n"
  "\t-h fn      Header PS-file\n"
  "\t-E x,y     Envelope size in mm (com10=241,105 personal=165,92)\n"
  "\t-T x,y     Position of to address: mm right, above center\n"
  "\t-R x,y     Position of return address: mm right, below upper left\n"
  "\t-S x,y     Position of stamp: mm left, below upper right\n"
  "\t-B x,y     Position of barcode: mm right, above lower center\n"
  "\t-P x,y     Add to printer offset in mm\n"
  "\t-O         Draw an outline of the envelope\n"
  "\t@settings  Read options from file `settings'\n"
;

void set_defaults(struct filo_settings *set)
{
	set->mode = DISPLAY_MODE;	/* just show records */
	set->dstyle = ASCII_STYLE;	/* plain directory */
	set->ignore_case = 0;		/* case-sensitive */
	set->outline = 0;		/* no envelope outline */
	set->do_barzip = 1;		/* use the bar code zipcode */
	set->sx = COM10_SX;		/* standard com10 envelope */
	set->sy = COM10_SY;
	set->header = NULL;		/* use internal version */
	set->ofn = "-";			/* standard output */

	set->xoff = set->yoff = 0;	/* defaults for Apple LaserWriter */

	set->from_x = 5;		/* mm from left */
	set->from_y = 8;		/* mm from top */

	set->stamp_x = 12;		/* mm from right */
	set->stamp_y = 8;		/* mm from top */

	set->to_x = -15;		/* mm right of center */
	set->to_y = 5;			/* mm above center */

	set->bar_x = 24;		/* mm right of center */
	set->bar_y = 12;		/* mm above bottom */

	/* Find out where file should be searched for, and what the
	 * default file names are.
	 */
	if((set->filopath = tryenv("FILOPATH", NULL)) == NULL)
	{
	  if((set->filopath = tryenv("HOME", NULL)) != NULL)
	    set->filopath = strconcat(set->filopath, "/.filo");
	  else
	    set->filopath = mystrdup(".");
	}
	set->filodex = tryenv("FILODEX", "filodex");
	set->sender = tryenv("FILORETURN", "filoreturn");
	set->stamp  = tryenv("FILOSTAMP", "filostamp");
}

void print_usage(int argc, char **argv)
{
	fprintf(stderr, usage, argv[0]);
}

static void rmarg(int m, int *argc, char **argv)
{
	int i;
	--(*argc);
	for(i=m; i<(*argc); ++i)
	  argv[i] = argv[i + 1];
}

/* Parse the command line options.
 */
char **set_from_args(struct filo_settings *set, int *argc, char **argv)
{
	int arg, recog;
	char **new = mymalloc((*argc) * sizeof(*new));

	for(arg=0; arg<(*argc); ++arg) new[arg] = argv[arg];
	argv = new;

	arg = 1;
	while(arg < (*argc)) {
	  if(argv[arg][0] == '-') {
	    recog = 1;
	    switch(argv[arg][1]) {
	      case 'i':
		set->ignore_case = 1; 
		break;
	      case 'e':
		set->mode = ENVELOPE_MODE; 
		break;
	      case 'k':
		set->mode = ENVELOPE_KIT_MODE; 
		break;
	      case 'd':
		set->mode = DIRECTORY_MODE; 
		break;
	      case 'O':
		set->outline = 1; 
		break;
	      case 'b':
		set->do_barzip = 0;
		break;
	      case 't':
		set->mode = DIRECTORY_MODE; 
		set->dstyle = TEX_STYLE; 
		break;
	      case 'p':
		set->mode = DIRECTORY_MODE; 
		set->dstyle = POSTSCRIPT_STYLE; 
		break;
	      case 'o':
		rmarg(arg, argc, argv);
		set->ofn = argv[arg];
		break;
	      case 'f':
		rmarg(arg, argc, argv);
		set->filodex = argv[arg]; 
		break;
	      case 'h':
		rmarg(arg, argc, argv);
		set->header = argv[arg]; 
		break;
	      case 'r':
		rmarg(arg, argc, argv);
		set->sender = argv[arg]; 
		break;
	      case 's':
		rmarg(arg, argc, argv);
		set->stamp = argv[arg]; 
		break;

	      case 'E':
		rmarg(arg, argc, argv);
		sscanf(argv[arg], "%d,%d", &set->sx, &set->sy); 
		break;
	      case 'T':
		rmarg(arg, argc, argv);
		sscanf(argv[arg], "%d,%d", &set->to_x, &set->to_y);
		break;
	      case 'R':
		rmarg(arg, argc, argv);
		sscanf(argv[arg], "%d,%d", &set->from_x, &set->from_y);
		break;
	      case 'S':
		rmarg(arg, argc, argv);
		sscanf(argv[arg], "%d,%d", &set->stamp_x, &set->stamp_y);
		break;
	      case 'B':
		rmarg(arg, argc, argv);
		sscanf(argv[arg], "%d,%d", &set->bar_x, &set->bar_y);
		break;
	      case 'P':
		{ int dx, dy;
		  rmarg(arg, argc, argv);
		  sscanf(argv[arg], "%d,%d", &dx, &dy);
		  set->xoff += dx;
		  set->yoff += dy;
		}
		break;

	      default:
		recog = 0;
		break;
	    }
	    if(recog)
	      rmarg(arg, argc, argv);
	    else {
	      fprintf(stderr, "%s: skipping \"%s\"\n", 
		argv[0], argv[arg]);
	      ++arg;
	    }
	  } /* '-' */
	  else if(argv[arg][0] == '@') {
	    FILE *f = fopen_path(set->filopath, argv[arg] + 1, "r");
	    if(f) {
	      argv = fgetargv(f, arg + 1, argc, argv);
	      fclose(f);
	    }
	    else {
	      fprintf(stderr, "%s: cannot open settings file `%s'\n",
		argv[0], argv[arg] + 1);
	      exit(1);
	    }
	    rmarg(arg, argc, argv);
	  } /* '@' */
	  else
	    ++arg; /* leave it for the caller to handle */
	} /* arg < argc */

	return argv;
}
