/*

  SelectLanguage
  ==============

    The main purpose of this door is to get the user to pick a language/screen type

*/

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

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

int    gargc;
char   **gargv;


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

void DoorMain( void )
{
  LONG loop;
  struct Node *node;
  BOOL Done=FALSE;
  // set to default if before a possible error occures

  N_ND->User.CallData.Language=1;
  N_ND->User.NormalData.Language=1;

  DOOR_WriteText(ANSI_RESET);

  for (loop=1,node = BBSGlobal->LanguageName->lh_Head ; node->ln_Succ ; node=node->ln_Succ,loop++)
  {
    sprintf(outstr,ANSI_FG_WHITE"%d) "ANSI_FG_CYAN"%s\r\n"ANSI_RESET,loop,node->ln_Name);
    DOOR_WriteText(outstr);
  }

  do
  {
    DOOR_WriteText(ANSI_FG_YELLOW "Select A Language/Screen Type "ANSI_FG_BLUE": "ANSI_FG_WHITE);

    strcpy(N_ND->CharsAllowed,"0123456789");
                                                     // more than 99 langauges ?  I don't think so.. :-)
    DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_USECHARS|GL_IMMEDIATE,'\0',BBSGlobal->Languages > 9 ? 2 : 1,0,NULL);

    if (N_ND->OnlineStatus==OS_ONLINE)
    {
      if (sscanf(N_ND->CurrentLine,"%ld",&loop))
      {
        if (loop>=1 && loop<=BBSGlobal->Languages)
        {
          N_ND->User.CallData.Language=loop;
          N_ND->User.NormalData.Language=loop;
          Done=TRUE;
        }
      }
    }
    else
    {
      Done=TRUE;
    }
  } while (!Done);
}

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("<doorname>");

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