#include <xsimple.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <geolib/cproj.h>
#include "mapview.h"


static void addmap(char *s)
{
   static MAPARG *arg, *prevarg = NULL;

   arg = (MAPARG *)malloc(sizeof(MAPARG));
   arg->data = (char *)malloc(strlen(s) + 1);
   strcpy(arg->data, s);
   arg->next = NULL;
   if (! mapargs)
       mapargs = arg;
   if (prevarg)
       prevarg->next = arg;
   prevarg = arg;
}

static int skipcmt(FILE *f, char *s)
{
   do {
       if (! fgets(s, 80, f))
           return 0;
   } while (*s == '#');
   return 1;
}

static void parse(FILE *f, char *s)
{
   if (! strncmp(s, ".PROJ", 5)) {
       double factor, centmer, feast, fnorth;
       long outdatum, indatum;
       long iflag;
       int i;

       if (! skipcmt(f, s)) return;
       sscanf(s, "%ld %lf %lf %lf %lf", &outdatum, &factor, &centmer,
               &feast, &fnorth);
       indatum = outdatum;

       for (i = 0; i < 15; i++)
           outparm[i] = inparm[i] = 0.;
       outparm[2] = inparm[2] = factor;
       outparm[4] = inparm[4] = centmer;
       outparm[6] = inparm[6] = feast;
       outparm[7] = inparm[7] = fnorth;

       for_init(PROJECT, 0, outparm, outdatum, NULL, NULL, &iflag, fortrans);
       if (iflag)
           exit(1);

       inv_init(PROJECT, 0, inparm, indatum, NULL, NULL, &iflag, invtrans);
       if (iflag)
           exit(1);

       return;
   }

   if (! strncmp(s, ".REGION", 7)) {
       if (! skipcmt(f, s)) return;
       sscanf(s, "%lf %lf %lf %lf", &iminlat, &imaxlat, &iminlon, &imaxlon);
       if (! skipcmt(f, s)) return;
       sscanf(s, "%lf %lf %lf %lf %u", &minlat, &maxlat, &minlon, &maxlon,
               &scale);

       bminlat = minlat; bmaxlat = maxlat; bminlon = minlon; bmaxlon = maxlon;
       bscale = scale;

       clat = minlat + (maxlat - minlat) / 2;
       clon = minlon + (maxlon - minlon) / 2;

       return;
   }

   if (! strncmp(s, ".MISC", 5)) {
       char tmp1[20], tmp2[20];

       if (! skipcmt(f, s)) return;
       sscanf(s, "%d %d %lf %s %s", &dogrid, &dolabels, &griddeg, tmp1, tmp2);

       if (strcmp(tmp1, "-1"))
           strcpy(bgcolor_s, tmp1);
       if (strcmp(tmp2, "-1"))
           strcpy(label_bgcolor_s, tmp2);

       return;
   }

   if (! strncmp(s, ".MAPS", 5) && (! mapargs)) {
       while (fgets(s, 80, f)) {
           if (*s == '#')
               continue;
           if (*s == '\n')
               break;
           s[strlen(s) - 1] = '\0';
           addmap(s);
       }
       return;
   }

   if (! strncmp(s, ".LOC", 4)) {
       LOCATION *loc, *prevloc = NULL;

       while (fgets(s, 80, f)) {
           char name[80], tab[80];

           if (*s == '#')
               continue;
           if (*s == '\n')
               break;

           loc = (LOCATION *)malloc(sizeof(LOCATION));
           sscanf(s, "%[^\t]%[\t]%lf%[\t]%lf%[\t]%d", name, tab,
                   &(loc->lat), tab, &(loc->lon), tab, &(loc->showscale));

           loc->name = (char *)malloc(strlen(name) + 1);
           strcpy(loc->name, name);

           loc->next = NULL;
           if (! locs)
               locs = loc;
           if (prevloc)
               prevloc->next = loc;
           prevloc = loc;

           nlocs++;
       }
       return;
   }
}

void readconfig(FILE *f, int argc, char **argv)
{
   char s[80];
   int i;

   if (argc > 1) {
       for (i = 1; i < argc; i++)
           addmap(argv[i]);
   }

   while (fgets(s, 80, f)) {
       s[strlen(s) - 1] = '\0';
       if (! strncmp(s, ".INCLUDE", 8)) {
           FILE *f;
           if ((f = fopen(s + 9, "r"))) {
               while (fgets(s, 80, f))
                   parse(f, s);
               fclose(f);
           }
           continue;
       }
       parse(f, s);
   }

   fclose(f);
}