#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;
struct IconBase *IconBase;

int FLAG;
char line[100];
double x;
double minx;
double Minx;
double maxx;
double Maxx;
double y;
double miny;
double Miny;
double maxy;
double Maxy;

struct coord {
  int          number;
  double        across;
  double       up;
  struct coord *next;
};

typedef struct coord ELEMENT;
typedef ELEMENT *LINK;
LINK head = NULL;
LINK place = NULL;

struct part {
  int partnumber;
  int from;
  int to;
  double minx;
  double maxx;
  double miny;
  double maxy;
  struct part *next;
  struct part *last;
};

typedef struct part PART;
typedef PART *PPART;
PPART firstpart =NULL;
PPART thispart = NULL;
PPART lastpart = NULL;


int i=0;
int k=1;
int j,l;

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



void main(argc,argv)
int argc;
char *argv[];
{

FILE *fp1, *fp2;
LINK getplace();
PPART getpart();

  /*** PARSE ARGS ***/

  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 = 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)
      {
         printf("Input file does not exist.\n");
         exit(0);
      }
   fp2 = NULL;
   if (*draw_name) fp2 = fopen(draw_name,"w");
   if (fp2==NULL)
      {
         printf("Unable to open Output File.\n");
         exit(0);
      }


   do { fgets(line,100,fp1); }
       while (sscanf(line,"%lf %lf",&x,&y)==NULL);
   Minx=Maxx=minx=maxx=x;
   Miny=Maxy=miny=maxy=y;
   head =(LINK)malloc(sizeof(ELEMENT));
   head->number=i++;
   head->across=x;
   head->up=y;
   head->next=(LINK)malloc(sizeof(ELEMENT));
   place=head->next;
    do {
          fgets(line,100,fp1);
          FLAG=sscanf(line,"%lf %lf",&x,&y);
          if ((FLAG!=NULL)&&(FLAG!=EOF))
            {
              if (x<minx) minx=x;
              if (x<Minx) Minx=x;
              if (x>maxx) maxx=x;
              if (x>Maxx) Maxx=x;
              if (y<miny) miny=y;
              if (y<Miny) Miny=y;
              if (y>maxy) maxy=y;
              if (y>Maxy) Maxy=y;
              place->number=i++;
              place->across=x;
              place->up=y;
              place->next=(LINK)malloc(sizeof(ELEMENT));
              place=place->next;
            }
       } while ((FLAG!=NULL)&&(FLAG!=EOF));
   firstpart =(PPART)malloc(sizeof(PART));
   firstpart->last=NULL;
   firstpart->partnumber=k++;
   firstpart->from=0;
   firstpart->to=i;
   firstpart->minx=minx;
   firstpart->miny=miny;
   firstpart->maxx=maxx;
   firstpart->maxy=maxy;
   firstpart->next =(PPART)malloc(sizeof(PART));
   firstpart->next->last=firstpart;
   thispart=firstpart->next;

   while (FLAG!=EOF) {

     do { fgets(line,100,fp1); }
         while (sscanf(line,"%lf %lf",&x,&y)==NULL);
     minx=maxx=x;
     miny=maxy=y;
     place->number=i++;
     place->across=x;
     place->up=y;
     place->next=(LINK)malloc(sizeof(ELEMENT));
     place=place->next;

      do {
          fgets(line,100,fp1);
          FLAG=sscanf(line,"%lf %lf",&x,&y);
          if ((FLAG!=EOF)&&(FLAG!=NULL))
            {
            if (x<minx) minx=x;
            if (minx<Minx) Minx=minx;
            if (x>maxx) maxx=x;
            if (maxx>Maxx) Maxx=maxx;
            if (y<miny) miny=y;
            if (miny<Miny) Miny=miny;
            if (y>maxy) maxy=y;
            if (maxy>Maxy) Maxy=maxy;
            place->number=i++;
            place->across=x;
            place->up=y;
            place->next=(LINK)malloc(sizeof(ELEMENT));
            place=place->next;
          }

       } while ((FLAG!=NULL)&&(FLAG!=EOF));
     thispart->partnumber=k++;
     thispart->from=1+thispart->last->to;
     thispart->to=i;
     thispart->minx=minx;
     thispart->miny=miny;
     thispart->maxx=maxx;
     thispart->maxy=maxy;
     thispart->next =(PPART)malloc(sizeof(PART));
     thispart->next->last=thispart;
     thispart=thispart->next;
  }
  fprintf(fp2,"81086 %f %f %f %f 0 1.00000 \"%s\"\n",Minx,Miny,Maxx,Maxy,draw_name);
  fprintf(fp2,"-1\n");
  for (l=1; l<k; l++)
     {
        thispart=getpart(firstpart,l);
        fprintf(fp2,"1 52 %f %f %f %f 4 0 0 0 0 \n",thispart->minx,thispart->miny,thispart->maxx,thispart->maxy);
        for (j=thispart->from; j<thispart->to; j++)
          {
             place=getplace(head,j);
             fprintf(fp2,"       1 %f %f\n",place->across,place->up);
          }
        fprintf(fp2,"       0\n");
     }
  fprintf(fp2,"-1\n");

  fclose(fp1);
  fclose(fp2);
  PutDiskObject(draw_name,&IconDiskObject);

   CloseLibrary(IntuitionBase);
   CloseLibrary(IconBase);

}


LINK getplace(place,x)
LINK place;
int x;
{
    while (place->number<x)
    place=place->next;
    return(place);
}


PPART getpart(part,x)
PPART part;
int x;
{
    while (part->partnumber<x)
    part=part->next;
    return(part);
}

