//
//  Test.m
//
//  Copyright 2006 Andreas Faerber. All rights reserved.
//

#include <mono/jit/jit.h>
#include <crt_externs.h>



/*int MyClassHandler(const char* className) {
	printf("Looking up '%s'\n", className);
	return 0;
}*/


MonoDomain* domain = NULL;

static void main_thread_handler(gpointer user_data) {
	char* file = (char*) user_data;
	//printf("in main_thread_handler\n");
	//printf("%s\n", file);
	
	mono_config_parse(NULL);
	MonoAssembly* assembly = mono_domain_assembly_open(domain, file);
	if (assembly != NULL) {
		//printf("mono_jit_exec\n");
		//char* sz = (char*) file;
		mono_jit_exec(domain, assembly, 1, &file);
	} else printf("assembly NULL\n");
}


@interface PBXLSPlugin : NSObject {
}
+ (void)pluginDidLoad:(NSBundle*)bundle;
// incomplete!
@end

char** environ = NULL;

@interface MCSPlugin : PBXLSPlugin {
}
@end

@implementation MCSPlugin

/*+ (void)initialize {
	printf("MCSPlugin +initialize\n");
}*/

+ (void)pluginDidLoad:(NSBundle*)bundle {
	printf("+pluginDidLoad:\n");

	environ = *_NSGetEnviron();
	
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	char* assembly = strdup([[bundle pathForResource:@"Test" ofType:@"exe"] UTF8String]);
	[pool release];
	
	mono_set_dirs(NULL, NULL);

	
	printf("mono_jit_init\n");
	domain = mono_jit_init(assembly);
	printf("mono_runtime_exec_managed_code\n");
	mono_runtime_exec_managed_code(domain, main_thread_handler, assembly);
}

@end
