/* Copyright (c) 1996 by Lele Gaifax.  All Rights Reserved
 *
 * This software may be used and distributed freely for any purpose
 * provided that this notice is included unchanged on any and all
 * copies. The author does not warrant or guarantee this software in
 * any way.
 *
 * This file is part of the PyObjC package.
 *
 * $RCSfile: OC_PythonBundle.m,v $
 * $Revision: 1.1.1.1 $
 * $Date: 1996/10/23 17:19:52 $
 *
 * Created Wed Oct 16 17:39:54 1996.
 */

#include <sys/param.h>
#include "Python.h"
#include "ObjC.h"
#include "OC_PythonBundle.h"
#include "ceval.h"
#include "compile.h"
#include "frameobject.h"

#define CLASS_VERSION 0

@implementation OC_PythonBundle

+ (void) initialize
{
  if (self == [OC_PythonBundle class])
    {
      [OC_PythonBundle setVersion:CLASS_VERSION];
    }
}

+ (OC_PYTHONBUNDLE_SUPER_CLASS *) mainBundle
{
  static OC_PYTHONBUNDLE_SUPER_CLASS *mainScriptBundle = nil;

  if (!mainScriptBundle)
    {
      PyFrameObject *frame = (PyFrameObject *) PyEval_GetFrame();
      const char *mainscript;

      /* go back to the first frame */
      while (frame->f_back)
	frame = frame->f_back;
      
      mainscript = PyString_AsString (frame->f_code->co_filename);
      if (strcmp (mainscript, "<stdin>"))
	{
	  const char *lslash = strrchr (mainscript, '/');
	  
	  if (lslash)
	    {
	      unsigned int len = lslash - mainscript;
	      char pathbuf[len + 1];

	      memcpy (pathbuf, mainscript, len);
	      pathbuf[len] = 0;

	      mainScriptBundle = [OC_PYTHONBUNDLE_SUPER_CLASS alloc];
#ifndef WITH_FOUNDATION
	      [mainScriptBundle initForDirectory:pathbuf];
#else
	      [mainScriptBundle initWithPath:
				  [NSString stringWithCString:pathbuf]];
#endif
	    }
	}

      if (!mainScriptBundle)
	{
	  char cwdbuf[1024];
	  extern const char *getcwd (char *, unsigned int);
	  
	  if (getcwd (cwdbuf, sizeof (cwdbuf)))
	    {
	      mainScriptBundle = [OC_PYTHONBUNDLE_SUPER_CLASS alloc];
#ifndef WITH_FOUNDATION
	      [mainScriptBundle initForDirectory:cwdbuf];
#else
	      [mainScriptBundle initWithPath:
				  [NSString stringWithCString:cwdbuf]];
#endif
	    }
	}
    }

  return mainScriptBundle;
}

@end /* OC_PythonBundle class implementation */

#ifndef WITH_FOUNDATION

@implementation NXBundle (OC_PythonBundle)

/* XXX I think this approach is safe, since these methods will almost
   surely performed through the PyObjC module, and it will convert the
   return value to a PyStringObject immediately. */
static char pathbuf[MAXPATHLEN+1];

+ (const char *) pathForResource:(const char *) name
			  ofType:(const char *) ext
		     inDirectory:(const char *) bundlePath
		     withVersion:(int) version
{
  if ([self getPath:pathbuf
	    forResource:name
	    ofType:ext
	    inDirectory:bundlePath
	    withVersion:version])
    return pathbuf;
  else
    {
#define ERRMSG "Resource `%s' of type `%s' is not available within `%s'"
      char errbuf[sizeof ERRMSG + strlen (name) + strlen (ext) + strlen (bundlePath)];

      sprintf (errbuf, ERRMSG, name, ext, bundlePath);
      PyErr_SetString (ObjC_Error, errbuf);
#undef ERRMSG
      return NULL;
    }
}
  
- (const char *) pathForResource:(const char *) name
			  ofType:(const char *) ext
{
  if ([self getPath:pathbuf
	    forResource:name
	    ofType:ext])
    return pathbuf;
  else
    {
#define ERRMSG "Resource `%s' of type `%s' is not available within `%s'"
      const char *bundlePath = [self directory];
      char errbuf[sizeof ERRMSG + strlen (name) + strlen (ext) + strlen (bundlePath)];

      sprintf (errbuf, ERRMSG, name, ext, bundlePath);
      PyErr_SetString (ObjC_Error, errbuf);
#undef ERRMSG
      return NULL;
    }
}
  
#endif

@end

/*
** Local Variables:
** change-log-default-name:"../ChangeLog.PyObjC"
** End:
*/
