/*

	Noctis galactic guide / CAT command.
	GOES Net Module.

*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//#include <conio.h>
#include <dos.h>
#include <io.h>
 

char *output = "..\\data\\X.TXT";
char *outputbuffer = "..\\data\\XBUFF.TXT";
char *tmpbuffer = "..\\data\\XBUFF.TMP";
/*
void getsecs ()
{
	int m;
	char seconds[3], minutes[3], hours[3], day[3], month[3], year[5];
	char dfm[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	asm {	mov ah, 4
		int 0x1a
		lea bx, year
		mov ah, ch
		shr ah, 4
		mov al, ch
		and al, 0xF
		add ax, '00'
				xchg al, ah
		mov [bx], ax
		mov ah, cl
		shr ah, 4
		mov al, cl
		and al, 0xF
		add ax, '00'
		xchg al, ah
		mov [bx+2], ax
		lea bx, month
		mov ah, dh
		shr ah, 4
		mov al, dh
		and al, 0xF
		add ax, '00'
		xchg al, ah
		mov [bx], ax
		lea bx, day
		mov ah, dl
		shr ah, 4
		mov al, dl
		and al, 0xF
		add ax, '00'
		xchg al, ah
		mov [bx], ax
		mov ah, 2
		int 0x1a
		lea bx, hours
		mov ah, ch
		shr ah, 4
		mov al, ch
		and al, 0xF
		add ax, '00'
		xchg al, ah
		mov [bx], ax
		lea bx, minutes
		mov ah, cl
		shr ah, 4
		mov al, cl
		and al, 0xF
		add ax, '00'
		xchg al, ah
		mov [bx], ax
		lea bx, seconds
		mov ah, dh
		shr ah, 4
		mov al, dh
		and al, 0xF
		add ax, '00'
		xchg al, ah
		mov [bx], ax
	}
	
	double secs = atol (year) - 1984;		      // anni dal 1984
	secs = secs * 365 + (long)(secs / 4);	      // anni * 365 + anni / 4

	for (m=1; m<atoi(month); m++)
		secs += dfm[m];			      // giorni per mese

	if (atoi(month)>2 && !(atol(year)%4)) secs++; // se l'anno Š bisestile

	secs += atoi(day) - 1;			      // + giorni mese attuale
	secs *= 86400;                                // secondi in un giorno

	secs += 3600 * atol(hours);		      // ora corrente in sec.
	secs += 60 * atoi(minutes);		      // minuti corr. in sec.

	int isecs = atoi(seconds);			      // secondi correnti.
	secs += isecs;
		
	if (timeAdjustment!=0) {
		secs += timeAdjustment;
		isecs = ((int)secs)%60;
	}
	
	return secs;
}
*/

void main()
{
	/*asm {	xor	ax, ax
		mov	es, ax
		cmp	byte ptr es:[0x449], 0x13
		je	startup }

	printf ("\nGalactic Organization of Explorers and Stardrifters (G.O.E.S)\n");
	printf ("-------------------------------------------------------------\n");
	printf ("This is a GOES NET module and must be run from a stardrifter.\n");
	printf ("Please use the onboard computer console to run this module.\n");
	printf ("\n\t- GOES NET onboard microsystem, EPOC 6011 REVISION 2\n");
	return;*/
	
	startup:
	
	
	FILE * f = NULL;
	unsigned char bufferexists=0;
	f = fopen(outputbuffer, "rt");
	if (f!=NULL) {
		fclose(f); f=NULL;
		bufferexists=1;
	}
	if (_argv[1]==NULL) {
		f = fopen(output, "rt");
		if (bufferexists==1) {
			if (f==NULL) {
				bufferexists=2;
			} else {
				fseek(f, 0, SEEK_END);
				long len = ftell(f);
				if (len==0) {	//Treat an empty x.txt like a nonexistant one.
					bufferexists=2;
					fclose(f);
					f=NULL;
					remove(output);
				}
			}
		}
		if (f!=NULL) {
			fclose(f);
			f=NULL;
		}   
		
	}
	for (int i = 1; _argv[i]!=NULL; i++) {
		if (f==NULL) {
			f = fopen(output, "rt");
			if (f!=NULL || bufferexists) {
				if (bufferexists==1 && f==NULL) bufferexists=2;
				if (f!=NULL) {
					fclose(f); f=NULL;
				}
				f = fopen(outputbuffer, "at");
				if (f==NULL) {
					printf("ERROR: CANNOT ADD TO BUFFER FILE (XBUFF.TXT). TWO INSTANCES OF X RUNNING AT ONCE?");
					return;
				}
				printf("WRITTEN TO BUFFER:   ");
				if (bufferexists) {
					fprintf(f, "\n");
				}
			} else {
				f = fopen(output, "wt");
				if (f==NULL) {
					printf("ERROR: CANNOT WRITE TO X.TXT. TWO INSTANCES OF X RUNNING AT ONCE?");
					return;
				}
				printf("WRITTEN TO FILE:     ");
			}
			printf("---------------------");
		} else {
			printf(" ");
			fprintf(f, " ");
		}
		fprintf(f, "%s", _argv[i]);
		printf(_argv[i]);
	}
	if (f!=NULL) {
		fclose(f);
		f=NULL;
	}
	if (bufferexists==2) {
		int c;
		f = fopen(outputbuffer, "rt");
		FILE *f2 = NULL;
		//read the first line of text and write it to the output file
		retry:
		int pos=0;
		for (c=fgetc(f); c!=-1 && c!=13 && c!=10; c=fgetc(f)) {
			if (f2==NULL) {
				f2 = fopen(output, "wt");
			}
			fputc(c, f2);
			pos++;
		}
		if (pos==0) {
			if (c!=-1) {	//-1 means end of file
				goto retry;
			}
		}
		if (f2!=NULL) {
			fclose(f2);
			f2=NULL;
		}
		//Write the rest of the buffer to a .tmp file and then rename it to the buffer file, to get rid of the first line of text.
		f2 = fopen(tmpbuffer, "wt");
		int stuffRemains=0;
		for (c=fgetc(f); c!=-1; c=fgetc(f)) {
			fputc((char) c, f2);
			stuffRemains=1;
		}
		if (f2!=NULL) {
			fclose(f2);
			f2=NULL;
		}
		if (f!=NULL) {
			fclose(f);
			f=NULL;
		}
		remove(outputbuffer);
		if (stuffRemains) {
			rename(tmpbuffer, outputbuffer);
		}
	}
}
