#include "bbs.h"

static void DisplayCallerslog(char *filename,long tf)
{
 #define MEMSIZE 4096
 long stat,stat2,loop,lnlp=0,ReadSize,CurrentPos;
 UBYTE *buf;
 BPTR fh;

 ReadSize=MEMSIZE;
 LineCount=gnsflag=0;
 if(tf)  gnsflag=1;

 if(buf=AllocMem(MEMSIZE+4,MEMF_ANY))    {
     if(fh=Open(filename,MODE_OLDFILE)) {
         Seek(fh,0,OFFSET_END);
         CurrentPos=Seek(fh,0,OFFSET_CURRENT);
         do  {
             if(CurrentPos<MEMSIZE) {
                 ReadSize=CurrentPos+lnlp;
                 CurrentPos=0;
             } else   CurrentPos=(CurrentPos-4096)+lnlp;

             stat=Seek(fh,CurrentPos,OFFSET_BEGINNING);
             if(stat>=0) {
                 Seek(fh,0,OFFSET_CURRENT);
                 if((stat=Read(fh,buf,ReadSize))>0) {
                     buf[ReadSize-1]='\0';
                     lnlp=0;
                     for(loop=ReadSize; loop>0; loop--) {
                         if(buf[loop]=='\n') {
                             sprintf(GSTR2,"%s\r\n",buf+loop+1);
                             AEPutStr(GSTR2);
                             buf[loop]='\0';
                             lnlp=loop+1;
                             if(stat2=CheckForPause()) {
                                 AEPutStr("\r\n");
                                 loop=0;
                                 stat=(-1);
                             }
                             if(SCheckInput()) {
                                 stat2=ReadChar(1L);
                                 if(stat2<0) {
                                     loop=0;
                                     stat=(-1);
                                 } else {
                                     switch(stat2) {
                                         case '\023': /* Pause */
                                             stat=ReadChar(KEYBOARD_TIMEOUT);
                                             if(stat2<0) {
                                                 loop=0;
                                                 stat=(-1);
                                             }
                                             break;
                                         case '\003': /* ^C */
                                             AEPutStr("**Break\r\n\r\n");
                                             if(AnsiColor)  AEPutStr("[0m");
                                             loop=0;
                                             stat=(-1);
                                             break;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if(stat<0) {
                     stat=IoErr();
                     if(stat>0) {
                         //sprintf(GSTR2,"IOErr #%d\r\n",stat);
                         //AEPutStr(GSTR2);
                     }
                     stat=(-1);
                 }
             }
         } while(CurrentPos>0&&stat>=0);
         Close(fh);
     } else   AEPutStr("\r\nNot a valid node!\r\n\r\n");

     FreeMem(buf,MEMSIZE+4);
 }
}

extern struct myst my_struct;           /* temp */

void DisplayNodeCLog(void)
{
 long loop,stat,which;
 BPTR fh;
 GSTR1[0]='\0';
 AEPutStr("\r\n");

 stat=CommandSplit();
 if(stat>1) {
     sscanf(Command[1],"%ld",&which);
     sprintf(GSTR1,"%sNode%ld/Callerslog",Cmds->BBSLoc,which);
     which=0;
     if(stat>2) {
         if((Command[2][0]=='N')||(Command[2][0]=='n')) {
             if((Command[2][1]=='S')||(Command[2][1]=='s'))
                 which=1;
         }
     }
     DisplayCallerslog(GSTR1,which);
     return;
 }

     loop=0;
     do  {
         sprintf(GSTR1,"%sNode%ld/Callerslog",Cmds->BBSLoc,loop);
         if(fh=Open(GSTR1,MODE_OLDFILE)) {
             Close(fh);
             sprintf(GSTR1,"%ld - Callerslog for Node %ld\r\n",loop,loop);
             AEPutStr(GSTR1);
             loop++;
         }
     } while(fh!=NULL);

     if(loop!=1) {
         AEPutStr("\r\nWhich node to view? ");
         stat=LineInput("",GSTR1,5,KEYBOARD_TIMEOUT);
         if(stat<0||GSTR1[0]=='\0')  {
             AEPutStr("\r\n");
             return;
         }
         sscanf(GSTR1,"%ld",&which);

      sscanf(GSTR1,"%ld",&which);
      sprintf(GSTR1,"%sNode%ld/Callerslog",Cmds->BBSLoc,which);
      if(fh=Open(GSTR1,MODE_OLDFILE))
      {
        Close(fh);
        sprintf(GSTR1,"%ld - Callerslog for Node %ld\r\n",which,which);
        AEPutStr(GSTR1);
        sprintf(GSTR1,"%sNode%ld/Callerslog",Cmds->BBSLoc,which);
        DisplayCallerslog(GSTR1,0);
        return;
      }
      else AEPutStr("\r\nNot a valid node!\r\n");

   }
}

