/* nextispell.m */
/* 
 *
 * copyright by Moritz Willers
 *
 * Thu Jan 19 21:38:39 GMT+0100 1995
 *
 */

#define DATE "Thu Jan 19 21:38:39 GMT+0100 1995\n"
#define VERSION "Version 0.8\n"

#import <appkit/appkit.h>
#import "Dictionaire.h"
#import "common.h"
#import "defines.h"	/* 	this file containes the defines for 
						LANGUAGE and COMPILEDATE, it is generated
						by the makefile
					*/

void main(int argc, char **argv)
{
	char		ispellPath[MAXPATHLEN];
	struct stat	statbuf;    
	NXSpellServer	*aServer = [[NXSpellServer alloc] init];

    if (argc > 1)
    {
		if (!strcmp((argv[1]), "-v"))
		{
			printf("\nnextispell by Moritz Willers\n");
			printf("email: willers@butp.unibe.ch (NeXTMail, MIME)\n");
			printf(VERSION);
			printf("%s\n", DATE);
			printf("Compiled for language: %s\n", LANGUAGE);
			printf("Compiled on %s\n\n", COMPILEDATE);
			exit(0);
		} else
		{
			fprintf(stderr, "Usage: %s [-v]\n", *argv);
			exit(0);
		}
    }
	/* check whether I can start ispell before doing anything */
	if ( NXGetDefaultValue("nextispell", "ispell") )
		strcpy(ispellPath, NXGetDefaultValue("nextispell", "ispell"));
	else
		findispell(ispellPath);
	if ( !((stat(ispellPath, &statbuf) == 0 ) && ((statbuf.st_mode & S_IFMT) == S_IFREG) && (statbuf.st_mode & S_IREAD) && (statbuf.st_mode & S_IEXEC)) ) {
		NXApp = [Application new];
		NXRunAlertPanel("nextispell", "Can't find ispell executable. Check defaults.", NULL, NULL, NULL);
		[NXApp free];
		exit(1);
	}

			
	if ( ![aServer registerLanguage:LANGUAGE byVendor:"ispell"] ) {
		fprintf(stderr, "nextispell: Couldn't register language: %s\n", LANGUAGE);
		exit(1);
	}
	[aServer setDelegate:[[Dictionaire alloc] initForLanguage:LANGUAGE]];
	[aServer run];
	fprintf(stderr, "nextispell: Unexpected death. Presumbaly another nextispell for the same language (%s) is already running.\n", LANGUAGE);
}
