#include <andrewos.h>
#include <class.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>
/* #include <file.ih> */
#include <extlist.ih>
#include <directory.eh>

boolean directory__InitializeObject(classid, self)
struct classheader *classid;
struct directory *self;
{
    return TRUE;
}

void directory__FinalizeObject(classid, self)
struct classheader *classid;
struct directory *self;
{
}

struct directory *directory__Get(classid, path)
struct classheader *classid;
char *path;
{
    struct stat stbuf;
    struct directory *dir = NULL;

    if (stat(path, &stbuf) == NULL
	 && (stbuf.st_mode & S_IFMT) == S_IFDIR) {
	dir = directory_New();
	if (dir != NULL) {
	    directory_SetPath(dir, path);
	}
    }
    return dir;
}

struct directory *directory__Create(classid, path, protection)
struct classheader *classid;
char *path;
long protection;
{
    struct directory *dir;

    if ((dir = directory_Get(path)) != NULL) {
	directory_Destroy(dir);
	return NULL;
    }
    if (mkdir(path, protection) == 0) {
	return directory_Get(path);
    }
    else {
	return NULL;
    }
}

static char *ForEach(self, fileProc, rock, includeNoStat, includeAll, includeDirs, includeFiles)
struct directory *self;
boolean (*fileProc)();
void *rock;
boolean includeNoStat;
boolean includeAll;
boolean includeDirs;
boolean includeFiles;
{
    DIR *dir;
    struct dirent *dirent;
    struct stat stbuf;
    struct stat *pstbuf;
    char *fileName;
    char fullFileName[MAXPATHLEN];
    char *path = directory_GetPath(self);

    pstbuf = (includeNoStat) ? NULL : &stbuf;
    dir = opendir(path);
    if (dir != NULL) {
	strcpy(fullFileName, path);
	fileName = &fullFileName[strlen(path)];
	*fileName++ = '/';
	
	while ((dirent = readdir(dir)) != NULL) {
	    if (dirent->d_name[0] == '.'
		&& (dirent->d_name[1] == '\0'
		    || (dirent->d_name[1] == '.'
			&& dirent->d_name[2] == '\0'))) {
		continue;
	    }
	    strcpy(fileName, dirent->d_name);
	    if (includeNoStat
		|| (stat(fullFileName, pstbuf) == 0
		    && (includeAll
			|| (includeDirs
			    && (stbuf.st_mode & S_IFMT) == S_IFDIR)
			|| (includeFiles
			    && (stbuf.st_mode & S_IFMT) == S_IFREG)))) {
		if ((*fileProc)(self, dirent->d_name, fullFileName, pstbuf, rock)) {
		    char *fileName;

		    fileName = (char *) malloc(strlen(dirent->d_name) + 1);
		    strcpy(fileName, dirent->d_name);
		    return fileName;
		}
	    }
	}

	closedir(dir);
    }

    return NULL;
}


char *directory__ForEach(self, fileProc, rock)
struct directory *self;
boolean (*fileProc)();
void *rock;
{
    return ForEach(self, fileProc, rock, TRUE, TRUE, TRUE, TRUE);
}

char *directory__ForEachSubDirectory(self, fileProc, rock)
struct directory *self;
boolean (*fileProc)();
void *rock;
{
    return ForEach(self, fileProc, rock, FALSE, FALSE, TRUE, FALSE);
}

char *directory__ForEachFile(self, fileProc, rock)
struct directory *self;
boolean (*fileProc)();
void *rock;
{
    return ForEach(self, fileProc, rock, FALSE, FALSE, FALSE, TRUE);
}

static boolean AddElement(dir, name, fullName, pstbuf, list)
struct directory *dir;
char *name;
char *fullName;
struct stat *pstbuf;
struct extendedlist *list;
{
    extendedlist_AddElement(list, name);
    return FALSE;
}

struct extendedlist *directory__GetList(self)
struct directory *self;
{
    struct extendedlist *list;

    list = extendedlist_CreateStringList();
    directory_ForEach(self, AddElement, list);

    return list;
}

struct extendedlist *directory__GetSubDirectoryList(self)
struct directory *self;
{
    struct extendedlist *list;

    list = extendedlist_CreateStringList();
    directory_ForEachSubDirectory(self, AddElement, list);

    return list;
}

struct extendedlist *directory__GetFileList(self)
struct directory *self;
{
    struct extendedlist *list;

    list = extendedlist_CreateStringList();
    directory_ForEachFile(self, AddElement, list);

    return list;
}

char *directory__GetFullFileName(self, name, buffer)
struct directory *self;
char *name;
char *buffer;
{
    char *fullName;
    char *path = directory_GetPath(self);

    fullName = (buffer == NULL)
      ? (char *) malloc(strlen(path) + strlen(name) + 2)
      : buffer;

    sprintf(fullName, "%s/%s", path, name);
    return fullName;
}

struct file *directory__GetFile(self, name)
struct directory *self;
char *name;
{
    /* Fill this in */

    return NULL;
}

struct file *directory__CreateFile(self, name, protection)
struct directory *self;
char *name;
long protection;
{
    /* TODO: Fill this in */
    return NULL;
}

