/* $Log:	getoptions.c,v $
 * Revision 0.8  92/11/23  19:46:46  19:46:46  bt (Bo Thide')
 * Fixed resolution bug. Portable downloading. Added/changed options. PJXL color support
 * 
 * Revision 0.7  92/11/13  02:41:30  02:41:30  bt (Bo Thide')
 * More bug fixes and improvements. Support for PaintJet XL
 * 
 * Revision 0.6  92/11/10  21:47:47  21:47:47  bt (Bo Thide')
 * Bug fixes. Added -R option. Better font handling.
 * 
 * Revision 0.5  92/11/09  16:25:34  16:25:34  bt (Bo Thide')
 * Rewrite of dospecial.c. Extended \special support
 * 
 * Revision 0.4  92/11/08  02:45:50  02:45:50  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:41  12:45:41  bt (Bo Thide')
 * Fixed 8 bit (dc font) support.
 * 
 * Revision 0.2  92/08/23  17:28:57  17:28:57  bt (Bo Thide')
 * Source cleaned up.  Changed certain function calls.  Removed globals.
 * 
 * Revision 0.1  92/08/22  23:58:47  23:58:47  bt (Bo Thide')
 * First Release.
 *  */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "globals.h"

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

/*
 * Put the selected options into global variables
*/

getoptions(argc, argv, envp, bitname, dviname, usgname,
   pcllevel, numcopies, device, maxdown, outreverse, resolution, twosided)
int	argc;
char	**argv;
char	**envp;
char    bitname[];
char    dviname[];
char    usgname[];
short	*pcllevel;
short	*numcopies;
short	*device;
short	*maxdown;
bool	*outreverse;
short	*resolution;
short	*twosided;
{ 
  	char		buffer[1024];
  	char		*bufptr;
  	char		*strptr;
	int		k;
	int		c, errflag=0;
	extern char	*optarg;
	extern int	optind;

	strcpy(bitname,"");
	verbose = FALSE;

	while ((c = getopt(argc, argv, "12345c:d:f:lmo:p:rR:s:tvx:X:y:?")) != EOF)

	switch (c) {
	case '1':			/* PCL level 1 */
	case '2':			/* PCL level 2 */
	case '3':			/* PCL level 3 */
	case '4':			/* PCL level 4 */
	case '5':			/* PCL level 5 */
		*pcllevel = -(short)atoi(argv[optind-1]);
		break;
	case 'c':			/* Number of copies of each page */
		if((*numcopies = (short)atoi(optarg)) <= 0)
			*numcopies = 1;
		break;
	case 'd':			/* Specify output device */
		if(strcmp(optarg,"lj+") == 0)
		  *device = DEV_LJPLUS;
		else if (strcmp(optarg,"lj2") == 0)
		  *device = DEV_LJ2;
		else if (strcmp(optarg,"lj2d") == 0)
		  *device = DEV_LJ2D;
		else if (strcmp(optarg,"lj2p") == 0)
		  *device = DEV_LJ2P;
		else if (strcmp(optarg,"lj3si") == 0){
		  *device = DEV_LJ3SI;
		  *pcllevel = 5;
		}
		else if (strcmp(optarg,"lj3p") == 0){
		  *device = DEV_LJ3P;
		  *pcllevel = 5;
		}
		else if (strcmp(optarg,"pj") == 0)
		  *device = DEV_PJ;
		else if (strcmp(optarg,"pjxl") == 0)
		  *device = DEV_PJXL;
		break;
	case 'f':			/* Max number of downloadable fonts */
		
		if((*maxdown = (short)atoi(optarg)) < 0)
			*maxdown = 32;
		break;
	case 'l':			/* Landscape mode flag */
		landscape = TRUE;
		break;
	case 'm':			/* Manual feed flag */
		manualfeed = TRUE;
		break;
	case 'o':			/* Output file name */
		strcpy(bitname, optarg);
		break;
	case 'p':			/* Max number of output pages */
		if((maxpages = atoi(optarg)) <= 0)
			maxpages = MAXPAGES;
		break;
	case 'r':			/* Reverse output order flag */
		*outreverse *= *outreverse;
		break;	
	case 'R':			/* Set PCL resolution */
		*resolution = (short)atoi(optarg);
		break;	
	case 's':			/* Start page */
		startvals = 0;
		startthere[0] = FALSE;
        	strcpy(buffer,optarg);
        	bufptr = buffer;
		k = 0;
		do { 
			if(*bufptr == '*') { 
				startthere[k] = FALSE;
				bufptr++;
			}
			else { 
				startthere[k] = TRUE;
				startcount[k] = (long)getcount(bufptr);
			}

			if((k<9) && (*bufptr == '.')) { 
				k++;
				bufptr++;
			}
			else 
				if((*bufptr==' ')||(*bufptr=='\0'))
					startvals = k;
			else { 
				startvals = 0;
				k = 0;
			}
		} while(startvals != k);
		break;
	case 't':			/* Two-sided mode flag */
		*twosided = TRUE;
		break;
	case 'v':			/* Verbose mode flag */
		verbose = TRUE;
		break;
	case 'x':			/* Left offset for odd sides */
		h_offset = dim_to_dots(optarg, resolution);
		break;
	case 'X':			/* Left offset for even sides */
		r_offset = dim_to_dots(optarg, resolution);
		break;
	case 'y':			/* Vertical top offset */
		v_offset = dim_to_dots(optarg, resolution);
		break;
	case '?':
		errflag++;
		break;
	default:
		break;
	}

	if (errflag) {
		fprintf(stderr, "\nUsage: %s [-12345lmrt] [-c <copies>] [-f <fonts>] [-o <outfile>]\n       [-p <pages>] [-R resolution] [-s <startpage>] [-x <offset>]\n       [-X <offset>] [-y <offset>] [<infile>]\n", argv[0]);
		exit(2);
	}

	/*
 	 * Allow for input file names of type
	 * xx.dvi, xx.yy.dvi, xx, or xx.yy
	 * Append ".dvi" if it's not already there.
	 *
	 * Get the .dvi file name from the input line
	 */
	strcpy(dviname, argv[optind]);
	if((strstr(dviname, DVIFILE_SUFFIX)) == NULL)
		strcat(dviname, DVIFILE_SUFFIX);

	/*
  	 * Allow for output file names of type;
 	 * xx.pcl, xx.yy.pcl, xx, or xx.yy
 	 * Append ".pcl" if it's not already there.
	 *
	 * The .dvi file name was input via the -o option.
	if((strstr(bitname, BITFILE_SUFFIX)) == NULL)
		strcat(bitname, BITFILE_SUFFIX);
 	 */

	if(verbose) { 
		strcpy(usgname, dviname);
		strcat(usgname, USGFILE_SUFFIX);
	}

	if(*twosided) { 
		if(!startvals && !(startcount[0] % 2)) { 
			if(startthere[0])
				startcount[0]--;
			else { 
				startthere[0] = TRUE;
				startcount[0] = 1;
			}
		}
		if(maxpages % 2)
			maxpages++;
		o_offset = h_offset;
		e_offset = r_offset;
	}

	if(verbose) { 
		fprintf(stderr,"Options selected:\n");
		fprintf(stderr,"Starting page:	 ");
		for(k=0; k<=startvals; k++) { 
			if(startthere[k])
				fprintf(stderr,"%d",startcount[k]);
			else
				fprintf(stderr,"*");
			if(k<startvals)
				fprintf(stderr,".");
			else
				fprintf(stderr,"\n");
		}
		fprintf(stderr,"Maximum numbers of pages:	%d\n",maxpages);
		fprintf(stderr,"Number of copies:		%d\n",*numcopies);
		if(*twosided)
			fprintf(stderr,"Two-sided printing\n");
		if(manualfeed)
			fprintf(stderr,"Feed pages maually\n");
	}
}