// skeleton code for writing a new door

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


#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>
#include <HBBS/hbbscommon_pragmas.h>
#include <HBBS/Hbbsnode_protos.h>
#include <HBBS/Hbbsnode_pragmas.h>

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

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

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

void HBBS_AddCfgList(struct CfgFileData *cfgfile,struct List *list,char *itemname)
{
  struct Node *node;
  int count=1;
  char tmpstr[BIG_STR];
  sprintf(tmpstr,"%s_#?",itemname);

  HBBS_RemoveCfgItem(cfgfile,tmpstr);

  for (node=list->lh_Head;node->ln_Succ;node=node->ln_Succ)
  {
    sprintf(tmpstr,"%s_%d",itemname,count++);
    HBBS_AddCfgItem(cfgfile,tmpstr,node->ln_Name);
  }
}

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;
  char filename[BIG_STR];
  char tmpstr[BIG_STR];
  V_SMALLNUM loop;

  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_WriteText(ANSI_CLS ANSI_RESET ANSI_FG_BLUE"+-----------------------------------------------------------------------------+\r\n"
                                                 "|"ANSI_BG_BLUE ANSI_FG_WHITE" = DoM-WALL = V1.0 = By Hydra / L.S.D.              (Utopia! 01202 434121) = "ANSI_FG_BLUE ANSI_BG_BLACK"|\r\n"
                                                 "+-----------------------------------------------------------------------------+\r\n");

  if (WallCFG=HBBS_LoadConfig(filename))
  {
    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)
    {


      // output all lines of ext from the list..

      for (node=WallList->lh_Head;node->ln_Succ;node=node->ln_Succ)
      {
        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,'\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))
          {
            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);
            NewStrNode(N_ND->CurrentLine,WallList);
            WallItems++;
            while (WallItems>MaxItems)
            {
              node=RemHead(WallList);
              FreeStr(node->ln_Name);
              FreeVec(node);
              WallItems--;
            }
          }
        }

        HBBS_AddCfgList(WallCFG,WallList,"WallItems");
        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);
}
