/*  This program converts Kepler data in 2-line NASA format to      */
/*  the format used by the QUIKTRAK program.                        */


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

unsigned char* trim(unsigned char *s, int len);
int trimlen;


main(int argc, char **argv)
{
	if(argc > 1)
		convert(argv[1]);
	else {
		printf("NASA to QUIKTRAK conversion program v1.0 by SM5SXL\n");
		printf("usage: n2q <source file in NASA format> \n");
		exit(1);
	}
}


convert(char *kepsfile)
{
	FILE* tf;
	FILE* kf;
	FILE* pf;
	char nasa_1[129], nasa_2[129], line_1[14][129], line_2[14][129], freq_str[80];
	int sat_exists, i;
	char *tmp;

	if(! (kf = fopen(kepsfile, "rt"))) {
		printf("Error opening %s!\n", kepsfile);
		exit(1);
	}
	if(! (pf = fopen("TRACK.DAT", "rt"))) {
		printf("Error opening TRACK.DAT!\n");
		exit(1);
	}

	tf = fopen("TEMP.$$$", "wt");

	printf("\n");

	fgets(nasa_1, 128, pf);         /* Old QTH data from TRACK.DAT */
	fputs(nasa_1, tf);

	for (i = 0; i < 14; i++) {
		fgets(line_1[i], 128, pf);
		fgets(line_2[i], 128, pf);
	}

	while(1) {
		if(! fgets(nasa_1, 128, kf)) break;      /* Satellite name */
		if(! strlen(nasa_1)) break;

		nasa_1[strlen(nasa_1)-1] = 0;
		i = strlen(nasa_1) - 1;						  /* Trim trailing spaces */
		while(nasa_1[i] == 0x20)
			nasa_1[i--] = 0;

		sat_exists = 0;

		for(i = 0; i < 14; i++) {                /* Search for match */
			if(strstr(line_1[i], nasa_1)) {
				sat_exists = 1;
				break;
			}
		}

		if(sat_exists) {
			printf("Updating %s\n", nasa_1);     /* message to screen */

			strcpy(line_1[i], "\"");
			strcat(line_1[i], nasa_1);
			strcat(line_1[i], "\",");          /* Satellite name */

			fgets(nasa_1, 128, kf);            /* Read in line 1 and 2 of one sat */
			fgets(nasa_2, 128, kf);

			strcat(line_1[i], "\"");
			tmp = trim(&nasa_1[2], 5);
			strncat(line_1[i], tmp, trimlen);   /* Cat number */
			strcat(line_1[i], " set ");

			tmp = trim(&nasa_1[64], 4);
			strncat(line_1[i], tmp, trimlen);   /* Element set */
			strcat(line_1[i], "\",");

			tmp = trim(&nasa_1[20], 12);
			strncat(line_1[i], tmp, trimlen);   /* Epoch day */
			strcat(line_1[i], ",");

			tmp = trim(&nasa_2[8], 8);
			strncat(line_1[i], tmp, trimlen);    /* Inclination */
			strcat(line_1[i], ",");

			tmp = trim(&nasa_2[17], 8);
			strncat(line_1[i], tmp, trimlen);    /* RAAN */
			strcat(line_1[i], ",");

			strcat(line_1[i], ".");
			tmp  = trim(&nasa_2[26], 7);
			strncat(line_1[i], tmp, trimlen);   /* Ecce */
			strcat(line_1[i], ",");

			tmp = trim(&nasa_2[34], 8);
			strncat(line_1[i], tmp, trimlen);   /* Arg of Perigee */
			strcat(line_1[i], ",");

			tmp = trim(&nasa_2[43], 8);
			strncat(line_1[i], tmp, trimlen);   /* Mean Anomaly */

			strcat(line_1[i], "\n");

			tmp = strchr(line_2[i], ',') + 1;   /* Extract Freq and BLAT/BLNG from old file */
			tmp = strchr(tmp, ',') + 1;
			tmp = strchr(tmp, ',') + 1;
			strcpy(freq_str, tmp);

			strcpy(line_2[i], "");

			tmp = trim(&nasa_2[52], 11);
			strncat(line_2[i], tmp, trimlen);   /* Mean Motion */
			strcat(line_2[i], ",");

			tmp = trim(&nasa_1[33], 10);
			strncat(line_2[i], tmp, trimlen);   /* Drag */
			strcat(line_2[i], ",");

			tmp = trim(&nasa_2[63], 5);
			strncat(line_2[i], tmp, trimlen);   /* Orbit nr */
			strcat(line_2[i], ",");

			strcat(line_2[i], freq_str);       /* Push back freq and BLNG/BLAT
															  values */
		} else {
			fgets(nasa_1, 128, kf);          /*  Read through sat entry  */
			fgets(nasa_1, 128, kf);
		}
	}


	for(i = 0; i < 14; i++) {           /* Put new sat values in TEMP.$$$ */
		fputs(line_1[i], tf);
		fputs(line_2[i], tf);
	}

	for(i = 0; i < 14; i++) {           /* Add window data to TEMP.$$$ */
		fgets(nasa_1, 128, pf);
		fputs(nasa_1, tf);
	}

	fprintf(tf,"%c\n", 0x1a);                  /* Ctrl-Z to be sure... */

	fclose(kf);
	fclose(pf);
	fclose(tf);

	remove("TRACK.DAT");               /* delete old TRACK.DAT */
	rename("TEMP.$$$", "TRACK.DAT");   /* rename temp file to TRACK.DAT */

	printf("OK.\n");
}

unsigned char* trim(unsigned char *s, int len)        /* Trims spaces */
{
	while(s[0] == 0x20) {
		len--;
		s += 1;
	}
	trimlen = len;
	return s;
}