/*

  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>


#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/Access.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: CmdPrompt "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];

long __stack=1024*16;

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[BIG_STR],doorname[BIG_STR],options[BIG_STR],prompt[BIG_STR];
  short tmpval;
  V_BIGNUM intype;
  BOOL skipmenu=FALSE,FirstRun=TRUE;
  do
  {
    doorname[0]=0;


    if ((N_ND->User->CallData->UserType==USERTYPE_NORMAL) && (skipmenu==FALSE))
    {
      if (FirstRun) // don't pause the first time..
      {
        FirstRun = FALSE;
      }
      else
      {
        DOOR_PausePrompt(NULL);
      }
      DOOR_DisplaySpecialScreen("MENU");
    }

    skipmenu=FALSE;
    // create the prompt string


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

    if ((HBBS_TimeLeft()<0) && (N_ND->User->Acs.Data[ACS_UNLIMTIME]=='N'))
    {
      if (!DOOR_DisplaySpecialScreen("NOTIME"))
      {
        DOOR_WriteText(N_ND->BBSStrings->OutOfTime);
        DOOR_WriteText("\r\n");
      }
      DOOR_Goodbye();
      
      HBBS_AddToCallersLog("User ran out of time!");
      
    }

    if (N_ND->OnlineStatus==OS_ONLINE)
    {
      strcpy(prompt,ANSI_RESET);
      strcat(prompt,N_ND->CurrentConf->MenuPrompt);

      HBBS_ModifyString(prompt);

      // now display it..

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

      RemoveSpaces(command);

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

      if (N_ND->OnlineStatus==OS_ONLINE && command[0] && intype!=IN_TIMEOUT)
      {
        // 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 if expertmode is off..)
        }
        else
        {  
          
          /*
           * don't run doors if they end with a !, e.g. "Quest!"
           */
          
          if ( (doorname[strlen(doorname)-1] == '!') || 
               (!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);
}
