#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fctype.h>
#include <fcntl.h>
#include <dos.h>
#include <sys/stat.h>
#include <exec/types.h>
#include <libraries/dosextens.h>

#include <dlg/dlg.h>
#include <dlg/msg.h>
#include <dlg/log.h>
#include <dlg/resman.h>
#include <dlg/user.h>
#include <dlg/menu.h>
#include <dlg/portconfig.h>

#include <link/io.h>
#include <link/util.h>

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/dlg.h>

#include <pragmas/dlg.h>

#include <private/Version.h>
#define  ObjRev "1"
const UBYTE version[]="\0$VER: WMail " BUILDVER "." ObjRev " ©" COPYRIGHT " by Digerati Dreams "__AMIGADATE__;

void  main (int, char **);
void  Usage(char *);
void _CXBRK(void);

#define STK RStruct.Command_Stack
#define HOT UserDat.Hot_Keys

BPTR               sout;
struct Ram_File    RStruct;
struct USER_DATA   UserDat;
char               Ext[5]  = "";

struct Library    *DLGBase = NULL;
struct LangStruct *ls;
char             **SA;


void main(int argc, char *argv[])
{
   int                fp;
   USHORT             pos;
   long               retval;
   long               threshold = 0;
   ULONG              size;

   struct WaitingMail WMail;
   char               ramfile [14];
   char               msgid   [12];
   char               filename[160];
   char               buffer  [255];

   char               brief  = FALSE;
   char               moreon = TRUE;

   sout = Output();

   if (!(DLGBase=OpenLibrary(DLGNAME,DLGVERSION)))  exit(0);
   if (GetDevName(Ext)==-1)                        _CXBRK();
   if (!ReadUser(&RStruct, &UserDat, Ext))          _CXBRK();
   if (!(ls=(struct LangStruct *)GetLang(Ext)))    _CXBRK();
   SA = ls->strings;

   while(--argc>0)
   {
      char  *s;

      s = *++argv;
      if (*s++=='-')
      {
         while(*s)
         {
            switch(*s++)
            {
               case 'b':
               case 'B':  brief = TRUE;
                          if (!--argc)  break;
                          threshold = atol(*++argv);
                          break;
            }
          }
      }
      else
         Usage(SA[2553]);
   }

 /* Check for existance of data file and open, otherwise, abort */

   AFPrintf(&UserDat, sout, SA[2554]);
   ASPrintf(NULL, filename, "USER:%s/WaitingMail.dat", RStruct.Name);
   UnderScore(filename);

   if ((fp=open(filename,O_RDONLY))==EOF)
   {
      AFPrintf(&UserDat, sout, SA[2555]);
      _CXBRK();
   }

   AFPrintf(NULL, sout, "\n\n");

   if (brief && threshold)
   {
      FileSize(filename, &size);
      if ((size/sizeof(WMail))<threshold)  brief = FALSE;
   }

 /* loop through and mark the messages while displaying them */

   ASPrintf(NULL, ramfile, "T:%s.tagged", Ext);
   pos    = 5;
   retval = 0;
   AFPrintf(&UserDat, sout, SA[2556]);

   while(read(fp,&WMail,sizeof(WMail))==sizeof(WMail))
   {
      /* Display the message */
      ScreenName(WMail.from);

      if (brief)
         ASPrintf(&UserDat, buffer,SA[2557],WMail.areanum,WMail.messagenum,WMail.from,WMail.subject);
      else
         ASPrintf(&UserDat, buffer,SA[2558],WMail.areaname,WMail.messagenum,WMail.from,WMail.subject);
  
      if (retval != -1)
         retval = DisplayBuffer(sout,buffer,UserDat.Screen_Width,UserDat.Screen_Len,(UserDat.More_Flag && moreon),UserDat.Ansi_Flag,&pos,0,NULL,1,"\x03");

      if (retval==-2)  moreon = FALSE;

       /* Add the message to the ram MW file */
      ASPrintf(NULL, msgid, "%04d:%05d", WMail.areanum, WMail.messagenum);
      AddStruct(ramfile, msgid, 11, 11);
   }

 close(fp);
 if (retval || brief)  AFPrintf(NULL, sout, "\n");
 AFPrintf(&UserDat, sout, SA[2559]);
 _CXBRK();
}


void Usage(char *string)

{if (string)  AFPrintf(&UserDat, sout, SA[2560], string);
 AFPrintf(&UserDat, sout, SA[2561]);
 AFPrintf(&UserDat, sout, SA[2562]);

 CloseLibrary(DLGBase);
 exit(0);
}


void _CXBRK()

{if (Ext[0])  WriteRam(&RStruct, Ext);
 CloseLibrary(DLGBase);

 exit(0);
}
