/*
        Copyright (c) 1996, NeXT Software, Inc.
        All rights reserved.

        You may freely copy, distribute and reuse the code in this example.
        NeXT disclaims any warranty of any kind, expressed or implied,
        as to its fitness for any particular use.
*/
#import <EOAccess/EOAccess.h>

void usage(void);

void fetch(void)
{
  NSString *modelPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"model"];
  NSString *entityName = [[NSUserDefaults standardUserDefaults] stringForKey:@"entity"];
  EOEditingContext *editingContext = [EOEditingContext new]; // context with defaultCoordinator

  NSArray *results;

  if(!modelPath || !entityName) {
    usage();
    return;
  }

  // For apps with models stored in the main bundle, the following is
  // unnecessary, since the defaultGroup will be automatically built from
  // the contents of the main bundle and and any referenced frameworks.
  [EOModelGroup setDefaultGroup:[[EOModelGroup new] autorelease]];
  [[EOModelGroup defaultGroup] addModelWithFile:modelPath];

  // Fetch objects from our context
  results = [editingContext objectsWithFetchSpecification:[EOFetchSpecification fetchSpecificationWithEntityName:entityName qualifier:nil sortOrderings:nil]];
  
  NSLog(@"Result: %@", results);
}

void usage(void)
{
    NSLog(@"Usage: fetch -model <full path for an eomodeld file> -entity <entity name>");
}


int main (int argc, const char *argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   NS_DURING
     fetch();
   NS_HANDLER
     NSLog(@"%@", [localException reason]);
   NS_ENDHANDLER

   [pool release];
   exit(0);       // insure the process exit status is 0
   return 0;      // ...and make main fit the ANSI spec.
}

