#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int main ()
{
  char line_in[100];
  FILE *zones;
  FILE *boundary;
  char boundary_name[100];
  zones = fopen ("maps/zones.sorted", "r");
  int zone_num = 0;
 
  printf ("typedef struct {double x;double y;double z;} vect_type;\n");
  while (!feof (zones))
  {
    char coords[100];
    int points_in_vector = 0;
    if (fgets (line_in, sizeof (line_in), zones))
    {
    //Strip the newline off the end.
    line_in[strlen(line_in) - 1] = 0;
    
    sprintf (boundary_name, "maps/boundary%s", line_in);

//printf ("%s<\n", boundary_name);
    
    boundary = fopen (boundary_name, "r");
    
    if (!boundary)
    {
      fprintf (stderr, "%s ", boundary_name);
      perror ("fopen");
      exit (1);
    }

    printf ("vect_type boundary_%d[] = {\n", zone_num);

    while (!feof (boundary))
    {
      int num_scanned;
      int id,code,point;
      double lon,lat,x,y,z;    
      
      memset (coords, 0, sizeof (coords));
      fgets (coords, sizeof (coords), boundary);
//printf ("scanning %s\n", coords); 

      num_scanned = sscanf (coords, "%d,%d,%d,%lf,%lf", &id,&code,&point,&lon,&lat);

//printf ("num scanned%d\n", num_scanned);
      
      if (num_scanned <= 0)
      {
        // EOF coming.
      }
      else if (num_scanned == 5)
      {
      lon *= M_PI / 180.0;
      lat *= M_PI / 180.0;
      
      x = cos (lon) * cos (lat);
      y = sin (lon) * cos (lat);
      z = sin (lat);
      
        points_in_vector++;
        printf ("{%.5lf, %.5lf, %.5lf},\n", x, y, z);
      }
      else
      {
//printf ("points in = %d\n", points_in_vector);

        // If there was only a single point in the vector duplicate it.
        // This will allow ust to use xdrawlines to draw it.
        if (points_in_vector == 1)
        printf ("{%.5lf, %.5lf, %.5lf},\n", x, y, z);
        
        //Print an end vector delimiter.
        printf ("{-10.0,-10.0,-10.0},\n");
        points_in_vector = 0;
      }
    }
    printf ("};\n");
    
    printf ("#define num_boundary_points_%d XtNumber (boundary_%d)\n", zone_num, zone_num);
    fclose (boundary);
    zone_num++;
    }
  } 

  fclose (zones);


  printf ("typedef struct {\n"
          "  char *zonename;\n"
          "  vect_type v;\n"
          "} place_info_type;\n");

  zones = fopen ("maps/zones.sorted", "r");
  zone_num = 0;
 
  while (!feof (zones))
  {
    FILE *places;
    char place_name[100];
    
    if (fgets (line_in, sizeof (line_in), zones))
    {
      char place_info[100];
      double lat,lon;
      char zonename[100];

      //Strip the newline off the end.
      line_in[strlen(line_in) - 1] = 0;
    
      printf ("#define zone_offset_%d \"%s%s\"\n", zone_num, (line_in[0] == '-') ? "" : "+", line_in);

      sprintf (place_name, "maps/places%s", line_in);

//printf ("%s<\n", place_name);
    
      places = fopen (place_name, "r");

      if (!places)
      {
        fprintf (stderr, "%s ", place_name);
        perror ("fopen");
        exit (1);
      }    

      printf ("place_info_type places_%d[] =\n{\n", zone_num);

      memset (place_info, 0, sizeof (place_info));

      while (fgets (place_info, sizeof (place_info), places))
      {
//printf (place_info);

        if (sscanf (place_info, "%lf %lf %s", &lat, &lon, zonename) == 3)
        {
          lat *= M_PI / 180.0;
          lon *= M_PI / 180.0;

          printf ("  {\"%s\", {%.5lf, %.5lf, %.5lf}},\n", zonename, 
                  
            cos (lon) * cos (lat),
            sin (lon) * cos (lat),
            sin (lat)
                  );
        }
      }
      printf ("};\n");

      printf ("#define num_places_%d XtNumber (places_%d)\n", zone_num, zone_num);
      zone_num++;
    } 
  }
  
  printf ("#define NUM_ZONES %d\n", zone_num);

  printf ("struct {\n"
          "  char *offset;\n"
          "  int num_boundary_points;\n"
          "  vect_type *boundary_points;\n"
          "  int num_places;\n"
          "  place_info_type *place_info;\n"
          "} zone_data[] =\n"
          "{\n");

  for (int zone = 0; zone < zone_num; zone++)
  {
    printf ("  {\n"
            "    zone_offset_%d,\n"
            "    num_boundary_points_%d,\n"
            "    boundary_%d,\n"
            "    num_places_%d,\n"
            "    places_%d\n"
            "  },\n", zone, zone, zone, zone, zone);
  }

  printf ("};\n");

  exit (0);
}