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

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

struct BBSGlobalData *BBSGlobal=NULL;
struct NodeData *N_ND=NULL;
int N_NodeNum=-1;
char outstr[1024]; // temp string for displaying text..

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 CoolText(char *str)
{
  int loop,loop2;
  int len,clipstart;
  char tmpstr[BIG_STR],cmovestr[10];

  len=strlen(str);
  clipstart=len / 2;

  for (loop=1;loop<=len;loop++)
  {
    for (loop2=0;loop2<clipstart;loop2++) tmpstr[loop2]=' ';
    tmpstr[loop2]=0;
    strNcpy(tmpstr+strlen(tmpstr),str+clipstart,loop);
    DOOR_WriteText(tmpstr);


    if (loop<len)
    {
      sprintf(cmovestr,"\033[%dD",strlen(tmpstr));
      DOOR_WriteText(cmovestr);
    }
    if (loop % 2) clipstart --;
  }
}

#define SCROLL_FROMRIGHT 1
#define SCROLL_FROMLEFT 2

void ScrollAnsi(int ScrollDirection,char *str)
{
  int loop;
  int len;
  char tmpstr[BIG_STR],clipstr[BIG_STR];

  len=strlen(str);

  if (ScrollDirection == SCROLL_FROMRIGHT)
  {
    for (loop=1;loop<=len;loop++)
    {
      strNcpy(clipstr,str,loop);
      sprintf(tmpstr,"\033[%dD%s",loop,clipstr);
      DOOR_WriteText(tmpstr);
    }
  }

  if (ScrollDirection == SCROLL_FROMLEFT)
  {
    for (loop=1;loop<=len;loop++)
    {
      strNcpy(clipstr,str+len-loop,loop);
      DOOR_WriteText(clipstr);

      if (loop<len)
      {
        sprintf(tmpstr,"\033[%dD",loop);
        DOOR_WriteText(tmpstr);
      }
    }
  }
}

void DoorMain(int argc,char *argv[])
{
  int loop;

  DOOR_WriteText("                : ");
  Delay(50);
  CoolText("This is a really really long test string!");
  Delay(50);
  DOOR_WriteText("\r\n");

  DOOR_WriteText("\r\n                                                                              ");
  ScrollAnsi(SCROLL_FROMRIGHT,"This is a test to scroll some text from the right      !");

  DOOR_WriteText("\r\n");

  ScrollAnsi(SCROLL_FROMLEFT,"    This is a test to scroll some text from the left!");

  DOOR_WriteText("\r\n");

}

int main(int argc,char *argv[])
{

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

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