/*

   ErrorMessage (C) 1995 Hydra/dAT

   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 ?


   todo
   ====

   update so that it will find hbbs's screen if hbbs is not using either workbench or it's own
   custom screen (i.e. any other public screen!)
*/

#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 <HBBS/release.h>
char *versionstr="$VER: ErrorMessage "RELEASE_STR;

#include "//common/files.h"

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

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))
  {

    // if we have reqtools.library then set the requester default window to the first window on ctrlscrn!

    // 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))
        {
          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 errstr[1024];
  char item[20];
  BOOL found=FALSE;

  BOOL setcr=FALSE;
  int loop;

  oldwinptr = myproc->pr_WindowPtr; // save this

  if (argc==1)
  {
    puts("ErrorMessage V"RELEASE_STR" "AUTHOR_SCENE", This program is part of HBBS!");
    puts("Usage: ErrorMessage <NUMBER> <NODE> [<STR..>]");
  }
  else
  {
    init();

    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)
    {
      for (loop=3;loop<argc;loop++)
      {
        if (loop!=3)
        {
          strcat(errstr," ");
          strcat(errstr,argv[loop]);
        }
        else
        {
          strcpy(errstr,argv[loop]);
        }
      }
      GetParams(errormsg,errline);
      if (argc>=3) replace(errormsg,errormsg,"@N@",argv[2]);
      if (argc>=4) replace(errormsg,errormsg,"@E@",errstr);

      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);
  }
}
