/*  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: filo.c,v 1.2 1995/10/26 19:25:28 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"

void main(int argc, char **argv)
{
	int i, n; 
	char *record, *address, *search = NULL;
	FILE *fin, *fout;
	struct filo_settings *set;

	/* Initialize the program settings, then read all of the
	 * command line arguments that modify the settings.
	 */
	set = mymalloc(sizeof(*set));
	set_defaults(set);
	argv = set_from_args(set, &argc, argv);

	for(i=1; i<argc; ++i) search = argv[i]; /* XXX */
	if(search == NULL) {
	  fprintf(stderr, "%s: no search string!\n", argv[0]);
	  print_usage(argc, argv);
	  exit(1);
	}

	/* Compile the regular expression search string.
	 */
	RegExpCompile(search, set->ignore_case);

	fin = fopen_path(set->filopath, set->filodex, "r");
	if(fin == NULL) {
	    fprintf(stderr, "%s: cannot open filodex `%s'.\n", 
	      argv[0], set->filodex);
	    exit(1);
	}

	fout = fopen_with_default(set->ofn, "w", NULL);

	/* Insert a PostScript header if required.
	 */
	if(  set->mode == ENVELOPE_MODE
	  || set->mode == ENVELOPE_KIT_MODE
	  || (set->mode == DIRECTORY_MODE && set->dstyle == POSTSCRIPT_STYLE)
	)
	{
	  if(set->header != NULL)
	    finsert(set->header, set->filopath, fout);
	  else
	    PrintHeader(fout);
	}

	/* Scan all records in the filodex.
	 */
	n = 0;
	while((record = read_filo(fin)) != NULL)
	{
	  if(record[0] != '%' && (search == NULL || RegExpCompare(record)))
	  {
	    ++n;
	    switch(set->mode)
	    {
	      case DISPLAY_MODE:
		fprintf(fout, "%s\n", record);
		break;

	      case ENVELOPE_MODE:
	      case ENVELOPE_KIT_MODE:
		fprintf(fout, "%%%%Page: %d %d\n"
		  "filoDict begin manualfeed\n"
		  "/EWIDTH %d mm def /EHEIGHT %d mm def\n"
		  "/XOFFSET %d mm def /YOFFSET %d mm def\n"
		  "%s\n",
		  n, n, set->sx, set->sy, set->xoff, set->yoff,
		  (set->mode == ENVELOPE_MODE ? "filostart" : "filostartkit"));
		if(set->outline)
		  fprintf(fout, "outline stroke\n");
		fprintf(fout,
		  "EWIDTH .5 mul %d mm add EHEIGHT .5 mul %d mm add "
		  "toaddress\n", set->to_x, set->to_y);
		  address = scan_address(record);
		  addps(fout, address);
		  myfree(address);
		if(strcmp(set->sender, "-")) {
		  fprintf(fout, "%d mm EHEIGHT %d mm sub fromaddress\n",
		    set->from_x, set->from_y);
		  finsert(set->sender, set->filopath, fout);
		}
		if(strcmp(set->stamp, "-")) {
		  fprintf(fout, "EWIDTH %d mm sub EHEIGHT %d mm sub stamp\n",
		    set->stamp_x, set->stamp_y);
		  finsert(set->stamp, set->filopath, fout);
		}
		if(set->do_barzip)
		  barzip(fout, record, set->bar_x, set->bar_y);
		fprintf(fout, "filoend end\n");
		break;

	      case DIRECTORY_MODE:
		AddDirectory(record);
		break;

	      default:
		break;
	    }
	  }
	}

	/* Do the final cleanup, depending upon the mode we are
	 * operating in.
	 */
	switch(set->mode) 
	{
	  case DISPLAY_MODE:
	    printf("%d matching record%s.\n", n, (n==1 ? "" : "s"));
	    break;

	  case ENVELOPE_MODE:
	  case ENVELOPE_KIT_MODE:
	    fprintf(fout, "%%%%Pages: %d\n%%%%EOF\n", n);
	    fprintf(stderr, "wrote %d envelope%s\n", n, (n > 1 ? "s" : ""));
	    break;

	  case DIRECTORY_MODE: /* XXX */
	    PrintDirectory(fout, set->dstyle);
	    break;

	  default:
	    break;
	}

	if(fout != stdout) fclose(fout);
	exit(0);
}
