/*
    bundle - Test out dynamic linking

*/
#include <sys/param.h>
#include "appkit/NXBundle.h"
#include "LoadMe.bproj/LoadMe.h"
#include "LoadMe.bproj/SecondClass.h"
#include "LoadMe.bproj/MyCategory.h"
/* Realize externals that objcX should have */
#define NXArgv NSArgv		/* latest definition? */
char     **NXArgv;

int 
main(int ac, char *av[]) 
{
    id main;
    id bundle;
    id object;
    BOOL ok;
    char path[MAXPATHLEN+1];
    
    NXArgv = av;

    main = [NXBundle mainBundle];
    printf("Looking for main bundle...\n");
    if (!main) {
	printf("* ERROR: Can't get main bundle\n");
	exit(1);
    }
    printf("  Main bundle directory is %s\n", [main directory]);

    printf("Looking for LoadMe bundle...\n");
    ok = [main getPath:path forResource:"LoadMe" ofType:"bundle"];
    if (!ok) {
	printf("* ERROR: Can't find LoadMe bundle in main bundle\n");
	exit(1);
    }
    printf("  Found LoadMe in: %s\n\n", path);

    printf("Initializing LoadMe bundle...\n");
    bundle = [[NXBundle alloc] initForDirectory:path];
    if (!bundle) {
	printf("* ERROR: Can't init LoadMe bundle\n");
	exit(1);
    }
    ok = [bundle getPath:path forResource:"readme" ofType:"txt"];
    if (!ok) {
	printf("* ERROR: Can't find readme in LoadMe bundle\n");
	exit(1);
    }
    printf("  Found readme file: %s\n\n", path);

    printf("Retreiving principal class...\n");
    object = [bundle principalClass];
    if (!object) {
	printf("* ERROR: Can't find principal class\n");
        printf("  Either there is a problem with dynamic loading, or there is\n");
	printf("  no dynamic loader on your system\n");
	exit(1);
    }
    printf("  Principal class is: %s\n", [object name]);

    printf("Testing LoadMe bundle classes...\n");
    printf("  This is LoadMe:\n");
    object = [[[bundle classNamed:"LoadMe"] alloc] init];
    [object afterLoad];
    [object free];

    printf("\n  This is SecondClass:\n");
    object = [[[bundle classNamed:"SecondClass"] alloc] init];
    [object printName];
    [object printMyName];

    return 0;
}
