#import <objc/Object.h>
#import <objc/NXString.h>
#import <streams/streams.h>
#import <stdio.h>
#import <mach/mach.h>
#import <objc/objc-load.h>

int staticA = 5;

@interface Foo : Object
{
    int a;
    char *b;
    int staticA;
}

-(int)a;
-(void)setA: (int)theA;

@end

@implementation Foo

-(int)a { return a; }
-(void)setA: (int)theA { a = theA; }

@end

void loadIt(char *file)
{
    char *objs[] = {file, 0};
    char *debugFile = alloca(strlen(file) + 5);
    struct mach_header address;
    NXStream *stream = NXOpenFile(fileno(stdout), NX_WRITEONLY);

    strcpy(debugFile, file);
    strcat(debugFile, ".deb");    
    objc_loadModules(objs, stream, NULL, &address, debugFile);
    printf("%s loaded at %p.\n", file, address);
    NXClose(stream);
}

int main(int argc, char **argv)
{
    id foo = [Foo new];
    int a;
    char *test = malloc(6);
    id theDyn;
    Class dynClass;
    char *fooString = "abcdef";
    int keepGoing = ((argc > 1) && (strcmp(argv[1], "stop") == 0));
    NXString *aString = @"Foo";
    
    while (keepGoing);
    
    [foo setA: 4];
    a = [foo a];
    loadIt("Dyn.o");
    loadIt("Dyn1.o");
    dynClass = objc_getClass("Dyn");
    theDyn = [dynClass new];
    [theDyn setC: 5];
    objc_unloadModules(NULL, NULL);
    return 1;
}

/*
void foofoo()
{
    int i = 1;
    printf("i is %d\n", i);
}
*/