#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ctype.h>

#include <exec/exec.h>
#include <exec/types.h>
#include <clib/exec_protos.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>
#include <dos/dosextens.h>
#include <clib/alib_protos.h>

char *index = 0L;
char indexpath[256];
int messagect = 0;
FILE *indexfile = 0L;
unsigned long indexsize = 0L;

struct MsgPort *msg = 0L;

unsigned long fsize(char *fname)
{
    BPTR file;
    __aligned struct FileInfoBlock FileInfo;
    unsigned long filesize;

    if(!fname)
        return 0L;

    file = Lock(fname,ACCESS_READ);
    if(!file)
        return 0L;

    if(!(Examine(file,&FileInfo)))
        return 0L;

    filesize = FileInfo.fib_Size;
    UnLock(file);

    return filesize;
}

void openport(void)
{
    /* wait until we own the files */

    int safe = FALSE;

    while(!safe)
    {
        Disable();
        if(!(msg = FindPort("simplebb")))
        {
            if(msg = CreatePort("simplebb",0L))
                safe = TRUE;
        }
        Enable();

        Delay(50);
    }
}

main(int argc, char *argv[])
{
    openport();

    if(argc != 3)
    {
        printf("Simple WWW BBS Message Cleanup Utility\nwritten by Jeff Crystal\n\n");
        printf("USAGE: bbsclean <bbs path> <message count>\n\nwhere:\n  <bbs path> is the directory where the index.html file is.\n");
        printf("  <message count> is the maximum number of messages to keep.\n\n");
        DeletePort(msg);
        exit(5);
    }

    strcpy(indexpath,argv[1]);
    if( (indexpath[strlen(indexpath)-1] != ':') && (indexpath[strlen(indexpath)-1] != ':') )
    {
        indexpath[strlen(indexpath)+1] = '\0';
        indexpath[strlen(indexpath)] = '/';
    }


    /* Add file name to path */
    strcat(indexpath,"index.html");

    messagect = atoi(argv[2]);
    if(messagect <= 0)
    {
        printf("<message count> must be a whole number > 0\n\n");
        DeletePort(msg);
        exit(5);
    }

    indexsize = fsize(indexpath);
    if(indexsize <= 0)
    {
        printf("Could not open index file: %s\ncannot proceed!\n\n",indexpath);
        DeletePort(msg);
        exit(10);
    }

    /* Filename and size is valid, load the index file and prepare to process it */

    index = AllocVec(indexsize+1,MEMF_CLEAR);
    if(index == 0L)
    {
        printf("Could not allocate memory for index file!!\n");
        DeletePort(msg);
        exit(10);
    }

    if(!(indexfile = fopen(indexpath,"r")))
    {
        printf("Could not open index file!!\n\n");
        FreeVec(index);
        DeletePort(msg);
        exit(10);
    }

    char c,*dst;
    dst = index;
    while(!(feof(indexfile)))
    {
        c = fgetc(indexfile);
        if(!(feof(indexfile)))
            *dst++ = c;
        else
            *dst = '\0';
    }
    fclose(indexfile);
    indexfile = 0L;

    /* index is loaded, now find markers */

    int i;
    char *endofmessages;
    char *message;

    if(!(endofmessages = strstr(index,"<!--end-of-messages-->")))
    {
        printf("could not find end of messages!\n");
        FreeVec(index);
        DeletePort(msg);
        exit(10);
    }

    message = index;
    for(i=0; i<=messagect; i++)
    {

        if(!(message = strstr(message,"<!--message-->")))
        {
            /* total messages < messagect */
            printf("total messages < <message count>\n");
            FreeVec(index);
            DeletePort(msg);
            exit(0);
        }
        message += strlen("<!--message-->");
    }
    message -= strlen("<!--message-->");
    /* message is pointing to the end of the last message to be kept */


    if(!(indexfile = fopen(indexpath,"w")))
    {
        printf("Cannot open index file for write!\n");
        FreeVec(index);
        DeletePort(msg);
        exit(10);
    }

    char *src = index;
    while(src < message)
        fputc(*src++,indexfile);

    src = endofmessages;
    while(*src != '\0')
        fputc(*src++,indexfile);

    fclose(indexfile);
    FreeVec(index);
    printf("Exit Successfully!\n");
    DeletePort(msg);
    exit(0);
}