# include "data.h"

int load (int argc, char **argv) {
  
  int N, n, Nline, ISCEL;
  int Ximage, Nimage;
  char *c, type[10], string[128];
  double x, y, dx, dy, x1, y1;
  double dra, ddec, ra1, dec1, ra, dec;
  FILE *f;
  char *buffer, *line;
  Coords coords;
  Buffer *buf;
  
  Nimage = -1;
  if (N = get_argument (argc, argv, "-n")) {
    remove_argument (N, &argc, argv);
    Nimage = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetImage (&Ximage, &Nimage)) return (FALSE);

  ISCEL = FALSE;
  if (N = get_argument (argc, argv, "-c")) {
    remove_argument (N, &argc, argv);
    ISCEL = TRUE;
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: load (overlay) <filename>\n [-c] [-n]");
    fprintf (stderr, "  -c: read overlay in celestial coords\n");
    return (FALSE);
  }
  
  if (!SelectOverlay (argv[1], &n)) return (FALSE);

  if (!strcmp (argv[2], "-")) {
    f = stdin;
  } else {
    f = fopen (argv[2], "r");
  }
  if (f == NULL) {
    fprintf (stderr, "can't find file %s\n", argv[2]);
    return (FALSE);
  }

  if (ISCEL) {
    if ((buf = SelectBuffer (GetImageName(), OLDBUFFER, TRUE)) == NULL) return (FALSE);
    GetCoords (&coords, &buf[0].header);
  }

  ALLOCATE (buffer, char, 65536);  /* space for 512 lines of 128 bytes */
  bzero (buffer, 65536);
  ALLOCATE (line, char, 1024);     /* space to read the file */
  Nline = 0;

  write (Ximage, "LOAD", 4); /* force Ximage to look for the incoming image */
  sprintf (line, "OVER %9d ", n);
  write (Ximage, line, 16);
  
  for (n = 0; scan_line (f, line) != EOF;) {
    c = strchr (line, '#');
    if (c != (char *) NULL) 
      *c = 0;  /* force end of line at comment! */
    while ((c = strchr (line, '(')) != (char *) NULL) 
      *c = ' ';
    while ((c = strchr (line, ')')) != (char *) NULL) 
      *c = ' ';
    while ((c = strchr (line, ',')) != (char *) NULL) 
      *c = ' ';
    /* we could use some syntactial checks here */
    /* have to get all three for this to be any valid object, if the line is commented out,
     we should get none, so check that N == 3 before continuing: */
    N = sscanf (line, "%s %lf %lf %lf %lf", type, &ra, &dec, &dra, &ddec);
    switch (N) {
    case 0:
    case -1:
      continue;
    case 1:
    case 2:
    case 3:
      if (strcmp (type, "TEXT")) {
	fprintf (stderr, "syntactical error in line:\n   %s\n", line);
	continue;
      }
      sscanf (line, "%s %lf %lf %127s", type, &ra, &dec, string);
    case 4:
      ddec = dra;
    case 5:
      if (ISCEL) {
	if (!strcmp (type, "LINE")) {
	  RD_to_XY (&x, &y, ra, dec, &coords);
	  ra1 = ra + dra;
	  dec1 = dec + ddec;
	  RD_to_XY (&x1, &y1, ra1, dec1, &coords);
	  dy = (y1 - y);
	  dx = (x1 - x);
	} else {
	  RD_to_XY (&x, &y, ra, dec, &coords);
	  ra1 = ra;
	  dec1 = dec + ddec;
	  RD_to_XY (&x1, &y1, ra1, dec1, &coords);
	  dy = (fabs(x1 - x) + fabs(y1 - y));
	  ra1 = ra + dra/cos(dec*RAD_DEG);;
	  dec1 = dec;
	  RD_to_XY (&x1, &y1, ra1, dec1, &coords);
	  dx = (fabs(x1 - x) + fabs(y1 - y));
	}
      }
      else {
	x = ra;
	y = dec;
	dx = dra;
	dy = ddec;
      }
    }
    if (!strcmp (type, "TEXT")) {
      snprintf (&buffer[Nline*128], 128, "%15s %20.10f %20.10f %s ", type, x, y, string);
    } else {
      snprintf (&buffer[Nline*128], 128, "%15s %20.10f %20.10f %20.10f %20.10f ", type, x, y, dx, dy);
    }
    n++;
    Nline ++;
    if (Nline == 512) {
      sprintf (line, "NLINES  %7d ", Nline);
      write (Ximage, line, 16);
      write (Ximage, buffer, Nline*128);
      bzero (buffer, 65536);
      Nline = 0;
    }
  }

  sprintf (line, "NLINES  %7d ", Nline);
  write (Ximage, line, 16);
  write (Ximage, buffer, Nline*128);
  sprintf (line, "DONE ");
  write (Ximage, line, 16);

  free (buffer);
  free (line);
  fprintf (stderr, "loaded %d objects\n", n);
  if (f != stdin) {
    fclose (f);
  }
  return (TRUE);
}

