/* $Log:	main.c,v $
 * Revision 0.8  92/11/23  19:46:49  19:46:49  bt (Bo Thide')
 * Fixed resolution bug. Portable downloading. Added/changed options. PJXL color support
 * 
 * Revision 0.7  92/11/13  02:41:34  02:41:34  bt (Bo Thide')
 * More bug fixes and improvements. Support for PaintJet XL
 * 
 * Revision 0.6  92/11/10  21:47:50  21:47:50  bt (Bo Thide')
 * Bug fixes. Added -R option. Better font handling.
 * 
 * Revision 0.5  92/11/09  16:25:37  16:25:37  bt (Bo Thide')
 * Rewrite of dospecial.c. Extended \special support
 * 
 * Revision 0.4  92/11/08  02:45:53  02:45:53  bt (Bo Thide')
 * Changed to portable bit manipulations. Replaced strrstr for non-POSIX compliant C. Fixed numerous bugs. Added support for more \special's.
 * 
 * Revision 0.3  92/08/24  12:45:44  12:45:44  bt (Bo Thide')
 * Fixed 8 bit (dc font) support.
 * 
 * Revision 0.2  92/08/23  17:28:58  17:28:58  bt (Bo Thide')
 * Source cleaned up.  Changed certain function calls.  Removed globals.
 * 
 * Revision 0.1  92/08/22  23:58:48  23:58:48  bt (Bo Thide')
 * First Release.
 *  */

#pragma COPYRIGHT "Bo Thide' and Helmut Kopka, 1992"
#include <stdio.h>
#include <string.h>
#include "globals.h"
#include "paths.h"
#include "pcl.h"

static char rcsid[] = "$Header: main.c,v 0.8 92/11/23 19:46:49 bt Exp $";

static char fatal_error[] = "This was a fatal error.\n";
static char copyright[] = "Copyright (c) Bo Thide' and Helmut Kopka, 1992.";

#ifdef TIMING
#include <sys/timeb.h>
#endif /* TIMING */

main(argc, argv, envp)
int	argc;
char	**argv;
char	**envp;
{
	char	bitname[NAMELENGTH];
	char	curname[NAMELENGTH];
	char	dviname[NAMELENGTH];
	char	usgname[NAMELENGTH];
	bool	outreverse=TRUE;
	short	device=DEV_LJPLUS;
	short	maxdown=MAXDOWN;
	short	numcopies=1;
	short	pcllevel=PCLLEVEL;
	short	resolution=RESOLUTION;
	short	twosided=FALSE;
	long	startloc=0;
	FILE	*bitfile;
	FILE	*dvifile;
	FILE	*resfile;
	FILE	*usgfile;

#ifdef TIMING
	struct timeb timebuffer;
	double  start_time, time;

        ftime(&timebuffer);
        start_time = (double)((timebuffer.time) + (timebuffer.millitm)/1000.0);
#endif /* TIMING */

	/* Get things started properly */
	initialize();

	/* Process all dvi2pcl options and construct the filenames. */
	getoptions(argc, argv, envp, bitname, dviname, usgname,
	  &pcllevel, &numcopies, &device, &maxdown, &outreverse,
	  &resolution, &twosided);

	/*
	 * Open the output bitfile or, if bitname is an empty string, stdout.
	 */
	if(strcmp(bitname, "") == 0) {
		bitfile = stdout;
	}
	else {
		if((bitfile = fopen(bitname,"w")) == NULL) {
			fprintf(stderr,"Cannot open the bit file %s for output.\n", bitname);
		exit(-1);
		}
	}

	/*
	 * Open the input dvifile or stdin.
	 */
	if(strcmp(dviname, DVIFILE_SUFFIX) == 0) {
		dvifile = stdin;
 	}
	else {
		if((dvifile = fopen(dviname,"r")) == NULL) {
			fprintf(stderr,"Cannot open the dvi file %s for input.\n", dviname);
		exit(-1);
		}
	}

	/*
	 * Open the input resident font info file
	 */
	strcpy(curname, respath);
	if((resfile = fopen(curname,"r")) == NULL) {
		fprintf(stderr,"Cannot open the resident font file %s for input.\n", curname);
		exit(-1);
	}

	/* Prescan the input file. */ 
	preamble(dvifile, resolution);
	startloc = skippages(bitfile, dvifile, pcllevel, resolution);	
	if(!inpostamble){
		/* If not in postamble, prescan the file up to maxpages */
		while(maxpages-- > 0){
			/* Prescan up to maxpages or postamble */
			prescanpage(bitfile, dvifile, pcllevel, resolution);
			if(inpostamble){
				postamble(dvifile);
				break;	/* font_def agreement */
      			}
			if(!betweenpages(dvifile, PRESCAN_ON))
				break;
    		}
	}

	/* Initialize the printer */
	fprintf(bitfile, PCL4_INIT_PRINTER);
	fprintf(bitfile, PCL4_COPIES, numcopies);
	fprintf(bitfile, PCL4_RESOLUTION, resolution);
	fprintf(bitfile, PCL5_PRINT_DIRECTION, 0);

	/* Download the defined fonts */
	cachefonts(bitfile, resfile, maxdown, device);

	/* Now do the main work */
	if(twosided)
		doodevpages(bitfile, dvifile, pcllevel, outreverse,
		  resolution, device);
	else
		doallpages(bitfile, dvifile, pcllevel, outreverse,
		  resolution, device);
	fprintf(bitfile, PCL4_CLEAN_PRINTER);

#ifdef TIMING
                ftime(&timebuffer);
                time = ((timebuffer.time) + (timebuffer.millitm)/1000.0)
                    -start_time;
                fprintf(stderr,"Time of complete run:		%.3lf sec,  %.3lf sec/page  (%.2lf ppm)\n",
		time, time/actualpagecount, (double)(actualpagecount*60)/time);
#endif /* TIMING */


	if(verbose){
		fprintf(stderr,"%s done.\n",argv[0]);
		if((usgfile = fopen(usgname,"w")) == NULL) {
			fprintf(stderr,"Cannot open the output file %s\n", usgname);
			exit(-1);
		}
		diagnostics(usgfile);
	}

	exit(0);
}
   
/*
 * Error and die routines.
 * Print error messages and exits.
 */

prerror(s)
char *s;
{
	fprintf(stderr,s);
	fprintf(stderr,fatal_error);
	fflush(stderr);
	exit(-1);
}

void sterror(s, st)
char *s;
char *st;
{
	fprintf(stderr,s, st);
	fprintf(stderr,fatal_error);
	fflush(stderr);
	exit(-1);
}

valerror(s,v)
char *s;
int  v;
{
	fprintf(stderr,s,v);
	fprintf(stderr,fatal_error);
	fflush(stderr);
	exit(-1);
}

valvalerror(s,u,v)
char *s;
int u,v;
{
	fprintf(stderr,s,u,v);
	fprintf(stderr,fatal_error);
	fflush(stderr);
	exit(-1);
}
