
/* Generated by Interface Builder */

#import "chaosStorage.h"
#import <objc/objc-load.h>


/**
 **	which-bin -- find the path for the named exectuable files
 **
 **	Lennart <Lovstrand@EuroPARC.Xerox.COM>, September 1, 1991.
 ** slightly modified by sam 910902
 **/

char * which_bin(const char *file)
{
    DIR *dir;
    struct direct *de;
    static char buf[MAXPATHLEN];
    char *paths, *p, *q;
    int filelen, dirlen;

    paths = getenv("PATH");
    if (paths == NULL)
	return NULL;

    filelen = strlen(file);
    for (p = q = paths; *q != '\0'; p = q + 1)
	{
		q = index(p, ':');
		if (q == NULL) q = p + strlen(p);

		dirlen = q - p;
		strncpy(buf, p, dirlen);
		buf[dirlen] = '\0';

		dir = opendir(buf);
		if (dir == NULL) continue;

		buf[dirlen] = '/';
		dirlen++;

		while ((de = readdir(dir)) != NULL)
		{
			if (de->d_namlen != filelen || strcmp(de->d_name, file) != 0)
				continue;

			strcpy(buf + dirlen, file);
			if (access(buf, X_OK) != 0) continue;

			closedir(dir);
			return buf;
		}

		closedir(dir);
    }
	return NULL;
}

void getAppDirectory (char *appDirectory)
{
    char *suffix, *path;

    strcpy (appDirectory,NXArgv[0]);

    suffix = rindex(appDirectory,'/');
    if (suffix != NULL)			// explicit path
	{ 
		*suffix  = '\0'; 		// remove executable name
    }
	else
	{
		path = which_bin([NXApp appName]);
		if (path != NULL)
			strcpy(appDirectory, path);
		else
			// Don't know where we came from -- arbitrarily
			// presume "." and continue (it's better than breaking)
			strcpy(appDirectory, ".");
    }  
}

@implementation chaosStorage

- chaosSelect:sender
{
    return self;
}

- chaosLoad:sender
{
    char launchdir[256];

	//[chaosMenu setTitle:"Circle"];
	getAppDirectory (&launchdir[0]);
printf("LD %s\n", launchdir);
	[self loadViewsFrom: &launchdir[0]];
	[self loadViewsFrom: VIEWDIRECTORY];
	[[chaosMenu target] setTarget:sender];
	[[chaosMenu target] setAction:@selector(selectChaos:)];
    return self;
}

//
//  Dynamically load all object files found in the specified directory
//
// MEMO: Must make sure this doesn't screw up when it finds a new
// object file with a class that's already loaded!!!!
- loadViewsFrom: (const char *) dirname
{
    DIR *dir;
    struct direct *de;
    NXStream *nxstderr;
    char path[MAXPATHLEN];
    char *filenames[] = {path, NULL};
    const char *NewTitle;

    nxstderr = NXOpenFile(2, NX_WRITEONLY);

    dir = opendir(dirname);
    if (dir == NULL) 
    	{
		perror(dirname);
		return self;
   	 }

    while ((de = readdir(dir)) != NULL)
	{
		BOOL ok;
	
		// Ignore '.'-files (not really necessary, I guess)
		if (de->d_name[0] == '.')
			continue;

		// Check that the file has the right extension
//printf("Here 1\n");
		if (de->d_namlen < 3 ||
				strcmp(&de->d_name[de->d_namlen-7], ".Chaoso") != 0)
			continue;
printf("Here Loaded\n");
		// refuse to load if the name matches a module already loaded
		//numstrings = [viewNames count];
		ok = YES;
		strcpy(path, de->d_name);
		//*(rindex(path, '.')) = '\0';
		//
		//for (i=0; i< numstrings; i++)
		//{
		//	if (!strcmp(path, [viewNames elementAt:i]))
		//	{	ok = NO;
		//		break;
		//	}
		//}
		//if (!ok) continue;
		

		// OK, all is well -- go load the little bugger
		sprintf(path, "%s/%s", dirname, de->d_name);
		if (objc_loadModules(filenames, nxstderr, NULL, NULL, NULL) == 1)
			// Ugh, failed.  Continue as usual.
			continue;

		// Smash out the 'V' in ".../FooView.o"
		*(rindex(path, '.')) = '\0';
		//[viewNames addElement: rindex(path, '/') + 1];
		NewTitle = rindex(path, '/') + 1;
		[[chaosMenu target] addItem:NewTitle];
    }

    closedir(dir);

    NXClose(nxstderr);

    return self;
}

- accessViewList: sender
{
	id myClass;
	const char *ptr;
	
	//if ([whichList objectAt:virtualViewIndex] == self)
	//{
	//	id myClass;
	//	char str[50];
	//	const char *ptr;
	//	
		ptr = [[chaosMenu target] selectedItem];
printf("newchaos will be %s\n", ptr);
		myClass = objc_getClass(ptr);

	//	theView = [[myClass alloc] init];
	//	if (theView) [whichList replaceObjectAt:(virtualViewIndex) with:theView];
	//}

	//theView = [whichList objectAt:(virtualViewIndex)];
	//if (theView == self) theView = nil;
	
	return myClass;
}

@end
