/*
 * hpgl2ps.c
 *
 */
 
#include    "defn.h"
#define	VERSION  "3.5"

#ifdef VAXC
#define USAGE "Usage: hpgl2ps [-a orig-plot-size] [-demprw] [-l line sizes] [-s scale] [-x offset] [-y offset] [-o output_file] [file]\n"
#else
#define USAGE "Usage: hpgl2ps [-a orig-plot-size] [-Demprw] [-l line sizes] [-s scale] [-x offset] [-y offset] [-o output_file] [file]\n"
#endif

main(argc, argv)
int     argc;
char    **argv[];
{
    extern int optind;
    extern char *optarg;

    int    op;
    int    opt;
   
  /*
   *  program version
   */
    Mode = "HPGL";
  /*
   *  flag for piece of code which must be executed only ones
   */
    ONE_TIME = TRUE;

  /* *******************************************************************
   * Here are default values for program variables which can be changed
   * by options from program invocation command line.
   * *******************************************************************
   */
    DEBUG = FALSE;			/* d,D option */

    PaperSize = "A4";			/* -a option make sure manual entry
					    reflects this default */

    ENCAPSULATED = FALSE;		/* e option		*/

    pen_size[0] = 0.1;			/* l option		*/
    pen_size[1] = 0.25;
    pen_size[2] = 0.35;
    pen_size[3] = 0.5;
    pen_size[4] = 0.7;
    pen_size[5] = 1.0;
    pen_size[6] = 1.25;
    pen_size[7] = 1.5;

    MANUAL_FEED = FALSE;		/* m option		*/

    PROP_SPACE = FALSE;			/* p option		*/
    chWfact = 2.5;		        /* factors for non-prop. chars */
    chHfact = 1.68;                     /* probably best match for Courier */

    LANDSCAPE = TRUE;			/* r option		*/

    SCALE = 1.0;                        /* s option		*/

    A4_OUT = TRUE;     			/* w option		*/

    xprzes = yprzes = 0.0;		/* x, y options 	*/

    outfile = stdout;                	/* o option		*/
/*
 * *********************************************************************
 */

#ifdef VAXC
    while ((opt = getopt(argc, argv, "deva:l:mwps:x:y:o:r")) != EOF)
#else
    while ((opt = getopt(argc, argv, "Deva:l:mwps:x:y:o:r")) != EOF)
#endif
    {
	switch (opt)
	{
	case 'D':
	case 'd':
	    DEBUG = TRUE;
	    break;

        case 'a':
	    strcpy(PaperSize, optarg);
	    PaperSize[0] = toupper(PaperSize[0]);
	    if ((strcmp(PaperSize, "A4") != 0) && (strcmp(PaperSize, "A3") != 0))
		PaperSize = "A4";
            break;

	case 'l':
	    changesizes(optarg);
	    break;

	/* case 'm':
	    MANUAL_FEED = TRUE;
	    break; */

	case 'p':
	    PROP_SPACE = TRUE;
	    chWfact = 2.0;
	    chHfact = 1.45;
	    break;

	case 'r':
	    LANDSCAPE = FALSE;
	    break;

	case 's':
	    SCALE = (float)atof(optarg);
	    if (SCALE < 0.1)
		SCALE = 0.1;
	    else
	    if (SCALE > 3)
		SCALE = 3;
	    break;

        case 'w':
            A4_OUT = FALSE;       /* output on A3 Laser Printer's paper size */
	    break;

	case 'x':
	    xprzes = (float)atof(optarg);
	    break;

	case 'y':
	    yprzes = (float)atof(optarg);
	    break;

        case 'o':
	    if ((outfile = fopen(optarg, "w")) == NULL)
	       {
		fprintf(stderr, "ERROR: cannot open \"%s\"\n", optarg);
		exit(1);
               }
	    break;
	
	case 'v':
	    fprintf(stderr,"Hpgl2ps Version: %s\n", VERSION);
	    fprintf(stderr, "%s\n", USAGE);
	    exit(0);
	    break;

	case 'e':
	    ENCAPSULATED = TRUE;
	    break;

	default:
	    fprintf(stderr, "%s\n", USAGE);
	    exit(1);
	}
    }
    if (optind == argc)
	stream = stdin;
    else
    if ((stream = fopen((char *)argv[optind], "r")) == NULL)
    {
	fprintf(stderr, "ERROR: cannot open \"%s\"\n", argv[optind]);
	exit(1);
    }
/*
 * Set up default P1 and P2 coordinates
 */
    if (strcmp(PaperSize, "A3") == 0)
    {
        defP1x = 170;
	defP1y = 602;
	defP2x = 15370;
	defP2y = 10602;
     
        rotP1x = 607;
	rotP1y = 797;
	rotP2x = 10607;
	rotP2y = 15997;
    } else				/* PaperSize must be A4 */
    {
	defP1x = 603;
	defP1y = 521;
	defP2x = 10603;
	defP2y = 7721;

	rotP1x = 0;
	rotP1y = 610;
	rotP2x = 7200;
	rotP2y = 10610;
    }

/*
 *  setup default P1, P2: (P1x, P1y, P2x, P2y, U1x, U1y, U2x, U2y, maxprX,
 *                         minprX, maxprY, minprY) 
 */
    plotcoords();
    
/*
 *  initialisation of PS macros;  setup of remaining flags;
 *  setup of some default variables based on P1, P2;
 *  establish default line type, character angle & slant, ticks length, 
 *  PEN_TICKNESS, FILL pattern, EOL terminator, rotation state;
 */
    plotinit();	

/*
 *  send PostScript macros to output
 */
    if (!ENCAPSULATED){
	ps_macros();
	fprintf(outfile, "%% ----- here is end of PostScript macros -----\n\n");

/*
 *  compute all scales and send ONE_TIME PostScript code to output;
 *  Scale the viewport for the plot;
 *  XSCALE_ABS, YSCALE_ABS, XSCALE, YSCALE are established here; 
 */
	fprintf(outfile, "%% ----- default/input parameters  -----\n\n");
    }
    viewport();


    if (MANUAL_FEED)
	manualfeed(1);

/*
 *  get proportional or non-proportional font;
 *  set char_width, char_height, char_space based on U1, U2 and XSCALE;
 */
    selectfont();	

    fprintf(outfile, "\n%% ----- here is beginning of source program interpretation -----\n\n");

/* *************************************************************************
 * main loop
 */
    while ((op = getc(stream)) != EOF)
       {
	if (isalpha(op) > 0)
	    hpglcom(op);		/* Only alpha characters accepted */
        else if (iscntrl(op) > 0)
	    filtr(op);			/* Test for control characters */
       }	 
/*
 * **************************************************************************
 */
    end_draw();

    fprintf(outfile, "showpage\n");

    if (MANUAL_FEED)
	manualfeed(0);
}
