/*
		Specific.c for DOS
*/

/// Includes
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <conio.h>
#include "mytypes.h"
#include "tmiga.h"
#include "protos.h"
///
/// Prototypes
Local void main_cleanup(void);
///
/// Global variables
void *rdargs = NULL;
void *ReqWindow = NULL;
char *argarray[NumArgs];
char progPath[256];
void *scrnSave;
int cursx, cursy;
///

/// GetArgs
/* ---------------------------------- GetArgs ----------------------------------

 Comment: Read command line arguments

*/

//void GetArgs(int argc, char **argv)
int main(int argc, char **argv)
{
	BOOL gotFile = FALSE, gotConfig = FALSE, gotArg = FALSE;
	int p;
	static char argbuf[512] = "";
	char *defcfg;

	/* Save screen contents */
	scrnSave = malloc(25*80*2);
	if(scrnSave)
		if(gettext(1, 1, 80, 25, scrnSave) == 0)
			free(scrnSave);
	cursx = wherex();
	cursy = wherey();
	atexit(main_cleanup);

	/* Draw backdrop */
	gotoxy(1, 1); clrscr();
	textcolor(WHITE);
	textbackground(BLUE);
	clreol();
	cprintf(" %s", IDSTRING);
	gotoxy(63, 1); cprintf("By Richard Downer");
	gotoxy(1, 2);
	textcolor(DARKGRAY);
	textbackground(BLACK);
	for(p = 0; p < (80*23); p++)
		putch('°');
	textcolor(WHITE);
	textbackground(BLUE);
	clreol();
	gotoxy(1, 2);
	textcolor(WHITE);
	textbackground(BLACK);

	/* Process argv[0] */
	{
		char *p, sep[256];
		unsigned int q;
		int found = 0;

		strcpy(progPath, argv[0]);
		strcpy(sep, PATH_SEPERATOR);
		p = &progPath[strlen(progPath)-1];
		while (p >= progPath) {
			for (q = 0; q < strlen(sep); q++) {
				if(*p == sep[q]) {
					*p = 0;
					found = 1;
					p = progPath;
					break;
				}
			}
			p--;
		}
		if (found == 0) strcpy (progPath, ".");
	}

	defcfg = ConfigFilename ();
	arg_CONFIG = defcfg;

	if (argc < 1) {
		fail (20, "Tag-o-Miga: Bad arguments (try --help)");
	}

	for (p = 1; p < argc; p++) {
		if (strncmp (argv[p], "--help", 6) == 0 || strncmp (argv[p], "-?", 2) == 0) {
			AlertUser ("Usage:\nTAGOMIGA [/C configfile] editfile [editorargs]", "OK");
			return 0;
		} else if (strnicmp (argv[p], "/C", 2) == 0) {
			if (strlen (argv[p]) > 2) {
				arg_CONFIG = &argv[p][2];
			} else {
				if (gotConfig == TRUE) fail (20, "Option -c can only be specified once");
				gotConfig = TRUE;
				if (argc == p+1) {
					fail (20, "Option -c requires a filename");
				}
				arg_CONFIG = argv[p+1];
				p++;
			}
		} else if (gotFile == FALSE) {
			arg_EDITFILE = argv[p];
			gotFile = TRUE;
		} else {
			if (gotArg == TRUE) strcat (argbuf, " ");
			strcat (argbuf, argv[p]);
			gotArg = TRUE;
		}
	}
	arg_ARGS = argbuf;

	if (gotFile == FALSE) fail (20, "Bad arguments - try --help");

	realMain();

	return 0;
}
///

/// main_cleanup
void main_cleanup(void)
{
	if(scrnSave) {
		puttext(1, 1, 80, 25, scrnSave);
		free(scrnSave);
		gotoxy(cursx, cursy);
	} else {
		gotoxy(1, 1);
		clrscr();
	}
}
