
/* **** Includes *********************************************************** */

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>

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

/* **** Variables ********************************************************** */


struct Library *HBBSCommonBase  = NULL;
struct Library *HBBSNodeBase    = NULL;

struct BBSGlobalData *BBSGlobal = NULL;
struct NodeData *N_ND           = NULL;
int    N_NodeNum                = -1;
char   outstr[1024];

long __stack=16*1024; // increse this in 4k incrments if you suffer from
                      // random/suprious crashings after or during the running
                      // of your door.

int    gargc;         // these are just copies of main()'s argc and argv..
char   **gargv;

/* **** Functions ********************************************************** */


#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif

static VOID cleanup(ULONG num)
{
  if (HBBSNodeBase)
  {
    HBBS_CleanUpDoor();
    CloseLibrary (HBBSNodeBase);
  }

  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }

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

  exit(0);
}

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

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

  if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  {
    cleanup(3);
  }

  if (!(HBBS_InitDoor(N_NodeNum,name)))
  {
    cleanup(4);
  }
  SetProgramName(name);
}

/* **** DoorMain *********************************************************** */

void DoorMain( void )
{
  V_BIGNUM LineNum=0,loop;
  BOOL Done=FALSE;
  BOOL Save=FALSE;
  struct List *TextList;
  char linewrap[BIG_STR]="";
  struct Node *node;

  // if load specified then load filename specified in paramstr[2]
  if (!((stricmp(gargv[3],"LOAD")==0) && (TextList=HBBS_LoadFile(gargv[2]))))
  {
    TextList=HBBS_CreateList();
  }

  LineNum=HBBS_NodesInList(TextList);

  if (TextList)
  {
    DOOR_WriteText(ANSI_RESET ANSI_FG_CYAN"-- "ANSI_FG_YELLOW"Enter Lines Of Text Below, blank line to bring up menu"ANSI_FG_CYAN" --------------\r\n");

    do
    {
      sprintf(outstr,ANSI_RESET ANSI_FG_CYAN"%02d"ANSI_FG_BLUE") "ANSI_FG_WHITE,LineNum+1);
      DOOR_WriteText(outstr);

      DOOR_GetLine(GL_HISTORY|GL_EDIT|GL_DISPLAY|GL_LINEWRAP|GL_IMMEDIATE,'\0',75,0,linewrap);
      strcpy(linewrap,N_ND->CurrentLineWrap);
      if (N_ND->OnlineStatus==OS_ONLINE)
      {
        if (N_ND->CurrentLine[0]==0)
        {
          // pop up a menu HERE.
          strcpy(N_ND->CharsAllowed,"WwDdSsLlEeCcQq");
          DOOR_MenuPrompt("[S]ave, [D]elete, [L]ist, [E]dit, [C]ontinue, [Q]uit :",'C');
          DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_USECHARS|GL_IMMEDIATE,'\0',1,0,NULL);
          if (N_ND->OnlineStatus==OS_ONLINE)
          {
            if (N_ND->CurrentLine[0])
            {
              switch (toupper(N_ND->CurrentLine[0]))
              {
                case 'W': // *C* make user aware of this key!
                  DOOR_WriteText(ANSI_CLS);
                  break;
                case 'S':
                  Done=TRUE;
                  Save=TRUE;
                  break;
                case 'Q':
                  if (DOOR_ContinuePrompt("Are you sure you want to abort this text ? ",DEFAULT_NO|DCP_ADDYN))
                  {
                    Done=TRUE;
                    DOOR_Return("CANCEL");
                  }
                  break;        
                case 'C':
                  break;
                case 'D':
                  DOOR_MenuPrompt("Enter Line Number :",' ');
                  strcpy(N_ND->CharsAllowed,"0123456789");
                  DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_USECHARS,'\0',3,0,NULL);
                  if (N_ND->OnlineStatus==OS_ONLINE)
                  {
                    if (sscanf(N_ND->CurrentLine,"%d",&loop))
                    {
                      if (loop>0 && loop<=LineNum)
                      {
                        node=GetNode(TextList,loop-1);
                        Remove(node);
                        FreeStr(node->ln_Name);
                        FreeVec(node);
                        LineNum--;

                      }
                    }
                  }
                  break;
                case 'E':
                  DOOR_MenuPrompt("Enter Line Number :",' ');
                  strcpy(N_ND->CharsAllowed,"0123456789");
                  DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_USECHARS,'\0',3,0,NULL);
                  if (N_ND->OnlineStatus==OS_ONLINE)
                  {
                    if (sscanf(N_ND->CurrentLine,"%d",&loop))
                    {
                      if (loop>0 && loop<=LineNum)
                      {
                        node=GetNode(TextList,loop-1);
                        sprintf(outstr,ANSI_RESET ANSI_FG_CYAN"%02d"ANSI_FG_BLUE") "ANSI_FG_WHITE,loop);
                        DOOR_WriteText(outstr);
                        DOOR_GetLine(GL_HISTORY|GL_EDIT|GL_DISPLAY,'\0',75,0,node->ln_Name);
                        if (N_ND->OnlineStatus==OS_ONLINE)
                        {
                          if (N_ND->CurrentLine[0])
                          {
                            if (!FreeAndSet(&node->ln_Name,N_ND->CurrentLine))
                            {
                              Remove(node);
                              FreeVec(node);
                              LineNum--;
                            }
                          }
                        }
                      }
                    }
                  }
                  break;
                case 'L':
                  for (loop=0,node=TextList->lh_Head;node->ln_Succ;node=node->ln_Succ,loop++)
                  {
                    sprintf(outstr,ANSI_RESET ANSI_FG_CYAN"%02d"ANSI_FG_BLUE") "ANSI_FG_WHITE"%s\r\n",loop+1,node->ln_Name);
                    DOOR_WriteText(outstr);
                  }
                  break;
                default:
                  DOOR_WriteText(ANSI_FG_GREEN "Unknown Option!\r\n");
                  break;
              }
            }
          } else Done=TRUE; //lost carrier!
        }
        else
        {
          LineNum++;
          NewStrNode(N_ND->CurrentLine,TextList);
        }
      }
    } while (!Done && N_ND->OnlineStatus==OS_ONLINE);
    if (Save)
    {
      if (LineNum)
      {
        HBBS_SaveFile(gargv[2],TextList);
      }
      else
      {
        DOOR_WriteText(ANSI_FG_GREEN"Message Containes No Text, Cancelled!\r\n");
        DOOR_Return("CANCEL");
      }
    }
  }
}

int main(int argc,char **argv)
{
  gargc=argc;
  gargv=argv;

  if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  {
    printf("Invalid/No Paramaters for door!\n");
    exit (20);
  }
  init("Editor");

  if (BBSGlobal=HBBS_GimmeBBS())
  {
    if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
    {
      DoorMain();
    }
  }
  cleanup(0);
}

/* **** End Of File ******************************************************** */
