#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] = "@(#) lprset.c V1.1 Copyright Julian H. Stacey 1988\n" ;

/* By Julian H. Stacey.
* - Generates escape sequences to swop the epson compatible printer to a
* 	different national character set.
* Open serial printer. 
* 
* brother serial printer must have dil 1-7 on (ie dc flow control, not ^s ^q
* written for BSD Symmetric,
* invoked like this:	lprset 49 > /dev/lps
*/
#include	<stdio.h>

#ifdef	unix
#define	PRINTER	"/dev/lpr"
#endif
#ifdef	MSDOS
#define	PRINTER	"AUX:"
#endif

char complain[] = "%s: must have one positive integer (country code).\n";

char **ARGV;

main(argc,argv)
	int argc; char **argv;
	{
	FILE *fil ;
	int national = 1 ;
	int code = 0 ;	/* exit code */

	ARGV = argv;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	fil = stdout;

	switch(argc)
		{
		case 1:
			printf("Setting %s to national phone code %d.\n",
				PRINTER, national ) ;
			if (freopen(PRINTER , "w", stdout) == (FILE *)0)
				{
				printf("Cannot write %s.\n",PRINTER);
				exit(1);
				}
			break ;
		case 2:
			if ( ( national = atoi(*++argv) ) < 0 )
				{
				fprintf(stderr,complain,*ARGV);
				exit(1);
				}
			break ;
		default:
			fprintf(stderr,complain,*ARGV);
			exit(1);
		}

	/* Clear print buffer, more than 1 cancel in case first few
		  get swallowed into a previous escape sequence 
	*/
	fwrite("\030\030\030\030",1,4,fil) ; /* 0x18 Cancel data in buffer */
	fwrite("\033@",1,2,fil) ;	/* Printer initialization */
	fwrite("\033<",1,2,fil) ;	/* Printer head Home positioning */
	fwrite("\033x\1",1,3,fil) ;	/* Set NLQ Print Mode		*/
	fwrite("\033O",1,2,fil) ;	/* Cancel Skip Perforation	*/
	/* fwrite("\033CF",1,3,fil) ;	/* Page Length Spacing No Of Lines */
	fwrite("\033P",1,2,fil) ;	/* Cancel Elite (set Pica) Size Chars */
/*
FORMAT		COMMAND				HEX	      DEC	   PAGE
ESC E		Emphasised Character Mode	1B 45	      27 69	      65
ESC F		Cancel Emphasised Char. Mode	1B 46	      27 70	      66
ESC G		Double Strike Character Mode	1B 47	      27 71	      67
ESC H		Cancel Double Stroke Char. Mode	1B 48	      27 72	      68
*/
	switch(national)
		{	/* set lpr to a national character set */
		case 1 :
			/* usa mode */
			fwrite("\033R\0",1,3,fil);
			break;
		case 44 :
			/* uk mode (same as usa, but pound for hash) */
			fwrite("\033R\3",1,3,fil);
			break;
		case 49 :
			/* german mode */ 
			fwrite("\033R\2",1,3,fil);
			break;
		default:
			fprintf(stderr,complain,*ARGV);
			code = 1 ;
			break;
		}
	/* hashed out 891128 why is it here? fwrite("      ",1,6,fil) ; */
	fclose(fil);
	exit(code);
	}
