/* 
   NSProcessInfo.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <config.h>

#include <stdio.h>

#if HAVE_LIBC_H
# include <libc.h>
#else
# include <unistd.h>
#endif

#include <Foundation/NSString.h>
#include <Foundation/NSPathUtilities.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSException.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/StackZone.h>

/*
 * Static global vars
 */

static NSString* operatingSystem = @TARGET_PLATFORM;

// The shared NSProcessInfo instance
static NSProcessInfo* processInfo = nil;

// Host name of the CPU executing the process
static NSString* hostName = nil;   

// Current process name
static NSString* processName = nil;

// Array of NSStrings (argv[1] .. argv[argc-1])
static NSArray* arguments = nil;

// Dictionary of environment vars and their values
static NSDictionary* environment = nil;

// Use a special zone to keep the environment variables to make possible
// debugging when we use the DejaGnu tests. This zone is allocated big enough
// to keep all the needed objects.
static NSZone* environmentZone = nil;

/*
 * NSProcessInfo implementation
 */

@implementation NSProcessInfo

+ (void)initializeWithArguments:(char**)argv
    count:(int)argc
    environment:(char**)env
{
    int i;
    char str[1024];
    NSAutoreleasePool* pool;
    
    if (processInfo)
	return;

    /* Create an autorelease pool since there might be no one in effect
       at this moment. */
    pool = [[NSAutoreleasePool alloc] init];

    /* Getting the process name */
    processName = [NSString stringWithCStringNoCopy:argv[0] freeWhenDone:NO];
    [processName retain];

    /* Create a zone big enough to hold all the environment variables. This
       should be done to make the program behaves exactly the same when it
       is run from DejaGnu, gdb or command line. */
#if 1
    environmentZone = NSDefaultMallocZone();
#else
    environmentZone = [[StackZone alloc]
			    initForSize:128000 granularity:0 canFree:NO];
#endif

    /* Copy the argument list */
    {
	id argstr[argc - 1];
	for (i = 1; i < argc; i++) {
	    argstr[i - 1] = [[[NSString allocWithZone:environmentZone]
		initWithCString:argv[i]] autorelease];
	}
	arguments = [[NSArray allocWithZone:environmentZone]
	    initWithObjects:argstr count:argc - 1];
    }
    
    /* Copy the evironment list */
    for (argc = 0; env[argc]; argc++)
	/* nothing */ ;
    {
	id keys[argc];
	id vals[argc];
	
	for (i = 0; env[i]; i++) {
	    char* cp;
	    for (cp = env[i]; *cp != '=' && *cp; cp++);
	    *cp = '\0';
	    vals[i] = [[NSString allocWithZone:environmentZone]
				initWithCString:(cp+1)];
	    keys[i] = [[NSString allocWithZone:environmentZone]
			    initWithCString:env[i]];
	    *cp = '=';
	}
	environment = [[NSDictionary allocWithZone:environmentZone]
	    initWithObjects:vals forKeys:keys count:argc];
    }
    
    gethostname(str, 1024);
    hostName = [[NSString allocWithZone:environmentZone] initWithCString:str];

    processInfo = [[self allocWithZone:environmentZone] init];

    [pool release];
}

+ (NSString*)operatingSystem
{
    return operatingSystem;
}

+ (NSProcessInfo*)processInfo
{
    if (!processInfo) {
	fprintf (stderr, "You must call +[NSProcessInfo "
			 "initializeWithArguments:count:environment:] "
			 "in main()\n");
	exit (1);
    }
    return processInfo;
}

- (id)init
{
    return self;
}

- (NSArray*)arguments
{
    return arguments;
}

- (NSDictionary*)environment
{
    return environment;
}

- (NSString*)hostName
{
    return hostName;
}

- (NSString*)processName
{
    return processName;
}

- (NSString*)globallyUniqueString
{
    id str = [NSMutableString stringWithCString:""];
    id dat = [[NSCalendarDate alloc] init];
    
    [dat setCalendarFormat:@"%d-%m-%Y-%H:%M:%S"];
    [str appendFormat:@"%s:%d:[%s]", 
	    [hostName cString], (int)getpid(), [dat description]];
    [dat release];
    return str;
}

- (void)setProcessName:(NSString*)aName
{
    if (aName && [aName length]) {
	[processName autorelease];
	processName = [aName copy];
    }
}

/*
 * internal class that cannot be deleted
 */

- (id)autorelease
{
    return self;
}

- (void)release
{
    return;
}

- (id)retain
{
    return self;
}

@end
