#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double x1, Y1, x2, y2, ap;
int le, ap0;
char line[1024];
char delim[] = ":,=\r\n";

void param(int type)
{
  if (type == 0){
    strtok(line, delim);
    strtok(NULL, delim); x1 = atof(strtok(NULL, delim));
    strtok(NULL, delim); Y1 = atof(strtok(NULL, delim));
    strtok(NULL, delim); le = atoi(strtok(NULL, delim));
    strtok(NULL, delim); ap0 = atoi(strtok(NULL, delim));
  }
  else{
    strtok(line, delim);
    strtok(NULL, delim); x1 = atof(strtok(NULL, delim));
    strtok(NULL, delim); Y1 = atof(strtok(NULL, delim));
    strtok(NULL, delim); x2 = atof(strtok(NULL, delim));
    strtok(NULL, delim); y2 = atof(strtok(NULL, delim));
    strtok(NULL, delim); le = atoi(strtok(NULL, delim));
    strtok(NULL, delim); ap0 = atoi(strtok(NULL, delim));
  }
}

int check_region(int x, int y, int xs, int ys, int xe, int ye)
{
  if (x >= xs && x <= xe && y >= ys && y <= ye) return(1);
  else return(0);
}


double conv_x(double x)
{
  return(300.0-x);
}

double conv_y(double y)
{
  return(y);
}

main(int ac, char **av)
{
  double XS = 0.0, YS = 0.0, XE = 300.0, YE = 210.0;
  int op = 0;
  char cmd;
  int let, led, le2;
  char type[10];
  double w, tmp, ex, ey, nx, ny;
  double xoff, yoff;
  double xmin, xmax, ymin, ymax;
  char line2[1024];

  if (ac > 1) cmd = av[1][0]; else cmd = ' ';
  if (cmd == 'f' || cmd == 'F') op = 1;
  else if (cmd == 'c' || cmd == 'C'){
    op = 2;
    if (ac > 2) let = 0; // Solder Side
    else let = 1;        // Component Side
  }
  else if (cmd == 'l' || cmd == 'L'){
    op = 3;
    if (ac >= 6){
      XS = atoi(av[2]); YS = atoi(av[3]); XE = atoi(av[4]); YE = atoi(av[5]);
    }
  }
  else if (cmd == 'r' || cmd == 'R'){
    op = 4;
    if (ac >= 6){
      XS = atoi(av[2]); YS = atoi(av[3]); XE = atoi(av[4]); YE = atoi(av[5]);
    }
  }
  else{
    fprintf(stderr, "usage: %s OPERATION [area]\n", av[0]);
    fprintf(stderr, " - f               : Flip\n");
    fprintf(stderr, " - c [L]           : Generate Check Draw (for Copper side)\n");
    fprintf(stderr, " - l [xs ys xe ye] : Layer Change\n");
    fprintf(stderr, " - r [xs ys xe ye] : Covert to Resist\n");
    exit(1);
  }

  printf("[Version]\nPCBE:Version 0.48.4\n[BoardSize]\nX:300\nY:210\nGRID:1\n");
  printf("[LayerDef]\n");

  printf("0:NAME=Aux,COLOR=0,DISP=ON,ACTIV=ON,PRINT=OFF\n");
  printf("1:NAME=Pat1,COLOR=1,DISP=ON,ACTIV=ON,PRINT=ON\n");
  printf("2:NAME=Pat2,COLOR=2,DISP=ON,ACTIV=ON,PRINT=OFF\n");
  printf("3:NAME=Silk1,COLOR=3,DISP=ON,ACTIV=ON,PRINT=OFF\n");
  printf("4:NAME=Silk2,COLOR=4,DISP=ON,ACTIV=ON,PRINT=OFF\n");
  printf("5:NAME=Sol1,COLOR=5,DISP=ON,ACTIV=ON,PRINT=OFF\n");
  printf("6:NAME=Sol2,COLOR=6,DISP=ON,ACTIV=ON,PRINT=OFF\n");
  printf("7:NAME=Outline,COLOR=0,DISP=ON,ACTIV=ON,PRINT=ON\n");
  printf("8:NAME=Hole,COLOR=0,DISP=ON,ACTIV=ON,PRINT=ON\n");
  printf("9:NAME=Base,COLOR=12,DISP=ON,ACTIV=ON,PRINT=OFF\n");

  printf("[PadDef]\n[FlshDef]\n[LineDef]\n255:0.001\n[DrawItems]\n");

  type[4] = '\0';
  while(fgets(line, 1023, stdin) != NULL){
    strcpy(line2, line);
    strncpy(type, line, 4);
    if (op == 1){
      // Flip
      if (strcmp(type, "LAND") == 0 || strcmp(type, "FLSH") == 0){
	param(0);
	printf("%s:X=%.3f,Y=%.3f,LE=%d,AP=%d\n", type, conv_x(x1), conv_y(Y1), le, ap0);
      }
      if (strcmp(type, "LINE") == 0){
	param(1);
	printf("%s:XS=%.3f,YS=%.3f,XE=%.3f,YE=%.3f,LE=%d,AP=%d\n",
	       type, conv_x(x1), conv_y(Y1), conv_x(x2), conv_y(y2), le, ap0);
      }
    }
    else if (op == 2){
      // Generate Check Draw
      if ((strcmp(type, "LAND") == 0) || strcmp(type, "FLSH") == 0){
	param(0);
	if ((le % 2) == let) printf("%s", line2);
      }
      else if (strncmp(line, "LINE:", 5) == 0){
	param(1);
	if ((le % 2) == let) printf("%s", line2);
      }
    }
    else if (op == 3){
      if (strcmp(type, "FLSH") == 0 || strcmp(type, "LAND") == 0){
	param(0);
	if (check_region(x1, Y1, XS, YS, XE, YE) == 1){
	  le2 = le;
	  if (le == 1) le2 = 2;
	  if (le == 2) le2 = 1;
	  if (le == 3) le2 = 4;
	  if (le == 4) le2 = 3;
	  if (le == 5) le2 = 6;
	  if (le == 6) le2 = 5;
	  printf("%s:X=%.3f,Y=%.3f,LE=%d,AP=%d\n", type, x1, Y1, le2, ap0);
	}
      }
      else if (strcmp(type, "LINE") == 0){
	param(1);
	if (check_region(x1, Y1, XS, YS, XE, YE) == 1 &&
	    check_region(x2, y2, XS, YS, XE, YE) == 1){
	  le2 = le;
	  if (le == 1) le2 = 2;
	  if (le == 2) le2 = 1;
	  if (le == 3) le2 = 4;
	  if (le == 4) le2 = 3;
	  if (le == 5) le2 = 6;
	  if (le == 6) le2 = 5;
	  printf("%s:XS=%.3f,YS=%.3f,XE=%.3f,YE=%.3f,LE=%d,AP=%d\n",
		 type, x1, Y1, x2, y2, le2, ap0);
	}
      }
    }
    else if (op == 4){
      if (strcmp(type, "FLSH") == 0 || strcmp(type, "LAND") == 0){
	param(0);
	if (le == 1 || le == 2){
	  printf("%s:X=%.3f,Y=%.3f,LE=%d,AP=%d\n", type, x1, Y1, le, ap0);
	  printf("%s:X=%.3f,Y=%.3f,LE=%d,AP=%d\n", type, x1, Y1, le+4, ap0+1);
	}
      }
      if (strcmp(type, "LINE") == 0){
	param(1);
	if (le == 1 || le == 2){
	  printf("%s:XS=%.3f,YS=%.3f,XE=%.3f,YE=%.3f,LE=%d,AP=%d\n",
		 type, x1, Y1, x2, y2, le, ap0);
	  printf("%s:XS=%.3f,YS=%.3f,XE=%.3f,YE=%.3f,LE=%d,AP=%d\n",
		 type, x1, Y1, x2, y2, le+4, ap0+1);
	}
      }
    }
  }
}
