/*namedef3.c
written by: Wm Helterbrand, Cartographer, USGS Mid-Continent Mapping Center.
version 1.0 july 18 1996
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <math.h>

#define READLEN 144
#define TRUE 1;
#define FALSE 0;

int match,maps,t,counter,breaker,i,junk,d,cnt,zone,scalenum;
int mapzone,map1zone,map2zone,map3zone,map4zone,map5zone;
float nl,sl,el,wl,Nlat,Slat,Elong,Wlong;
double name,name2,zonef;
char N[40];
char S[10];
char E[10];
char W[10];
char line[READLEN];
char var1[40];
char var1_2[40];
char Spaces[2];
char temp[40];
char var2[2];
char var3[10];
char var4[10];
char var5[10];
char var6[10];
char var7[10];
char map[41];
char map1[41];
char map2[41];
char map3[41];
char map4[41];
char map5[41];
char state[3];
char state1[3];
char state2[3];
char state3[3];
char state4[3];
char state5[3];
char scale;
char s[2];
char n[40];
char mrc[8];
char mrc2[9];
char mapmrc[9];
char map1mrc[9];
char map2mrc[9];
char map3mrc[9];
char map4mrc[9];
char map5mrc[9];
char map6mrc[9];
char scalec[12];
char *endptr;
char *endptr2;
FILE *tics;
FILE *infp;
FILE *outfp;
FILE *mapsel;
FILE *map_1;
FILE *map_2;
FILE *map_3;
FILE *map_4;
FILE *map_5;
FILE *lst;

int main(int argc, char *argv[])
{
	scale = *argv[1];
	for(d=2;d<argc;d++)
	{
		maps++;
		if (argv[d] != NULL)
		{
			if(d==2) sprintf(map,"%s\0",argv[d]);
			else if(d==3) sprintf(state,"%s\0",argv[d]);
			else if(d==4) sprintf(map1,"%s\0",argv[d]);
			else if(d==5) sprintf(state1,"%s\0",argv[d]);
			else if(d==6) sprintf(map2,"%s\0",argv[d]);
			else if(d==7) sprintf(state2,"%s\0",argv[d]);
			else if(d==8) sprintf(map3,"%s\0",argv[d]);
			else if(d==9) sprintf(state3,"%s\0",argv[d]);
			else if(d==10) sprintf(map4,"%s\0",argv[d]);
			else if(d==11) sprintf(state4,"%s\0",argv[d]);
			else if(d==12) sprintf(map5,"%s\0",argv[d]);
			else if(d==13) sprintf(state5,"%s\0",argv[d]);
		}
	}
	maps = (maps /2);
	if(map[0] != '\0')
	{
		SUBST(temp,map,"_"," ");
		map[0]='\0';
		strcpy(map,temp);
	}
	if(map1[0] != '\0')
	{
		SUBST(temp,map1,"_"," ");
		map1[0]='\0';
		strcpy(map1,temp);
	}
	if(map2[0] != '\0')
	{
		SUBST(temp,map2,"_"," ");
		map2[0]='\0';
		strcpy(map2,temp);
	}
	if(map3[0] != '\0')
	{
		SUBST(temp,map3,"_"," ");
		map3[0]='\0';
		strcpy(map3,temp);
	}
	if(map4[0] != '\0')
	{
		SUBST(temp,map4,"_"," ");
		map4[0]='\0';
		strcpy(map4,temp);
	}
	if(map5[0] != '\0')
	{
		SUBST(temp,map5,"_"," ");
		map5[0] = '\0';
		strcpy(map5,temp);
	}
	SET_SCALE();
	SET_QUADNAMES();
	/*OUTPUT();*/
}
/***************************FUNCTIONS**********************************/
SET_SCALE()
{
	if (scale == 'o' || scale == 'p')
		scalenum = 24000;
	if (scale == 'l' || scale == 'k')
		scalenum = 25000;
	if (scale == 'g' || scale == 'f')
		scalenum = 100000;
	if (scale == 'i')
		scalenum = 63360;
	if (scale == 'r')
		scalenum = 20000;
	if (scale == 'c')
		scalenum = 250000;
	if (scale == 'j')
		scalenum = 30000;
	sprintf(scalec,"%d.lst",scalenum);
}/*SET_SCALE()*/
/*************************************************************************/
SET_QUADNAMES()
{
	lst = fopen(scalec, "rb");
	outfp=fopen("mapnames","wb");
	map1mrc[0] = '\0';
	mapmrc[0] = '\0';
	map2mrc[0] = '\0';
	map3mrc[0] = '\0';
	map4mrc[0] = '\0';
	map5mrc[0] = '\0';
	for(d=0;d!=EOF&&breaker==0;d++)
	{
		fgets(line,READLEN,lst);
		for(i=0;i < READLEN && counter==0;i++)
		{
			if(line[i] !=32)
				{
					cnt=i;
					t=0;
				}
			if (line[i]==32)
				{
					t++;
				
					if (t>1)
						counter++;
					else
						cnt=i;
				}
		}
		counter=0;
		strncat(var1,&line[0],cnt);
		strncat(var2,&line[41],2);
                strncat(var3,&line[44],8);
                strncat(var4,&line[64],8);
                strncat(var5,&line[84],8);
                strncat(var6,&line[102],10);
                strncat(var7,&line[122],10);
                if ((strcmp(map,var1)==0 && strcmp(state,var2)==0)||(strcmp(map1,var1)==0 && strcmp(state1,var2)==0)||(strcmp(map2,var1)==0 && strcmp(state2,var2)==0)||(strcmp(map3,var1)==0 && strcmp(state3,var2)==0)||(strcmp(map4,var1)==0 && strcmp(state4,var2)==0)||(strcmp(map5,var1)==0 && strcmp(state5,var2)==0))
		{
			match++;
			SUBmrc(mrc,var3);
			sprintf(mrc2,"%c%s",scale,mrc);
			strcat(mrc2,".tic");
			SUBTIC(N,var4);
			SUBTIC(S,var5);
			SUBTIC(E,var6);
			SUBTIC(W,var7);
			zone = abs (atoi (E));
			zone = (30 - (zone / 6));
                       	if (strcmp(map,var1)==0)
			{
                                sprintf(mapmrc,"%c%s",scale,mrc);
				mapzone=zone;
			}
                        if (strcmp(map1,var1)==0)
			{
                                sprintf(map1mrc,"%c%s",scale,mrc);
				map1zone=zone;
			}
                        if (strcmp(map2,var1)==0)
			{
                                sprintf(map2mrc,"%c%s",scale,mrc);
				map2zone=zone;
			}
                        if (strcmp(map3,var1)==0)
			{
                                sprintf(map3mrc,"%c%s",scale,mrc);
				map3zone=zone;
			}
                        if (strcmp(map4,var1)==0)
			{
                                sprintf(map4mrc,"%c%s",scale,mrc);
				map4zone=zone;
			}
                        if (strcmp(map5,var1)==0)
			{
                                sprintf(map5mrc,"%c%s",scale,mrc);
				map5zone=zone;
			}
			tics = fopen(mrc2, "wb");
			fprintf(tics,"1\n%s,%s\n%s,%s\n%s,%s\n%s,%s\n%s,%s\nEND\nEND\n",E,S,W,S,W,N,E,N,E,S);
			fclose (tics);
		}	
		if(match == maps)
			breaker++;
		var1[0] = '\0';
		var2[0] = '\0';
		var3[0] = '\0';
		var4[0] = '\0';
		var5[0] = '\0';
		var6[0] = '\0';
		var7[0] = '\0';
		mrc[0] = '\0';	
		N[0] = '\0';
		S[0] = '\0';
		E[0] = '\0';
		W[0] = '\0';
	}
	if(mapmrc[0] != '\0')
	{
		fprintf(outfp,"%s.tif\n",mapmrc);
		fprintf(outfp,"%d\n",mapzone);
	}
	if(map1mrc[0] != '\0')
	{
		fprintf(outfp,"%s.tif\n",map1mrc);
		fprintf(outfp,"%d\n",map1zone);
	}
	if(map2mrc[0] != '\0')
	{
		fprintf(outfp,"%s.tif\n",map2mrc);
		fprintf(outfp,"%d\n",map2zone);
	}
	if(map3mrc[0] != '\0')
	{
		fprintf(outfp,"%s.tif\n",map3mrc);
		fprintf(outfp,"%d\n",map3zone);
	}
	if(map4mrc[0] != '\0')
	{
		fprintf(outfp,"%s.tif\n",map4mrc);
		fprintf(outfp,"%d\n",map4zone);
	}
	if(map5mrc[0] != '\0')
	{
		fprintf(outfp,"%s.tif\n",map5mrc);
		fprintf(outfp,"%d\n",map5zone);
	}
	fclose(outfp);
}
/*******************************************************************************/
SUBST(a,b,c,d)
char *a;
char *b;
char c;
char d;
{
	int z;
	int length;
	a[0] = '\0';
	z=0;
	length=0;
	length = strlen(b);
	for(z=0;z<length+1;z++)
	{
		if (b[z] != 95)
			{
			if(b[z] != 35)
				a[z] = b[z];
			if(b[z] == 35)
				a[z] = 38;
			}
		if (b[z] == 95)
			a[z] = 32;
		if(b[z]=='\0')
			{
			a[z] = '\0';
			}
	}
	return(a[0] != '\0');
}/*SUBST*/
/******************************************************************************/
SUBmrc(a,b)
char *a;
char *b;
{
	int z;
	int y;
	int length,lengtha;
	a[0]='\0';
	z=0;
	length=0;
	length = strlen(b);
	lengtha = length -1;
	while(b[z] != 45)
		{
			y=z;
			a[y] = b[z];
			z++;
		}
	if (b[z] == 45)
	{
		z++;
	}
	for(;z<length+2;z++)
	{
		y++;
		if(b[z] >= 65 && b[z] <= 90)
			{
			a[y] = b[z] + 32;
			}
		else 
			{
			a[y] = b[z];
			}
		
	}
	return(a[0] != '\0');
}
/****************************************************************************/
SUBTIC(a,b)
char *a;
char *b;
{
	int z;
	char *startptr;
	startptr = strrchr(b,' ');
	strcat(a,startptr+1);
}