/* 
   NSAccount.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 <sys/types.h>
#include <pwd.h>
#include <grp.h>

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

#include <Foundation/NSString.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSUtilities.h>

#include "NSAccount.h"


@implementation NSAccount

// Creating an account

+ currentAccount 
{
    [self subclassResponsibility:_cmd];
    return nil; 
}

+ accountWithName:(NSString*)name
{
    [self subclassResponsibility:_cmd];
    return nil; 
}

+ accountWithNumber:(unsigned int)number
{
    [self subclassResponsibility:_cmd];
    return nil; 
}

// Getting account information

- (NSString*)accountName
{
    [self subclassResponsibility:_cmd];
    return nil; 
}

- (unsigned)accountNumber
{
    [self subclassResponsibility:_cmd];
    return 0; 
}

// Copying Protocol

- copy
{
    return [self retain];
}

- copyWithZone:(NSZone*)zone
{
    return [self retain];
}

@end /* NSAccount */

/*
 *  User Account
 */

@implementation NSUserAccount

// Init & dealloc

- initWithPasswordStructure:(struct passwd*)ptr
{
    name = [[NSString stringWithCString:ptr->pw_name] retain];
#if defined(linux)
#warning The user info stuff commented out!
    fullName = [[NSString stringWithCString:ptr->pw_gecos] retain];
    homeDirectory = [[NSString stringWithCString:ptr->pw_dir] retain];
#endif
    userNumber = ptr->pw_uid;

    return self;
}

- (void)dealloc
{
    [name release];
    [fullName release];
    [homeDirectory release];
    [super dealloc];
}

// Creating an account

static NSUserAccount* currentUserAccount = nil;

+ currentAccount 
{
    // THREAD
    if (!currentUserAccount)
	currentUserAccount = [[self accountWithNumber:getuid()] retain];
    return currentUserAccount;
}

+ accountWithName:(NSString*)aName
{
    // THREAD
    struct passwd* ptr = getpwnam((char*)[aName cString]);
    
    return ptr ? 
	[[[self alloc] initWithPasswordStructure:ptr] autorelease]
	: nil;
}

+ accountWithNumber:(unsigned int)number
{
    // THREAD
    struct passwd* ptr = getpwuid(number);
    
    return ptr ? 
	[[[self alloc] initWithPasswordStructure:ptr] autorelease]
	: nil;
}

- (NSString*)accountName	{return name;}
- (unsigned)accountNumber	{return userNumber;}
- (NSString*)fullName		{return fullName;}
- (NSString*)homeDirectory	{return homeDirectory;}

@end /* NSUserAccount */

/*
 *  Group Account
 */

@implementation NSGroupAccount

// Init & dealloc

- initWithGroupStructure:(struct group*)ptr
{
    int cnt;

    name = [[NSString stringWithCString:ptr->gr_name] retain];
    groupNumber = ptr->gr_gid;

    // count group members
    for (cnt = 0; ptr->gr_mem[cnt]; cnt++)
	;
    
    {
	NSString* array[cnt];
	int i;
	
	for (i = 0; i < cnt; i++)
	    array[i] = [NSString stringWithCString:ptr->gr_mem[i]];
	members = [[NSArray alloc] initWithObjects:array count:cnt];
    }
    
    return self;
}

- (void)dealloc
{
    [name release];
    [members release];
    [super dealloc];
}

// Creating an account

static NSGroupAccount* currentGroupAccount = nil;

+ currentAccount 
{
    // THREAD
    if (!currentGroupAccount)
	currentGroupAccount = [[self accountWithNumber:getgid()] retain];
    return currentGroupAccount;
}

+ accountWithName:(NSString*)aName
{
    // THREAD
    struct group* ptr = getgrnam((char*)[aName cString]);
    
    return ptr ? 
	[[[self alloc] initWithGroupStructure:ptr] autorelease]
	: nil;
}

+ accountWithNumber:(unsigned int)number
{
    // THREAD
    struct group* ptr = getgrgid(number);
    
    return ptr ? 
	[[[self alloc] initWithGroupStructure:ptr] autorelease]
	: nil;
}

- (NSString*)accountName	{return name;}
- (unsigned)accountNumber	{return groupNumber;}
- (NSArray*)members		{return members;}

@end /* NSGroupAccount */
