/*

   ErrorMessage (C) 1995 Hydra/TSN

   Usage:  ErrorMessage <n>  where <n> is the number of the error message
           contained in the file HBBS:Doors/System/ErrorMessage/ErrorMessage.TXT

   This program is called by the main program when it encounters an error,
   it's done like this to a) save having all the error message strings loaded in
   memory at one time b) to save code size of main program c) cos it's cool
   and d) so that you can have really long error messages when can contain
   solution descriptions.

   e.g.

   2=There's an error in you HBBS:Nodes/NodeList file or your HBBS:BBSGlobal file, you have specified more nodes in BBSNodes=x than you have parameters for NodeList_XX=X

   see what i mean ?

*/

#define ERRORMESSAGE
#define MAIN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <exec/exec.h>
#include <clib/exec_protos.h>
#include <libraries/reqtools.h>
#include <clib/reqtools_protos.h>

#include <intuition/intuition.h>
#include <clib/intuition_protos.h>

#include <ctype.h>

#include <HBBS/Defines.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas.h>

#include "//common/files.h"

struct Library *HBBSCommonBase=NULL;
struct ReqToolsBase *ReqToolsBase; // define globallaly or stack overflow away! :-)

long __stack=10000;
  struct Screen *scr=NULL;
  struct Process *myproc=NULL;
  struct Window *oldwinptr=NULL;


static VOID cleanup(ULONG num)
{
  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }

  if (num) printf("Door Error = %d\n",num);

  if (ReqToolsBase)
    CloseLibrary ((struct Library *)ReqToolsBase);

  exit(0);
}

static VOID init(VOID)
{
  if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  {
    cleanup(1);
  }

  if (!(HBBS_InitCommon()))
  {
    cleanup(2);
  }

  if (ReqToolsBase = (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))
  {

    // lock the ctrlscrn, if that's not there then get the wb screen. if thats not there
    // then we'll just go argh!

    if ((scr=LockPubScreen("CtrlScrn")) || (scr=LockPubScreen(NULL))) // get screen
    {
      if (scr->FirstWindow) // get
      {
        if (myproc=(struct Process *)FindTask(NULL))
        {
          oldwinptr = myproc->pr_WindowPtr;
          myproc->pr_WindowPtr=scr->FirstWindow;
        }
      }

    }
  }

}

void main(int argc, char *argv[])
{
  char *cantfind="An error has occured but I can't find the message text!";

  FILE *errfile;
  char errline[2048];
  char errormsg[2048];
  char item[20];
  BOOL found=FALSE;

  BOOL setcr=FALSE;
  int loop;

  if (argc==1)
  {
    puts("ErrorMessage V1.0 (C) 1995 Hydra/LSD, This program is part of HBBS!");
    puts("Usage: ErrorMessage <NUMBER> <NODE> <STR>");
  }
  else
  {
    init();
    // if we have reqtools.library then set the requester default window to the first window on ctrlscrn!

    if (errfile=fopen(FILE_ERRORMESSAGE,"r"))
    {
      while (!feof(errfile) && !found)
      {
        fgets(errline,1000,errfile);
        if (!feof(errfile))
        {
          stripcr(errline);
          GetItem(item,errline);
          if (stricmp(item,argv[1])==0)
          {
            found=TRUE;
          }
        }
      }
      fclose(errfile);
    }

    if (found)
    {
      GetParams(errormsg,errline);
      if (argc>=3) replace(errormsg,errormsg,"@N@",argv[2]);
      if (argc==4) replace(errormsg,errormsg,"@E@",argv[3]);

      for (loop=0;loop<strlen(errormsg);loop++)
      {
        if ((loop % 70)==69) // insert a cariage return every 70 chars if possible
        {
          setcr=TRUE;
        }
        if (setcr && errormsg[loop]==' ')
        {
          setcr=FALSE;
          errormsg[loop]='\n';
        }
      }
    }
    else
    {
      strcpy(errormsg,cantfind);
    }

    if (ReqToolsBase)
    {
      if (myproc) ScreenToFront(scr);
      rtEZRequest(errormsg,"OK!",NULL,NULL,NULL);
    }
    else
      puts(errormsg);

    if (oldwinptr)
      myproc->pr_WindowPtr=oldwinptr;
    if (scr)
      UnlockPubScreen(NULL,scr);
    cleanup(0);
  }
}
