/*

  function of cmdprompt door

  - run user doors.

*/
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_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;
char outstr[1024];

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 )
{
  char command[80],doorname[80],options[80],prompt[80];
  short tmpval;
  BOOL skipmenu=FALSE;
  do
  {
    doorname[0]=0;


    if ((N_ND->User.CallData.UserType==USERTYPE_NORMAL) && (skipmenu==FALSE))
    {
      DOOR_PausePrompt(NULL);
      DOOR_DisplaySpecialScreen("MENU");
    }

    skipmenu=FALSE;
    // create the prompt string


    if (N_ND->NodeFlags & NFLG_OLMSWAITING)
    {
      DOOR_SystemDoor("ReadOLM",NULL);
    }


    strcpy(prompt,ANSI_RESET);
    strcat(prompt,N_ND->CurrentConf->MenuPrompt);

    HBBS_ModifyString(prompt);

    // now display it..

    DOOR_WriteText(prompt);
    DOOR_WriteText(" ");
    DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',0,0,NULL);
    strNcpy(command,N_ND->CurrentLine,79);

    RemoveSpaces(command);

    // *C* strip spaces from end of "command" and from the start

    if (N_ND->OnlineStatus==OS_ONLINE && command[0])
    {
      // ok, everything before the first space is the doorname, anything after
      // it are the options.

      tmpval=iposition(" ",command);

      if (tmpval<0) tmpval=strlen(command);

      strNcpy(doorname,command,tmpval);
      strfcpy(options,command,tmpval+1);

      if (strcmp(doorname,"?")==0)
      {
        DOOR_DisplaySpecialScreen("MENU");
        skipmenu=TRUE; // so we don't display it twice..)
      }
      else
      {
        if (!DOOR_UserDoor(doorname,options))
        {
          DOOR_WriteText("\n\rUnknown Command! - Type ? For Help!\n\r\n\r");
        }
      }
    }

    if ((CarrierLost()) && (N_ND->LoginType==LOGIN_REMOTE)) N_ND->OnlineStatus = OS_OFFLINE;

  } while (N_ND->OnlineStatus == OS_ONLINE); // && stricmp("G",doorname));
}

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

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