/*PROGRAM:  mim2hp.c                                                        */
/*==========================================================================*/
/*This program is designed to read a MIM and to output a HP GL file.        */
/*It assumes that the MIM is in a format matching the MIM specification,    */
/*dated November 1993.                                                      */
/*==========================================================================*/
/*VERSION:  27 April 1995                                                   */
/* This routine is largly based on mim2ps.c (mim to postscript) written by: */
/*STAFF: F.R.Broome, Leslie Godwin, Les Libelo, Fred Malkus, and Lisa Nyman */
/*       U.S. Department of Commerce, Census Bureau, Geography Division     */
/*                                                                          */
/* Any modifications done for the purposes of supporting HPGL were made by  */
/* Sheeja Malaveetil - U.S. Dept. of Commerce, Census Bureau, Geography Div.*/
/*==========================================================================*/
/* This program, like mim2ps,:                                              */
/*  Does not process some commands.                                         */
/*  Does ignore entity status of Xref and Xdel                              */
/*                                                                          */
/* HISTORY:                                                                 */ 
/*        09/21/95 - modifed MIM2HPTRANS.C so that line attributes are butt */
/*                   ends, mitred/BEVELED joins, and mitre limit is the     */
/*                   device dependent default.                              */
/*        11/07/95 - sm - modified mim2hpTrans.c & mobTexthp.c for Raster   */
/* 		     Graphics. 0.005 is added to all line weights in an     */
/* 		     attempt to eliminate gaps in lines 		    */
/*          11/27/95 - sm - modified so default output device is a plotter  */
/*                      not a laser. Laser must be specified via -d         */
/*          11/28/95 - sm - modified so that the fontpath qualifier is      */
/*                     interpreted. This will specify what directories the */
/*                     files will be at. Default will be current directory. */
/*          12/05/95 -sm - modifed mim2hpTrans.c & patternSethp.c for Raster*/
/*                      Graphics Devices:                                   */
/*                       mim2hpTrans.c - modified so that Lines have rounded*/
/*                           ends by default (see LA1) in "cmt" code.  This */
/*                           was done to cover up the Raster Graphics       */
/*                           limitation of no line joins                    */
/*                       patternSethp.c -  To eliminate gaps in lines less  */
/*                        than .005 inches in width for the Raster Graphics*/
/*                        devices, modified so that all fills that are     */ 
/*                        defined by all 1s (solid fills) will be defined  */
/*                        and set using the defaults:                      */
/*                        TYPE    CURRENT PATTERN DEFINITION  PREVIOUS   */
/*                        Line    RF;                         RF8,<bits> */
/*                        Poly    RF;                         RF7,<bits> */
/*                                                                       */
/*                        TYPE    CURRENT PATTERN SET         PREVIOUS   */
/*                        Line    SV;                         SV2,8,0;   */
/*                        Poly    FT;                         FT11,7,0;  */
/*          12/06/95 - sm - modified so that the -d flag will take       */
/*                        the argument "rg" meaning that the output device */
/*                        is a Raster Graphics plotter. By default, the    */
/*                        format flag will be interpreted as pur HPGL/2.   */
/*                        This flag was added because the Raster Graphics  */
/*                        interprets the number of copies as additional    */
/*                        copies not as total number of copies.            */
/*          12/11/95 - sm - modified mim2hpTrans.c, mimDPAhp.c,            */
/*                        patternSethp.c, and mim2hp.c                     */
/*                        for the PC version. This included removing       */
/*                        variables that were not used from the files as   */
/*                        well as removing a library "wctype.h" from a file.*/
/*
            03/05/96 - sm -
               1. Reduced the number of arguments sent to patternSethp.c 
                  in all subroutines that called it.
               2. Added the ability to run mim2hp with the -TEST (-NOTEST) or
                  -DEBUG (-NODEBUG) flags so for debugging purposes, the code
                  will not have to be modified and recompiled.
               3. In mim2hpll.c, modified so that MIMpatternSearch will 
                  return the number of bits per row in the pattern.
               4. In patternSethp.c, removed the "RF;" that sets the solid 
                  fill for lines and polygons when a solid default is 
                  requested. Handled solid defaulting by using "SV;" and "FT;"                  which will fill with a solid pattern no mattern what "RF;"
                 says. Also did not write "solid" to the currentPolyPattern or                  the currentLinePattern variables.
               5. In mimStrhp.c, added semi-colons to the end of all "PU" statements.
               6. Changed version number to Ver.96.03.05.
               7. Added "-stat" qualifier to output mim2hp status messages to
                         the screen 
          03/20/96 - sm -
               1. Changed Version number to 96.03.20.
               2. Added *cmt Param_MC and *cmt Param_TR for testing firmware
                         fix 3.2
               3. Changed default line join to beveled (2,5) instead of mitred
                         beveled(2,2)
          03/25/96 - sm - modified mim2hpTrans.c so that the "*cmt Param_SPANISH" is 
                         interpreted. Also added the "*cmt" if checks.
*/
/* HISTORY:                                                     */
/*          04/24/96 - Sheeja did the following to some routines*/
/*                        1. added #include "parseTokens.h"     */
/*                        2. deleted prototype for parseTokens  */
/*                        3. deleted declarations for arrays    */
/*                            tokenStrings & tokenStrPtrs       */
/*                     This was done because parseTokens.h now  */
/*                     contains the declarations as extern and  */
/*                     includes the prototype                   */
/*                                                              */
/* ========================================================================== */

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>

#define PU 1016
#define DPI 300
#define DEBUG 0

/* MIM Library Global variables. ----------------------------------- */
int            ROTATEFLAG = 0;       /* Default to no rotation. */
int            COPIESFLAG = -1;       /* Default to no copies.   */
int 	       DEBUG_FLAG;
int            TEST_FLAG = 0; 
char           fcs[32], fcp[32], fcv[32],  /* Fill color      */
                        fpp[32], fpv[32],  /* Fill pattern    */
               lcs[32], lcp[32], lcv[32],  /* Line color      */
               lps[32], lpp[32], lpv[32],  /* Line pattern    */
               lts[32], ltp[32], ltv[32],  /* Line type       */
               lws[32], lwp[32], lwv[32];  /* Line weight     */

char           fontFill[4], fontKerning[4];
float          fontSlope,   fontSpacing,   fontWidth;
float          mszXsize, mszYsize;          /* msz MIM size.   */
int            mszResolution;               /* msz resolution  */

int            firstPatternFlag = 0;     /* Initialize to none.*/
float          deviceFactor = 1.0;       /* Initial value.     */
float          relArrayX[128], relArrayY[128], offsetX, offsetY;
int            befDepth = 0;
float          ltsValues[64];
int            ltsNumVals = 0;

int 	       pu_i = 0;		/*sm: in int: Plotter units/inch from argument list */
int 	       dpi_i = 0; 		/*sm: in int: Dots per inch (DPI) from agrument list */
int	       pcl_flag = 0;		/*sm: Output format flag from argument list, if "1"
					     will include PCL commands also, else pure HPGL/2 */
int            pageSizeFlag = 0;    /* Set to force to page size. */ 
int            convertSpanishFlag = 0;/* Set to no-conversion.      */
int	       plot_flag = 1;      /*sm: from command line: =1 if output device is plotter */
int            rg_flag = 0; /* used to subtract 1 from number of copies if raster graphics */ 
char	       ch_fontpath[80]="";     /*sm: From arg list: contains directory path containing 
                                             font files. Default is current directory */
/* Prototypes */
int mim2hpTrans(void);

/*
char            *versionNum = "95.04.27";
char            *versionNum = "95.07.12";
char            *versionNum = "95.08.16";
char            *versionNum = "95.08.28";
char            *versionNum = "95.09.13";
char            *versionNum = "95.09.21";
char            *versionNum = "95.10.31";
char            *versionNum = "95.11.07";
char            *versionNum = "95.11.27";
char            *versionNum = "95.12.05";
char            *versionNum = "95.12.06";
char            *versionNum = "95.12.11";
char            versionNum[9] = "96.03.08";
char            versionNum[9] = "96.03.20";
char            versionNum[9] = "96.03.25";
*/
char            versionNum[9] = "96.04.24";

float           pointsFactor;          /* Inches to points factor.  */
FILE            *hpfile;               /* Pointer to output hp file.*/
FILE            *mimfile;              /* Pointer to input MIM file.*/
char            mimfilename[127];      /* Input MIM file name.      */
float           adjscale = 1.0;        /* Default value.            */
float           BoundingBoxLeft, BoundingBoxBottom;
char            currentFont[64],       /* Holder for font and FOB   */
                FOB = 'N';
long            currentEntity=0; /*Attributes active this ent.  type*/
char            currentColor[32],       /* Active HPGL/2   */
                currentLineWeight[32],  /* graphics attributes */
		currentLineType[32],	/* states.            */
                currentLinePattern[32],
                currentTextPattern[32],
                currentPolyPattern[32];
char            FOBpgX = 'N';           /* Current FOB pgX flag.     */

/* MAIN PROGRAM */
int main(int argc, char  *argv[])
{
extern char           fcs[32], fcp[32], fcv[32],  /* Fill color      */
                               fpp[32], fpv[32],  /* Fill pattern    */
                      lcs[32], lcp[32], lcv[32],  /* Line color      */
                      lps[32], lpp[32], lpv[32],  /* Line pattern    */
                      lts[32], ltp[32], ltv[32],  /* Line type       */
                      lws[32], lwp[32], lwv[32]; /* Line weight     */

extern char     currentColor[32],       /* Active HPGL/2   */
                currentLineWeight[32],  /* graphics attributes */
		currentLineType[32],	/* states.            */
                currentLinePattern[32],
                currentTextPattern[32],
                currentPolyPattern[32];

extern float    pointsFactor;
extern float    BoundingBoxLeft, BoundingBoxBottom;
extern int      befDepth;
extern char     mimfilename[127];      /* Input MIM file name.      */
extern FILE     *mimfile;              /* Pointer to input MIM file.*/
char            hpfilename[127];       /* Input HPGL file name*/
extern FILE     *hpfile;
extern float    adjscale;              /* Default value.            */
extern float    deviceFactor;
extern float    relArrayX[128], relArrayY[128], offsetX, offsetY;
extern char     ch_fontpath[80];           /*sm: Output font path */ 
extern char     versionNum[9];

char 		*pu_ch = " ";		/*sm: in char: Plotter units/inch from argument list */
char 		*dpi_ch = " "; 		/*sm: in char: Dots per inch (DPI) from agrument list */
char		*ch_form = " ";		/*sm: Output format flag from argument list, if "pcl"
					     will include PCL commands also, else just HPGL/2 */
char            *ch_dev = " ";         /*sm: in char: Output device specifier */
char            argString[128]="";     /* Argument text holder.     */       
char            adjustScale[16]="1.0"; /* Default value.            */
 
int             i, k = 0;           /* Working variable.         */
int             firstPointFlag = 0;    
int             moreFlag = 1;
int             helpFlag = 0;          /* If set, displays help.    */
int             adj_warn=0,mim_warn=0,hp_warn=0,font_flag=0,font_warn=0,dev_warn=0,
                output_warn=0,copy_warn=0;

/*.......... INITIALIZE .............*/ 
strcpy(mimfilename,""); 
strcpy(hpfilename, "");
DEBUG_FLAG = DEBUG;

/*......... GET COMMAND LINE ARGUMENTS .........*/
if( argc == 1 )
  helpFlag = 1;
else
{
  strcpy(argString, argv[1]);
  for( i = 1; i < strlen(argString); i++)
    *(argString+i) = toupper(*(argString+i));
  if((*argString == '?') || (strncmp(argString,"-HELP",5) == 0 ) && argc == 2 )
    helpFlag = 1;
}
if(helpFlag)
{
   printf("========================================================\n");
   printf(" MIM to HPGL/2 file converter.  Version %s\n",versionNum);
   printf("________________________________________________________\n");
   printf("Default argument sequence:\n");
   printf("     mim2hp  input_mim_name  output_hpgl_file_name\n");
   printf("To adjust the scale of the output image:\n");
   printf("     mim2hp  arg1 arg2 ... -adjust scaleFactor\n");
   printf("To allow Spanish characters to be generated on output:\n");
   printf("     mim2hp  arg1 arg2 ... -Spanish\n");
   printf("To rotate output image by 90 degrees (landscape output):\n");
   printf("     mim2hp  arg1 arg2 ... -rotate\n");
   printf("To make output to page size (DEFAULT scale = actual): \n");
   printf("     mim2hp  arg1 arg2 ... -page\n");
   printf("To set output format; =pcl if output has PCL & HPGL commands \n");
   printf("  (DEFAULT=hpgl): \n");
   printf("     mim2hp  arg1 arg2 ... -f pcl_or_hpgl\n");
   printf("To set the number of copies to be greater than 1 (DEFAULT=1): \n");
   printf("     mim2hp  arg1 arg2 ... -copies numberOfCopies\n");
   printf("To set the output device : laser=laser, rg=Raster Graphics Plotter,\n");
   printf("  plotter=plotter or plot (DEFAULT=plotter): \n");
   printf("     mim2hp  arg1 arg2 ... -d laser_or_rg_or_plotter \n");
   printf("To specify directory path for font file (DEFAULT=current directory):\n");
   printf("     mim2hp  arg1 arg2 ... -fp or -fontP <directory_path>\n");
   printf("           Ex. mim2hp test.mim test.hp -fp /home/mob/sheejam/ \n"); 
   printf("           NOTE: The last character, /, must be there.\n"); 
   printf("To request status messages on the screen: -stat (DEFAULT = no messages)\n");
   printf("     mim2hp  arg1 arg2 ... -stat \n");
   printf("\nFor example, to output a map rotated, with Spanish characters,\n");
   printf("and copies = 4, one would use these arguments:\n");
   printf("     mim2hp input_mim  output_HPGL -rotate -Spanish -copies 4\n");
   printf("----------------------------------------------------------\n"); 
   printf("----------------------------------------------------------\n");
   printf("                       DEVICE NOTES \n");
   printf("----------------------------------------------------------\n");
   printf(" The following is a list of arguments for specific output devices. \n");
   printf(" Feel free to use other arguments such as -copies and -rotate as well \n");
   printf(" as what is suggested below. \n");
   printf("    - Raster Graphics Color Plotter: \n");
   printf("         mim2hp arg1 arg2 -d rg \n");
   printf("               WHEN YOU USE -d rg YOU DO NOT HAVE TO SPECIFIY A PLOTTER\n");
   printf("    - Hewlett-Packard Plotter: \n");
   printf("         mim2hp arg1 arg2 -f pcl \n");
   printf("    - Hewlett-Packard Laser Jet: \n");
   printf("         mim2hp arg1 arg2 -d laser -f pcl \n");
   printf("    - Any other HPGL/2 plotter (there are no guarantees on this): \n");
   printf("         mim2hp arg1 arg2\n");
   printf("----------------------------------------------------------\n\n"); 
   exit(0);
}

if(argc > 2)
{
  /*Extract type of arguments and store values for later use.*/
   for(i = 1; i < argc; i++)
   {
      strcpy(argString, argv[i]);
      if(*argv[i] == '-')            /*See if arg-type flag.      */
      {
      /*See if a scale adjust arg. */
         if(strncmp(argString,"-adj", 4) == 0    ||
            strncmp(argString,"-aS",3) == 0      ||
            strncmp(argString,"-Adjust",7) == 0  ||
            strncmp(argString,"-as",3) == 0      ) 
         {
            i++;                           /*Advance pointer to value.  */
      /*See if a scale adjust arg. */
            if(i < argc)                  /*Be certain there is a value*/
            {
               strcpy(adjustScale, argv[i]);
               adjscale = atof(adjustScale);
               continue;
            } 
            else
            {
               adj_warn=1;
               continue;
            }
         }
      /*See if a mim file name arg. */
         if( strncmp(argString,"-MIM", 4) == 0      ||
            strncmp(argString,"-mim", 4) == 0 )
         {
            i++;                               /*Advance pointer to value.  */
            if((i < argc) && (*argv[i] != '-'))/*Be certain there is a value*/
            {
               strcpy(mimfilename, argv[i]);
               continue;
            } 
            else
            {
               mim_warn=1;
               continue;
            }
        }
      /* If arg = "page" then force hp file to page size map of 8.5 x   */         
      /* 11.0 inches (7.5 x 10 inches display area).  If this  flag is  */
      /* present, the image defined by the *msz-command will be fit     */
      /* to the 7.5 x 10.0 area.  If not present, the values in the     */
      /* *msz-command will be used to set the HPGL/2 BoundingBox.   */
        if(strncmp(argString,"-pag",4) == 0     ||
           strncmp(argString,"-Pag",4) == 0     ||
           strncmp(argString,"-PAG",4) == 0)
        {
           pageSizeFlag = 1;
           continue;
        }
      /*See if want to convert from census diacritical flags to ISO     */
      /* character values.  If argument not given assumes no conversion.*/
        if(strncmp(argString,"-Span",5) == 0     ||
           strncmp(argString,"-span",5) == 0     ||
           strncmp(argString,"-SPAN",5) == 0 ) 
        {
           convertSpanishFlag = 1;
           continue;
        }
      /*See if a mim file name arg. */
      /*See if a HPGL output file name arg. */
        if(strncmp(argString,"-HP", 3) == 0      ||
           strncmp(argString,"-hp", 3) == 0      ||
           strncmp(argString,"-hewlett", 11) == 0)
        {
           i++;                               /*Advance pointer to value.  */
           if((i < argc) && (*argv[i] != '-'))/*Be certain there is a value*/
           {
              strcpy(mimfilename, argv[i]);
              continue;
           } 
           else
           {
              hp_warn=1;
              continue;
           }
        }
      /*Check for ROTATEFLAG argument. */  
        if(strncmp(argString,"-rotate", 7) == 0  ||
           strncmp(argString,"-ROTATE", 7) == 0  ||
           strncmp(argString,"-Rotate", 7) == 0) 
        {
           ROTATEFLAG = 1;
           continue;
        }
      /*sm: 11/28/95 check to see if font file directory qualifier is specified */
        if(strncmp(argString,"-fp",3) == 0        ||
           strncmp(argString,"-fontpath",9) == 0  ||
           strncmp(argString,"-font",5) == 0)
        {
           i++;                           /*Advance pointer to value.  */
      /*sm: See if path name is specified */
           if(i < argc)                  /*Be certain there is a value*/
           {
              strcpy(ch_fontpath, argv[i]);
              strcat(ch_fontpath,"\0");
              font_flag=1;
              continue;
           }
           else
           {
              font_warn=1;
              continue;
           }
        }
      /*sm: See what device the output file will be going to. Either plotter or laser printer */
        if(strncmp(argString,"-d",2) == 0 )
        {
            i++;                           /*Advance pointer to value.  */
      /*sm: See if output device specified . */
            if(i < argc)                  /*Be certain there is a value*/
            {
               strcpy(ch_dev, argv[i]);
	       if(strncmp(ch_dev,"lase",4)== 0)
	       {
		  plot_flag = 0;
                  continue;
	       }
               else
               {
		  if((strncmp(ch_dev,"rg",2)== 0) || (strncmp(ch_dev,"RG",2)== 0))
                  {
		     rg_flag = 1;
                     plot_flag = 1;
                     continue;
                  }
               }
            }
            else
            {
               dev_warn=1;
               continue;
            }
	}
      /*sm: See what format the output file will be. Either PCL + HPGL/2 or just HPGL/2 */
        if( strncmp(argString,"-f",2) == 0   ||
            strncmp(argString,"-form",5)== 0)  
        {
           i++;                           /*Advance pointer to value.  */
      /*sm: See if format specified . */
           if(i < argc)                  /*Be certain there is a value*/
           {
              strcpy(ch_form, argv[i]);
	      if(strncmp(ch_form,"pcl",3)== 0)
	      {
		 pcl_flag = 1;
                 continue;
	      }
              else
                 if(strncmp(ch_form,"hp",2))
                 {
                    output_warn = 1;
                    continue;
                 }
           }
           else
           {
              output_warn = 1;
              continue;
           }
	}
      /*Check for a number of copies argument. */  
        if(strncmp(argString,"-copies", 7) == 0  ||
           strncmp(argString,"-COPIES", 7) == 0  ||
           strncmp(argString,"-Copies", 7) == 0) 
        {
           i++;                              /* Advance pointer to value. */
           if(i < argc )
           {
              strcpy(argString, argv[i]);
              if(argString[0] < '0'  ||  argString[0] > '9' )
              {
                 copy_warn=1;
                 continue;
              }
              else
              {
                 COPIESFLAG = atoi(argv[i]);  /* Get the value. */
                 continue;
              }
           }
        }
      /*Check for test flag argument. */  
        if(strncmp(argString,"-test",5) == 0)
        {
            TEST_FLAG = 1;
            DEBUG_FLAG = 1;
            continue;
        }
        if(strncmp(argString,"-notest",7) == 0)
        {
            TEST_FLAG = 0;
            continue;
        }
        if(strncmp(argString,"-stat",5) == 0) 
        {
            DEBUG_FLAG = 1;
            continue;
        }
        if(strncmp(argString,"-nostat",7) == 0) 
        {
            DEBUG_FLAG = 0;
            TEST_FLAG = 0;
            continue;
        }
      }
      else                        /*Assume arguments in proper sequence.  */
      {
         if(k == 0)
            strcpy(mimfilename, argv[i]);
         if(k == 1)
            strcpy(hpfilename, argv[i]);
         if(k == 2)
         {
            strcpy(argString, argv[i]);
            adjscale = atof(argString);
         }
         k++;
      } 
   }    /* End for-loop */
}
else
{
   if(DEBUG_FLAG)
   {
     printf("\n  ERROR: Either no MIM name passed or no HPGL\n");
     printf("name passed.  Not enough arguments.  Aborting run!\n\n");
   }
   exit(0);
}

/*.............. PARAMETER ADJUSTMENTS .........*/

if(pu_i == 0)
   pu_i = PU;
if(dpi_i == 0)
   dpi_i = DPI;
if((pcl_flag)&& (rg_flag))
   pcl_flag = 0; 
if(COPIESFLAG==0)
   COPIESFLAG = -1;
if(COPIESFLAG > 1)
{
   if(rg_flag)
      COPIESFLAG--;
}
else
   COPIESFLAG = -1;

/*................ OUTPUT SCREEN MESSAGES ........... */ 
if(DEBUG_FLAG)
{
   printf("========================================================\n");
   printf(" MIM to HPGL/2 file converter.  Version %s\n",versionNum);
   printf("________________________________________________________\n");

   if(DEBUG_FLAG)
   {
      printf("Working variables are:\n     MIM file name:        %s\n",mimfilename);
      printf("     HPGL/2 file name:     %s\n",hpfilename);
      printf("     Scale factor:        %7.4f\n\n",adjscale);
   }

   if(mim_warn)
   {
      printf("  ERROR: No MIM file name given, though indicated.\n");
      printf("    Ignoring request.  You could be in trouble.\n");
   }
   if(hp_warn)
   {
      printf("  ERROR: No HPGL file name given, though indicated.\n");
      printf("    Ignoring request.  You could be in trouble.\n");
   }
   if(dev_warn)
      printf("  WARNING: No output device selected, though indicated. Defaulting...\n");
   if(plot_flag)
   {
      if(rg_flag)
         printf("  Output device: Raster Graphics Plotter. Note: Plotter rotates as necessary.\n");
      else
         printf("  Default: Output device: plotter. Note: Plotter rotates as necessary.\n");
   }
   else
      printf("  Output device: laser printer. Rotatation must be specified.\n");

   if(output_warn)
      printf("  WARNING: Invalid output file format given.\n");
   if(pcl_flag)
      printf("  Output format: PCL + HPGL/2.\n");
   else 
      printf("  Output format: HPGL/2.\n");

   if(ROTATEFLAG)
      printf("  ROTATEFLAG requested via -rotate commmand\n");
   if(adj_warn)
   { 
      printf("  WARNING: No adjust scale value given, though indicated.\n");
      printf("    Ignoring request.\n");
   }
   if(convertSpanishFlag)
      printf("  Converting census diacritical flags to ISO Latin I values.\n");
   if(font_flag)
      printf("  Font file path = %s.\n",ch_fontpath);
   if(font_warn)
   {
      printf("  WARNING: No font file path given, though indicated.\n");
      printf("    Defaulting to current directory.\n");
   }
   if(copy_warn)
   {
      printf("  WARNING: Invalid value type following copies argument.\n");
      printf("    Default: Number of copies: 1.\n");
   }
   if(COPIESFLAG>1)
      printf("  Number of copies: %d.\n",COPIESFLAG);
}

/* ............ FILE CHECKS .............*/
if(strcmp(mimfilename,hpfilename)==0)
{
   if(DEBUG_FLAG)
   {
      printf("\n  ERROR: Input and Output files have the same name.\n");
      printf("  Danger of Overwriting. Aborting run!....\n\n");
   }
   exit(0);
}
if(strlen(mimfilename) < 1  || strlen(hpfilename) < 1)
{
   if(DEBUG_FLAG)
   {
      printf("\n  ERROR: Either no MIM name passed or no HPGL\n");
      printf("name passed.  Not enough arguments.  Aborting run!\n\n");
   }
   exit(0);
}
if((mimfile = fopen(mimfilename,"r")) == NULL)
{
   if(DEBUG_FLAG)
   {
     printf("\nFATAL ERROR: Cannot open input file:%s\n",mimfilename);
     printf("  Aborting run!.... . .  .\n\n");
   }
   exit(errno);
}
else
{
   if(DEBUG_FLAG)
      printf("Now processing MIM file %s\n",mimfilename);
} 
if((hpfile = fopen(hpfilename,"w+")) == NULL)
{
   if(DEBUG_FLAG)
   {
     printf("\nFATAL ERROR: Cannot open output file:%s\n",hpfilename);
     printf("  Aborting run!.... . .  .\n\n");
   }
   exit(errno);
}

/*-----------------------------------------------------------------*/
/*                NOW PROCESS MIM COMMAND RECORDS.                 */
/* Note:                                                           */
/* There is a fundamental difference between MIM and Postscript in */
/* the way entity (line, polygon, and text) attribute states are   */
/* set. Postscript settings for color, lineweight, and so forth are*/
/* effective for ALL successive entities drawn. MIM is more precise*/
/* in that it allows each entity type to have separate attribute   */
/* states, i.e., the color red can be active for lines (*str's) at */
/* the same time that blue is the active for text (*vtx's).   In   */
/* Postscript if red is the current color, it is so for all follow-*/
/* entities.  This means that MIM to Postscript translators must do*/
/* much more attribute state checking to assure the proper states  */
/* are set for the entity type being output.  This program does the*/
/* required checking.                                              */
/*                                                                 */

relArrayX[0] = 0.0;  relArrayY[0] = 0.0;     /*Set initial values. */
k = mim2hpTrans(); 
if(DEBUG_FLAG)
{
   printf("End of MIM file detected.\n");
   printf("Output HPGL/2 file:  %s\nPROGRAM COMPLETE\n\n",hpfilename);
}
return(0);
}