#include "Includes.h"
#include "Defines.h"
#include "vars.h"
#include "Version.h"
#include "protos.h"

#include "/ross/protos.h"

#include <link/io.h>

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

long __stack=50000L;
char *_procname="DLGMail Resident";
long __priority=-11;
long __BackGroundIO=0;


#define XORVAL 0x00195300   /* for decoding the illegit string */
#define SHIFTBITS 12        /* for decoding the illegit string */

int IT_IS_ZMH=0;
int TRAPDOOR_LOADED=1;

struct IntuitionBase    *IntuitionBase=NULL;
struct Library    *DLGBase;
struct Screen     *DLGMailScreen=NULL;
struct Window     *DLGMailWindow=NULL;

LONG i_am_bundling=0;
LONG i_am_processing=0;

int MyDEBUG=0;

int OS_VERSION=0;

BPTR NULLFH=NULL;
BPTR sout;

char ILLEGITSTRING[50];

char logstr[1000];

#define CRC1OK 0xd158f14e
#define CRC2OK 0x637cf337

/// Main
int main(int argc, char **argv)
{
   int lock;
   int rc=0;
   int i;
   struct Process *my_process;
   struct Task    *my_task;
   BPTR GCFGFH=NULL;
   int OldTaskPri;
   char buf[100];
   ULONG ILLEGITNUMS[]={0x001ce300,0x001db300,0x001fa300,0x001e1300,0x001b5300,0x001d9300,0x001f0300,0x001f2300,0x001fc300,0x001e1300,0x001c8300,0x00195300,0x00122300,0x00198300,0x00101300 };

   onbreak(brkfunc);

   sout = Output();

   DLGBase = OpenLibrary("dlg.library",4L);

   if(!DLGBase)
   {
      DisplayErrReq(NULL,"DLG.library version 4+ not found!","DLG installation not properly done!");
      exit(10);
   }

   if(FindPort(DLGMAILREXXPORT))
   {
      DisplayErrReq(NULL,"DLGMail already running. Can't run again.","Use 'DMC QUIT' to kill other DLGMail program.");
      exit(10);
   }

   NULLFH = Open("NULL:DLGMail.Out",MODE_NEWFILE);

   if(!NULLFH)
   {
      DisplayErrReq(NULL,"Could not open NULL:.","Is NULL: mounted? Reboot and try again...");
      return(0);
   }

   DoAScript("DLGMail_Startup.DMB",NULLFH);

   getcd(0,buf);
   chdir("INBOUND:");
   getcd(0,buf);

   strcpy(ILLEGITSTRING," ");

   for(i=0;;i++)
   {
      int c;
      char str[10];

      c=(ILLEGITNUMS[i]^XORVAL)>>SHIFTBITS;
      if(c==0) break;
      ASPrintf(NULL,str,"%c",c);
      strcat(ILLEGITSTRING,str);
   }

/* initialization goodies */

   my_task=FindTask(NULL);
   my_process=(struct Process *)my_task;

   SetEnv("DLGMail","DLGMail Installing");

   IntuitionBase=(void *)OpenLibrary("intuition.library",NULL);

   if(IntuitionBase)
   {
      lock=LockIBase(0);
      OS_VERSION=IntuitionBase->LibNode.lib_Version;
      UnlockIBase(lock);

      i=LoadConfig();

      if(i==LS_NO_ERROR)
      {
         if(MyDEBUG==0) MyDEBUG=_DLGMAIL_DB;

         Log("*-- %s",ID_STRING);

         _AREACRC=CalcAreasCRC(_ROSSAREAS);
         Log(". Resident area structures verified");

         OldTaskPri=SetTaskPri(my_task,_TASKPRI);
         if(OldTaskPri>127) OldTaskPri=OldTaskPri-256;

         ASPrintf(NULL,buf,"%d",GCFG);
         SetEnv("GCFG",buf);
         GCFGFH=Lock("ENV:GCFG",ACCESS_READ);

         if(GCFGFH)
         {
            i=0;

            if(_STRIPPOINTNET) i=OK2Strip(_POINTNET);

            if(i==0)
            {
               if(ConOpen())
               {
                  StatCommandLine("Initializing ARexx Port");

                  SetEnv("DLGMail","DLGMail Installed");

                  /* actual work done here */

                  rc=ARexxHeart();

                  /* ********************* */

                  SetEnv("DLGMail","DLGMail Exiting");

                  ConClose();
               }
               else
               {
                  DisplayErrReq(NULL,"Error opening status window/console.","Reboot your machine.");
               }
            }
            else
            {
               DisplayErrReq(NULL,"Your POINTNET address duplicates your main/an AKA address","See manual for details.");
            }

            UnLock(GCFGFH);
            DeleteFile("ENV:GCFG");
         }
         else
         {
            DisplayErrReq(NULL,"Couldn't lock ENV:GCFG.","Reboot your machine.");
         }

         Log("*-- DLGMail Terminating\n");

         __UnLoadARE();

      }
      else
      {
         DisplayErrReq(NULL,"DLGMail configuration error. Hints may be logged to the","LOGS:DLGMailCfgErr.RPT file.");
      }

      if(GCFG) FreeMem((void *)GCFG_block,GCFG_size);

      CloseLibrary((void *)IntuitionBase);
   }

   DeleteFile("ENV:DLGMail");
   SetTaskPri(my_task,OldTaskPri);
   DoAScript("DLGMail_ShutDown.DMB",NULLFH);

   if(NULLFH) Close(NULLFH);
   if(DLGBase) CloseLibrary(DLGBase);

   exit(0);
}
//-

/// DisplayErrReq
/* ERROR REQUESTER */
int DisplayErrReq(struct Window *window, char *errtext, char *solutiontext)
{
   BOOL intuitionpresent=TRUE;
   struct IntuiText topline,prompt,solution,notok;

   if(!IntuitionBase)
   {
      IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
      if(IntuitionBase)
         intuitionpresent=FALSE;
   }

   if(!IntuitionBase)
      return 0;

   topline.NextText=&prompt;
   prompt.NextText=&solution;
   solution.NextText=NULL;
   notok.NextText=NULL;

   CreateMes(&topline,3,15,"FATAL DLGMail PROBLEM DETECTED:");
   CreateMes(&prompt,3,35,errtext);
   CreateMes(&solution,3,55,solutiontext);

   CreateMes(&notok,3,3,"Sigh");
   AutoRequest(window,&topline,NULL,&notok,NULL,NULL,640,100);

   if(intuitionpresent==FALSE) CloseLibrary((void *)IntuitionBase);

   return 0;
}
//-

/// CreateMes
int CreateMes(struct IntuiText *x, int left,int top,char *mesg)
{
   x->FrontPen=AUTOFRONTPEN;
   x->BackPen=AUTOBACKPEN;
   x->DrawMode=JAM1;
   x->LeftEdge=(SHORT)left;
   x->TopEdge=(SHORT)top;
   x->ITextFont=NULL;
   x->IText=mesg;

   return 0;
}
//-

/// brkfunc
int brkfunc(void)
{
   Log(". Break signal detected");
   return(0);
}
//-
