#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: Who "RELEASE_STR;

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 DoorMain(int argc,char *argv[])
{
  char outstr[100],tmpstr[50];
  V_BIGNUM nodenum;
  struct NodeData *N_Dat=NULL;

//  for(loop=2;loop<argc;loop++) // if there are any parameters then display them
//  {
//    sprintf(outstr,"Parameter %d = %s\n\r",loop-1,argv[loop]);
//    DOOR_WriteText(outstr);
//  }

  DOOR_WriteText(ANSI_FG_YELLOW ANSI_BG_BLACK ANSI_UNDERLINE
                 "                                                                               \n\r");
  DOOR_WriteText(ANSI_BG_BLUE
                 "                             HydraWHO - "AUTHOR_SCENE"                              " ANSI_RESET "\n\r\r\n");
  DOOR_WriteText(ANSI_FG_PURPLE ANSI_UNDERLINE "  NODE   HANDLE                 GROUP                   ACTION                 \n\r\r\n" ANSI_RESET ANSI_FG_GREEN);
  for (nodenum=0;nodenum<BBSGlobal->BBSNodes;nodenum++)
  {
    // get a pointer to another node..

    if (N_Dat=HBBS_NodeDataPtr(nodenum))
    {
      switch(N_Dat->Status)
      {
        case STAT_ONLINE:
          if (N_Dat->User->Valid)
          {
            sprintf(outstr,"  %4d   ",nodenum);
            sprintf(tmpstr,"%-20s   ",N_Dat->User->CallData->Handle);
            strncat(outstr,tmpstr,23); // 20+3 (Handle + spaces)
            sprintf(tmpstr,"%-21s   ",N_Dat->User->CallData->Group);
            strncat(outstr,tmpstr,24);
            sprintf(tmpstr,"%-20s  ",N_Dat->Action);
            strncat(outstr,tmpstr,22);
          }
          else
          {
            sprintf(outstr,"  %4d                                                  No User              ",nodenum);
          }
          break;
        case STAT_CLOSED:
          sprintf(outstr,"  %4d                                                  InActive             ",nodenum);
          break;
        case STAT_READY:
          sprintf(outstr,"  %4d                                                  Awaiting Connect!    ",nodenum);
          break;
      }
      DOOR_WriteText(outstr);
      DOOR_WriteText("\r\n\r\n");
    }
  }
  DOOR_WriteText(ANSI_RESET ANSI_FG_PURPLE ANSI_UNDERLINE "                                                                               \r\n\r\n" ANSI_RESET);
}

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

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