/* THIS PROGRAM WILL READ A MIM AND DETERMINE IF THE COORDINATES FOR
   PGX's ARE ORDER CLOCKWISE AND COUNTERCLOCKWISE.  BASED ON WHAT
   THE USER SPECIFIES FOR DESIRED ORDERING, THE PROGRAM WILL REORDER
   THE COORDINATES FOR THE PGX AND CREATE A NEW MIM.

   FORMAT : pgx_clock  mimname <outer cycle order> <inner cycle order>
   OUTPUT FILENAME:    mimname_c

      cc = counter clock-wise
      cw = clock-wise
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "mapfonts.h"
#include "parseTokens.h"

#define TRUE 1
#define FALSE 0

int  parseTokens();
void mimft_coord(FILE *inFile, char text[], float xa[], float ya[], long int npt [], long int *ncycle, float *minx, float *maxx, float *miny, float *maxy);
short int clockw(float xa[], float ya[], long int npts, long int *ptr);
void flip(float xa[], float ya[], long int npts, long int *ptr);
void mimft_coord_out_dec(FILE *outFile, char text_in[], float xa[], float ya[],long int npt[], long int ncycle);

main(argc, argv)

int	argc;
char	*argv[];
 
 {

  FILE *inFilePtr;
  FILE *outFilePtr;

  float xa[10000],ya[10000],xc,yc,xdim,ydim,read,fminx,fmaxx,fminy,fmaxy,fx,fy;
  long int ncycle, nxy[200];
  char  text[255], text2[255], tmp[255];
  char  outFileName[60],inner[2],outer[2];
  long int i, j, k, l, m, n, count=0,start=0;
  long int numToks, numrecs, numpolys, numpts;
  short int statusflag=0,order=0,optiono=0,optioni=0;
  int  debug = 0, debug2 = 0;
  long int *ptr;

if(argc < 4)
   {
   printf("<ERROR> Missing input parameters. \n");
   printf("<ERROR> Use format: pgx_clock <mimname> <outer cw/cc) <inner cw/cc>\n");
/* printf("<ERROR> Use format: pgx_clock <mimname> <outer cw/cc> <inner cw/cc>\n"); */
   printf("<ERROR> EXAMPLE: pgx_clock myfile cw cc\n");
   exit(1);
   }

/* DETERMINE DESIRED ORDERING FOR POLYGON CYCLES */
strncpy(outer,argv[2],2);  
strncpy(inner,argv[3],2);         
n=strlen(outer);              
for(i=0;i<n;i++)
    outer[i]=tolower(outer[i]); 
n=strlen(inner);                
for(i=0;i<n;i++)                
    inner[i]=tolower(inner[i]); 
if(strncmp(outer,"cw",2)==0){printf("Outer polygon cycles will be clockwise \n");optiono=1;}
if(strncmp(outer,"cc",2)==0){printf("Outer polygon cycles will be counter clockwise \n");optiono=1;}
if(strncmp(inner,"cw",2)==0){printf("Inner polygon cycles will be clockwise \n");optioni=1;}
if(strncmp(inner,"cc",2)==0){printf("Inner polygon cycles will be counter clockwise \n");optioni=1;}
if(!optiono || !optioni)
   {
   printf("<ERROR>\n");
   printf("Invalid options for rotation of outer and inner polygons\n");
   printf("Use cc for counter clock-wise and cw for clockwise\n");
   exit(1);
   }

/* OPEN INPUT MIM */
printf("\ninput metafile: %s\n",argv[1]);
if((inFilePtr = fopen(argv[1], "r")) == NULL)
   {
   printf("<ERROR> opening modify metafile\n");
   exit(1);
   }   

/* OPEN OUTPUT MIM */
outFileName[0] = NULL;
strncpy(outFileName,argv[1],50);
strcat(outFileName,"_c");
printf("\noutput metafile: %s\n",outFileName);
if((outFilePtr = fopen(outFileName, "w")) == NULL)
   {
   printf("<ERROR> opening new file for output\n");
   exit(1);
   }   

/*  READ THROUGH MIM */
  while(fgets(text,255,inFilePtr) != NULL && !feof(inFilePtr)) 
     { 
     numToks = parseTokens(text);
     strcpy(tmp, *tokenStrPtrs);

/*   IF COMMAND IS PGX REORDER IF NECESSARY AND WRITE TO OUTPUT MIM */
     if (strncmp(tmp,"*pgX",4) == 0) 
        {
     /* CHECK STATUS OF PGX COMMAND */
        statusflag=0;
        for(k=4; k<numToks; k++)
           {
           if(strncmp(tokenStrPtrs[k],"Xref",4)==0 ||
              strncmp(tokenStrPtrs[k],"Xdel",4)==0)statusflag=1;
           }
 
     /* IF PGX NOT XDEL OR XREF, PROCESS */
        if(!statusflag)
          {
     /* LOAD UP COORDINATES OF PGX */
        mimft_coord(inFilePtr,text,xa,ya,nxy,&ncycle,
                    &fminx,&fmaxx,&fminy,&fmaxy);

        if(debug)printf("ncycles is %d\n",ncycle);
     /* DETERMINE HOW COORDINATES ARE CURRENTLY ORDERED */
        start=0; 
        for(i=0; i<ncycle; i++)
           {
           ptr=&start;
           count=nxy[i];
           order=clockw(xa,ya,count,ptr);
           if(debug)printf("order of pgx is %d\n",order);

     /* FOR EACH POLYGON, FLIP AS NECESSARY */
           if(i==0)
             {
             if(strncmp(outer,"cw",2)==0 && order==0)flip(xa,ya,count,ptr);
             if(strncmp(outer,"cc",2)==0 && order==1)flip(xa,ya,count,ptr);
             }
           else if (i>0)
             {
             if(strncmp(inner,"cw",2)==0 && order==0)flip(xa,ya,count,ptr);
             if(strncmp(inner,"cc",2)==0 && order==1)
               {
               if(debug)printf("call flip with x2 of %6.3f\n",xa[start+1]);
               flip(xa,ya,count,ptr);
               if(debug)printf("after flip with x2 of %6.3f\n",xa[start+1]);
               }
        
             }

           start=start+count;
           }

        /* WRITE OUT PGX IN MIM */
           mimft_coord_out_dec(outFilePtr,text,xa,ya,nxy,ncycle);

          } /* End if for status check */
         if(statusflag)fputs(text,outFilePtr);
        }/* End if for pgx */

/*   FOR ALL OTHER MIM COMMANDS, WRITE TO OUTPUT MIM AND CONTINUE */
     else
        {
        fputs(text,outFilePtr);
        }
     continue;
     } /* End while loop */

  fclose(inFilePtr);
  fclose(outFilePtr);

 } /* end of main */