2005-03-01  Adam Fedor  <fedor@gnu.org>

	* Source/NSApplication.m: Add support for loading backend from a
	framework. 
	(gnustep_backend_framework, gnustep_backend_bundle): New functions
	(initialize_gnustep_backend): Use them.

Index: Source/NSApplication.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSApplication.m,v
retrieving revision 1.275
diff -u -r1.275 NSApplication.m
--- Source/NSApplication.m	28 Feb 2005 04:18:06 -0000	1.275
+++ Source/NSApplication.m	28 Feb 2005 17:12:08 -0000
@@ -152,6 +152,86 @@
 + (void) initializeBackend;
 @end
 
+/* Find and load the backend framework, if there is one. The name is
+   taken from a user default containing the name of the backend framework,
+   such as 'GNUstep-back', or simply 'back', or for historical reasons,
+   'libgnustep-back'.  */
+static NSString *
+gnustep_backend_framework (NSString *bundleName)
+{
+  NSEnumerator *benum;
+  NSString *path;
+  
+  if ( bundleName == nil )
+    bundleName = @"GNUstep_back.framework";
+  else
+    {
+      if ([bundleName hasPrefix: @"GNUstep-"])
+	bundleName = [bundleName stringByAppendingString: @".framework"];
+      else 
+	{
+	if  ([bundleName hasPrefix: @"libgnustep-"])
+	  {
+	    bundleName = [bundleName stringByDeletingPrefix: @"libgnustep-"];
+	  }
+	bundleName = [NSString stringWithFormat: @"GNUstep-%@.framework",
+			       bundleName];
+	} 
+    }
+  NSDebugFLLog(@"BackendBundle", @"Looking for %@", bundleName);
+  
+  /* Find the backend framework */
+  benum = [NSStandardLibraryPaths() objectEnumerator];
+  while ((path = [benum nextObject]))
+    {
+      path = [path stringByAppendingPathComponent: @"Frameworks"];
+      path = [path stringByAppendingPathComponent: bundleName];
+      if ([[NSFileManager defaultManager] fileExistsAtPath: path])
+	{
+	  break;
+	}
+    }
+  
+  return path;
+}
+
+/* Find and load the backend bundle, if there is one. The name is
+   taken from a user default containing the name of the backend bundle,
+   such as 'back', or for historical reasons, 'libgnustep-back'.  */
+static NSString *
+gnustep_backend_bundle(NSString *bundleName)
+{
+  NSEnumerator *benum;
+  NSString *path;
+  
+  if ( bundleName == nil )
+    bundleName = @"libgnustep-back.bundle";
+  else
+    {
+      if ([bundleName hasPrefix: @"libgnustep-"])
+	bundleName = [bundleName stringByAppendingString: @".bundle"];
+      else 
+	{
+	  bundleName = [NSString stringWithFormat: @"libgnustep-%@.bundle",
+				 bundleName];
+	} 
+    }
+  NSDebugFLLog(@"BackendBundle", @"Looking for %@", bundleName);
+  
+  /* Find the backend bundle */
+  benum = [NSStandardLibraryPaths() objectEnumerator];
+  while ((path = [benum nextObject]))
+    {
+      path = [path stringByAppendingPathComponent: @"Bundles"];
+      path = [path stringByAppendingPathComponent: bundleName];
+      if ([[NSFileManager defaultManager] fileExistsAtPath: path])
+	{
+	  break;
+	}
+    }
+  return path;
+}
+
 BOOL
 initialize_gnustep_backend(void)
 {
@@ -173,33 +253,23 @@
 #ifdef BACKEND_BUNDLE
       {      
 	NSBundle *theBundle;
-	NSEnumerator *benum;
 	NSString *path, *bundleName;
 	NSUserDefaults	*defs = [NSUserDefaults standardUserDefaults];
 
 	/* What backend ? */
 	bundleName = [defs stringForKey: @"GSBackend"];
-	if ( bundleName == nil )
-	  bundleName = @"libgnustep-back.bundle";
-	else
-	  bundleName = [bundleName stringByAppendingString: @".bundle"];
-	NSDebugFLLog(@"BackendBundle", @"Looking for %@", bundleName);
-
-	/* Find the backend bundle */
-	benum = [NSStandardLibraryPaths() objectEnumerator];
-	while ((path = [benum nextObject]))
+	path = gnustep_backend_framework (bundleName);
+	if (path == nil)
 	  {
-	    path = [path stringByAppendingPathComponent: @"Bundles"];
-	    path = [path stringByAppendingPathComponent: bundleName];
-	    if ([[NSFileManager defaultManager] fileExistsAtPath: path])
-	      {
-		break;
-	      }
+	    NSDebugLLog(@"BackendBundle", @"Did not find backend framework.");
+	    path = gnustep_backend_bundle (bundleName);
 	  }
 
 	/* FIXME/TODO - update localized error messages.  */
 
 	/* Backend found ? */
+	if (bundleName == nil)
+	  bundleName = @"back";
 	NSCAssert1(path != nil, _(@"Unable to find backend %@"), bundleName);
 	NSDebugLog(@"Loading Backend from %@", path);
 	NSDebugFLLog(@"BackendBundle", @"Loading Backend from %@", path);
