#include <exec/types.h>
#include <stdio.h>
#include <exec/memory.h>
#include "libraries/dos.h"
#include "exec/exec.h"
#include "intuition/intuitionbase.h"
#include "devices/keymap.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include "drawicon.h"
struct IntuitionBase *IntuitionBase;
long IconBase;

#define STOP 0
#define GO 1
int StopFlag = GO;

#define OPEN 1
#define CLOSED 0
int PartFlag = CLOSED;

char line[100];

struct coord {
    float x;
    float y;
    struct coord *next_coord;
};

struct part {
    float minx;
    float maxx;
    float miny;
    float maxy;
    int colour;
    struct coord *first_coord;
    struct part *next_part;
};

char plot_name[100];
char draw_name[100];



void main(argc,argv)
int argc;
char *argv[];
{
float x,y;
float MinX, MinY, MaxX, MaxY;
FILE *fp1, *fp2;
struct part *Part, *FirstPart;
struct coord *Coord;
int FLAG,i,PartNumber;

  /*** PARSE ARGS ***/
MinX=MinY=MaxX=MaxY=0;
PartNumber=0;

  if ((argv[1][0] == '?')||(argc!=3))
     {
        printf("usage: Plot2Draw infile outfile\n");
        exit(0);
     }
   else
     {
       strcpy(plot_name,argv[1]);
       strcpy(draw_name,argv[2]);
     }

   /*** OPEN LIBRARIES ***/

   if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0))) {
      printf("Can't open intuition library...\n");
      exit(0);
   }
   if (!(IconBase = OpenLibrary("icon.library", 0))) {
      printf("Can't open icon library...\n");
      CloseLibrary(IntuitionBase);
      exit(0);
   }

   /*** GET DATA FROM FILE ***/

   fp1 = NULL;
   if (*plot_name) fp1 = fopen(plot_name,"r");
   if (fp1==NULL)
      {
         CloseLibrary(IntuitionBase);
         CloseLibrary(IconBase);
         printf("Input file does not exist.\n");
         exit(0);
      }
   fp2 = NULL;
   if (*draw_name) fp2 = fopen(draw_name,"w");
   if (fp2==NULL)
      {
         fclose(fp1);
         CloseLibrary(IntuitionBase);
         CloseLibrary(IconBase);
         printf("Unable to open Output File.\n");
         exit(0);
      }

  Part=(struct part *)malloc(sizeof(struct part));
  FirstPart=Part;
  Coord=(struct coord *)malloc(sizeof(struct coord));

while (StopFlag==GO) {
  if (fgets(line,100,fp1)==NULL)
    {
         Part->next_part=NULL;
         StopFlag=STOP;
    }
  else
    {
      FLAG = sscanf(line,"%f %f",&x,&y);
      if ((line[0]=='*')&&(PartFlag==OPEN))
         {
            if (line[1]=='C')                   /* Get the colour */
               {
                  PartFlag=CLOSED;
                  Part->colour=atoi(&line[3]);
                  MinX=min(MinX,Part->minx);
                  MaxX=max(MaxX,Part->maxx);
                  MinY=min(MinY,Part->miny);
                  MaxY=max(MaxY,Part->maxy);
                  Coord->next_coord=NULL;
                  Part->next_part=(struct part *)malloc(sizeof(struct part));
                  Part=Part->next_part;
               }
         }
      else if (FLAG>1)       /* Is it numbers? */
        {
            if (PartFlag==CLOSED)         /* A new part? */
               {
                   PartFlag=OPEN;
                   PartNumber++;
                   Part->first_coord=(struct coord *)malloc(sizeof(struct coord));
                   Coord=Part->first_coord;
                   Coord->x =x;
                   Coord->y =y;
                   Part->minx=x;
                   Part->maxx=x;
                   Part->miny=y;
                   Part->maxy=y;
               }
            else                           /* Continuing a part */
              {
                   Coord->next_coord=(struct coord *)malloc(sizeof(struct coord));
                   Coord=Coord->next_coord;
                   Coord->x =x;
                   Coord->y =y;
                   Part->minx=min(Part->minx,x);
                   Part->maxx=max(Part->maxx,x);
                   Part->miny=min(Part->miny,y);
                   Part->maxy=max(Part->maxy,y);
              }
         }
      else if (FLAG==EOF);   /* blank line reached */
    }
 }

  fprintf(fp2,"81086 %f %f %f %f 0 1.00000 \"%s\"\n-1\n",MinX,MinY,MaxX,MaxY,draw_name);
  Part=FirstPart;
  for (i=0;i<PartNumber;i++)
    {
       if (Part->first_coord!=NULL)
         {
            fprintf(fp2,"1 52 %f %f %f %f %d 0 0 0 0 \n",Part->minx,Part->miny,Part->maxx,Part->maxy,Part->colour);
            Coord=Part->first_coord;

            while (Coord!=NULL)
              {
                 fprintf(fp2,"       1 %f %f\n",Coord->x,Coord->y);
                 Coord=Coord->next_coord;
              }
            fprintf(fp2,"       0\n");
         }
      Part=Part->next_part;
    }
  fprintf(fp2,"-1\n");

  fclose(fp1);
  fclose(fp2);

  PutDiskObject(draw_name,&IconDiskObject);

  CloseLibrary(IntuitionBase);
  CloseLibrary(IconBase);

  /* Note: I'm relying on the Lattice compiler to clean up allocated
     memory. Not nice, but saves on file size!    */
}

