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

#include <dlg/resman.h>
#include <dlg/dlg.h>
#include <dlg/log.h>
#include <dlg/user.h>

#include <link/io.h>
#include <link/lang.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: WriteLog " BUILDVER "." ObjRev " ©" COPYRIGHT " by Digerati Dreams "__AMIGADATE__;

void             main(int, char **);
void             CleanUp(int);
void             Usage(char *);
void __regargs __chkabort(void);


BPTR              sout;
struct USER_DATA  UserDat  = {"", 100, 80, 0, 1, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
struct Library   *DLGBase = NULL;
char            **SA;


void main(int argc, char *argv[])

{char  *s;
 char  *desc=NULL;
 int    code;

 char            devname[4];
 char            Ext[4]  = "";
 struct Ram_File RStruct;


 if (!(DLGBase=OpenLibrary(DLGNAME,DLGVERSION)))  exit(5);
 if (!(SA = getlang(NULL)))                       exit(5);

 sout = Output();
 if(argc<2)  Usage("");

 AFPrintf(NULL, sout, "\n");
 while(--argc>0)
      {s = *++argv;
       if (*s++ == '-')
          {while(*s)
                {switch(*s++)
                       {case 'p':
                        case 'P':  if (!--argc) break;
                                   strncpy(Ext,*++argv,4);
                                   break;

                        case 'c':
                        case 'C':  if(!--argc) break;
                                   code=atoi(*++argv);
                                   break;

                        case 'd':
                        case 'D':  if (!--argc) break;
                                   desc=*++argv;
                                   if (strlen(desc) > 35)  desc[35]='\0';
                                   break;

                        default:   Usage("Bad Switch");
                       }
                }
          }
        else
          Usage("Bad Syntax");
      }

 if (!strlen(Ext))
    {strcpy(RStruct.Name,"System");
     strcpy(devname,"EXT");
    }
  else
    {strncpy(devname,Ext,3);
     devname[3]='\0';
     if (!ReadRam(&RStruct,Ext))  CleanUp(5);
    }

 if (code < 128 || code > 255)  Usage("Invalid LogCode");

 WriteLog(code,RStruct.Name,devname,(desc)?desc:"");
 CleanUp(0);
}


void CleanUp(int code)

{CloseLibrary(DLGBase);
 exit(code);
}


void Usage(char *string)

{int        fp;
 USHORT     pos          = 2;
 char       flippybindle = 0;
 char       line   [120];
 char       templine[80];
 struct Log log;

 AFPrintf(NULL, sout, SA[2569],string);
 AFPrintf(NULL, sout, SA[2570]);
 AFPrintf(NULL, sout, SA[2571]);

 if ((fp = open("Dlgconfig:Port/CustomLog.bbs",O_RDONLY)) != EOF)
    {AFPrintf(NULL, sout, SA[2572]);
     Draw_Line(72);

     while(read(fp, &log, sizeof(log)) == sizeof(log))
          {if (!flippybindle)
              {ASPrintf(NULL, line,SA[2573], log.number,log.desc,log.level);
               flippybindle = 1;
              }
            else
              {ASPrintf(NULL, templine,SA[2574],log.number,log.desc,log.level);
               strcat(line, templine);
               flippybindle = 0;
               if (DispBuffer(sout, line, &pos, 0, NULL, 0, "\003", &UserDat))  break;
              }
          }

     if (flippybindle)
        {DispBuffer(sout, line, &pos, 0, NULL, 0, "\003", &UserDat);
         AFPrintf(NULL, sout, "\n");
        }

     Draw_Line(72);
     close(fp);
    }

 AFPrintf(NULL, sout, SA[2575]);
 exit(5);
}


void __regargs __chkabort(void)

{;
}
