#include <andrewos.h>
#include <class.h>
#include <sys/stat.h>

#include <amsconf.ih>
#include <extlist.ih>
#include <msgdb.ih>
#include <mhfolder.ih>

#include <unmhmlbox.eh>

#define BUFFSIZE 10000

static boolean canUseColonSeparator;

boolean unixmhmailbox__InitializeClass(classid)
struct classheader *classid;
{
    canUseColonSeparator = amsconfiguration_GetBoolean("AMS_AllowColonInSeparatingFrom");
    return TRUE;
}

boolean unixmhmailbox__InitializeObject(classid, self)
struct classheader *classid;
struct unixmhmailbox *self;
{
    return TRUE;
}

void unixmhmailbox__FinalizeObject(classid, self)
struct classheader *classid;
struct unixmhmailbox *self;
{
   
}

boolean unixmhmailbox__IsThereNewMail(self)
struct unixmhmailbox *self;
{
    char *mailPath = unixmhmailbox_GetSource(self);
    struct stat stbuf;

    /* TODO: better check for mail file */

     return stat(mailPath, &stbuf) == 0 && stbuf.st_size != 0;
}

long unixmhmailbox__CountOfNewMail(self)
struct unixmhmailbox *self;
{
    char *mailPath = unixmhmailbox_GetSource(self);
    FILE *mailFile;
    long count = 0;
    char buffer[BUFFSIZE];

    if (mailPath != NULL && *mailPath != '\0')  {
	mailFile = fopen(mailPath, "r");

	if (mailFile != NULL) {
	    while (fgets(buffer, BUFFSIZE, mailFile) != NULL) {
		if (strncmp(buffer, "From", 4) == 0) {
		    if (buffer[4] == ' ' || (canUseColonSeparator && buffer[4] == ':' && buffer[5] == ' ')) {
			count++;
		    }
		}
	    }
	    fclose(mailFile);
	}
    }

    return count;
}

static void *CopyString(str)
char *str;
{
    char *newStr;

    newStr = (char *) malloc(strlen(str) + 1);
    strcpy(newStr, str);
    return (void *) newStr;
}

boolean unixmhmailbox__ProcessNewMail(self, numGood, numBad, numLocked, firstError, errorBuf, bufferSize)
struct unixmhmailbox *self;
long *numGood;
long *numBad;
long *numLocked;
long *firstError;
char *errorBuf;
long bufferSize;
{
    struct msgdatabase *database;
    struct mhfolder *folder;

    if (numGood != NULL) {
	*numGood = 0;
    }
    if (numBad != NULL) {
	*numBad = 0;
    }
    if (numLocked != NULL) {
	*numLocked = 0;
    }
    if (firstError != NULL) {
	*firstError = 0;
    }

    /* TODO : really process the new mail */

    database = unixmhmailbox_GetDatabase(self);
    if (database != NULL) {
	folder = (struct mhfolder *) msgdatabase_GetFolder(database, unixmhmailbox_GetFolderName(self));
	if (folder != NULL) {
	    char command[5000];
	    FILE *incFile;

	    mhfolder_Scan(folder, FALSE);

	    /*
	     TODO: not sure this is what I really want to do
		 might be better to parse the file ourselves
		   or at least use a process that picks up
		   errors better.
		   */

	    sprintf(command, "inc -file %s +%s -format \"%%(msg)\" -truncate", unixmhmailbox_GetSource(self), mhfolder_GetPath(folder));

	    if ((incFile = popen(command, "r")) != NULL) {
		struct extendedlist *fileList;
		long count;
		char buffer[100];

		fileList = extendedlist_Create(CopyString, free);

		while (fgets(buffer, 1000, incFile) != NULL) {
		    char *buf = buffer;
		    if (*buf >= '1' && *buf <= '9') {
			for (buf++; *buf >= '0' && *buf <= '9'; buf++) {
			}
			*buf = '\0';
			extendedlist_AddElement(fileList, buffer);
		    }
		}
		pclose(incFile);
		count = extendedlist_NumberOfElements(fileList);
		if (count != 0) {
		    if (numGood != NULL) {
			*numGood = count;
		    }
		    mhfolder_AddMessageFiles(folder, fileList);
		}
		extendedlist_Destroy(fileList);
		return FALSE;
	    }
	}
    }
    /* TODO: Really handle the errors here */
    return TRUE;
}


