
#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>


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

#include <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/Hbbscommon_protos.h>
#ifdef __SASC
#include <HBBS/hbbscommon_pragmas_sas.h>
#else
#include <HBBS/hbbscommon_pragmas_stc.h>
#endif

#include <HBBS/Hbbsnode_protos.h>
#ifdef __SASC
#include <HBBS/Hbbsnode_pragmas_sas.h>
#else
#include <HBBS/Hbbsnode_pragmas_stc.h>
#endif
#include <HBBS/release.h>
char *versionstr="$VER: Wall "RELEASE_STR;

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

struct BBSGlobalData *BBSGlobal=NULL;
struct NodeData *N_ND=NULL;
int N_NodeNum=-1;

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

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


/* *I* nick this function for HBBSCommon.library
char *GetParentDir(char *str)
{
  int loop;
         
  if (position("/",str)>=0)
  {
    for (loop=strlen(str)-1;str[loop]!='/';loop--) str[loop]=0;
  }
  return(str);
}
*/

struct List *CreateList(void )
{
  struct List *newlist=NULL;

  if (newlist=AllocVec(sizeof(struct List),MEMF_PUBLIC))
  {
    NewList(newlist);
  }
  return(newlist);
}
void DoorMain(int argc,char *argv[])
{
  struct CfgFileData *WallCFG;
  struct List *WallList=NULL;
  struct Node *node;
  ULONG WallItems=0,MaxItems=60;//,displayed=0;
  char filename[BIG_STR];
  char tmpstr[BIG_STR];
  V_SMALLNUM loop;

  strcpy(filename,"Progdir:Wall.CFG");
//  sprintf(filename,"%sWall.CFG",GetParentDir(argv[0]));

//  DOOR_WriteText("[2J[H[36m-=[ [37;44m [0;3;37;44mHydra Wall V1.0[0;37;44m [36;40m ]=-[35m =============== [36m-=[ [37;44m [0;3;37;44m(C) 1995 Deluxe Software Ltd[0;37;44m [36;40m ]=-\r\n");

  DOOR_DisplaySpecialScreen("Wall");
  DOOR_WriteText(ANSI_RESET ANSI_FG_BLUE
                 "+ --------------------------------------------------------------------------- +\n\r");

  if (WallCFG=HBBS_LoadConfig(filename,LCFG_NONE))
  {
    if ((WallItems=HBBS_GetSetting(WallCFG,(void *)&WallList,VTYPE_STRINGLIST,"WallItems",OPT_MULTI))==0)
    {
      WallList=CreateList();
    }
    HBBS_GetSetting(WallCFG,(void *)&MaxItems,VTYPE_BIGNUM,"MaxItems",OPT_SINGLE);
  }
  else
  {
    if (WallCFG=HBBS_CreateConfig(filename))
    {
      if (WallList=CreateList())
      {
        DOOR_WriteText("New Config Created!\n\r");
        HBBS_AddCfgItem(WallCFG,"MaxItems","60"); // default.
      }
      else
      {
        DOOR_WriteText("Could not create config!\n\r");
        HBBS_FlushConfig(WallCFG);
      }
    }

  }
  if (WallCFG)
  {
    if (WallList)
    {
      
/*               
      for (node = WallList->lh_Head ; displayed++ <= 15 && node->ln_Succ ; node = node->ln_Succ)
      {
        sprintf(tmpstr,ANSI_FG_CYAN " %s\n\r",node->ln_Name);
        DOOR_WriteText(tmpstr);
      }
*/      
      
      for (loop=15 ; loop >= 1 ; loop -- )
      {                    
        if (loop <= WallItems)
        {                       
          node=GetNode(WallList,loop-1);
          sprintf(tmpstr,ANSI_FG_CYAN " %s\n\r",node->ln_Name);
          DOOR_WriteText(tmpstr);
        }
      }

      DOOR_WriteText(ANSI_RESET ANSI_FG_BLUE
                     "+ --------------------------------------------------------------------------- +\n\r");

      if (DOOR_ContinuePrompt(ANSI_RESET " Wanna Write On Da Wall ",DEFAULT_NO|DCP_ADDYN))
      {

        DOOR_WriteText(ANSI_RESET "Enter Some Text Below!\r\n"
                                  ANSI_FG_CYAN"("ANSI_FG_WHITE"|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|-"ANSI_FG_CYAN")\n\r"ANSI_FG_WHITE
                                  " ");

        DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',76-strlen(N_ND->User->CallData->Handle),0,NULL);
        if (N_ND->OnlineStatus==OS_ONLINE)
        {
          StripSpaces(N_ND->CurrentLine);
          if ((N_ND->CurrentLine[0]) && (strlen(N_ND->CurrentLine)>3))
          {
            // add the line of text to the Wall.History file..

            strcpy(filename,"Progdir:Wall.History");
            HBBS_GetDate(tmpstr);
            strcat(tmpstr," ");
            HBBS_GetTime(tmpstr+strlen(tmpstr));
            strcat(tmpstr," Handle: ");
            strcat(tmpstr,N_ND->User->CallData->Handle);
            strcat(tmpstr," Text: ");
            strcat(tmpstr,N_ND->CurrentLine);
            strcat(tmpstr,"\n");
            HBBS_AppendStrToFile(filename,tmpstr);

            // then flaff around with it and add it to the wall and the datafile..

            for (loop=strlen(N_ND->CurrentLine);loop<77-strlen(N_ND->User->CallData->Handle);loop++)
            {
              N_ND->CurrentLine[loop]=' ';
            }
            N_ND->CurrentLine[loop]=0;
            strcat(N_ND->CurrentLine,N_ND->User->CallData->Handle);
            
            if(node = HBBS_CreateNode(N_ND->CurrentLine,0))
            {
              AddHead(WallList,node);
              WallItems++;
              
              while (WallItems>MaxItems)
              {
                node=WallList->lh_TailPred;
                Remove(node);
                FreeStr(node->ln_Name);
                FreeVec(node);
                WallItems--;
              }
              if (!HBBS_AddCfgList(WallCFG,WallList,"WallItems")) 
              {
                DOOR_WriteText("Error adding wall items\r\n");
              }
              else
              {
                if (!HBBS_SaveConfig(WallCFG)) 
                  DOOR_WriteText("Unable To Save Config!\n\r");
              }
            }
          }
        }

      }
      FreeStrList(WallList);
    }
    HBBS_FlushConfig(WallCFG);
  }
  else DOOR_WriteText("Error Loading/Creating Config!");  // *C* add to node log..


}

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

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