/*
  Bulletin Viewer
  ===============

    Displayed bulletins

  Version
  =======

    Source code last changed for release V1.01

  Docs
  ====

    Not written yet

  Notes
  =====

    Source code based on ExampleDoor code from HydraBBS V1.0
*/

/* **** Includes *********************************************************** */

#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 <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/Access.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>

#include <HBBS/Hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas_sas.h>

#include <HBBS/Hbbsnode_protos.h>
#include <HBBS/Hbbsnode_pragmas_sas.h>

#include <HBBS/release.h>
char *versionstr="$VER: DoorName "RELEASE_STR;

/* **** Variables ********************************************************** */


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=16*1024; // increse this in 4k incrments if you suffer from
                      // random/suprious crashings after or during the running
                      // of your door. (16k default)

int    gargc;         // these are just copies of main()'s argc and argv..
char   **gargv;

/* **** Functions ********************************************************** */


#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif

#define INITERR_NOERROR 0
#define INITERR_NOPARAMS 1
#define INITERR_NOHBBSCOMMON 2
#define INITERR_INITCOMMONFAILED 3
#define INITERR_NOHBBSNODE 4
#define INITERR_INITDOORFAILED 5

static VOID CleanUp(ULONG num)
{
  if (HBBSNodeBase)
  {
    HBBS_CleanUpDoor();
    CloseLibrary (HBBSNodeBase);
  }

  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }

  switch(num)
  {
    case INITERR_NOERROR:
      exit(0);

    case INITERR_NOPARAMS:
      puts("Bad or no paramaters specified, check your config files\n"
           "(Don't try starting doors from the CLI!)");
      break;

    case INITERR_NOHBBSCOMMON:
      puts("Could not open HBBSCommon.library, check your assign's");
      break;

    case INITERR_INITCOMMONFAILED:
      puts("Could not initialise HBBSCommon.library functions\n"
           "Is HBBS Running ?");
      break;

    case INITERR_NOHBBSNODE:
      puts("Could not open HBBSNode.library, check your assign's");
      break;

    case INITERR_INITDOORFAILED:
      puts("The node specified is either not running or did not want to\n"
           "start this door now. (Don't run doors from the CLI!)");
      break;
  }
  exit(20);
}

static VOID Init(char *name)
{
  if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  {
    CleanUp(INITERR_NOHBBSCOMMON);
  }

  if (!(HBBS_InitCommon()))
  {
    CleanUp(INITERR_INITCOMMONFAILED);
  }

  if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  {
    CleanUp(INITERR_NOHBBSNODE);
  }

  if (!(HBBS_InitDoor(N_NodeNum,name)))
  {
    CleanUp(INITERR_INITDOORFAILED);
  }
  SetProgramName(name);
}

/* **** DoorMain *********************************************************** */

void DisplayBulletin( char *BullName,V_BOOL Pause)
{
  
  DOOR_WriteText("\014\033[2J\033[1;1H\0"); // clear screen and ansi reset
  
  if (DOOR_DisplayBulletin(BullName))
  {
    // and wait..

    if (Pause) DOOR_PausePrompt(NULL);
  }
  else
  {
    // or display message if bulletin was not found.
    sprintf(outstr,ANSI_FG_YELLOW "The bulletin \"" ANSI_FG_WHITE " %s " ANSI_FG_YELLOW "\" was not found! [Return]",BullName);
    DOOR_ContinuePrompt(outstr,DEFAULT_NONE|DCP_NODISPLAY|DCP_COLOURIZE);
  }
}

void DisplayList( void )
{
  DOOR_DisplaySpecialScreen("Bulletins_TOP");
  DOOR_DisplaySpecialScreen("Bulletins_LIST");
  DOOR_DisplaySpecialScreen("Bulletins_BOT");
}

void DoorMain( void )
{
  BOOL Done=FALSE;
  char *BullName;

  if (N_ND->OnlineStatus==OS_ONLINE)
  {
    if (gargc == 3)
    {
      DisplayBulletin(gargv[2],TRUE);
    }
    else
    {


      DisplayList();
      while (!Done)
      {
        DOOR_MenuPrompt("Name of bulletin or [L]ist Again, [Q]uit :",'Q');
        DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',10,0,NULL);

        if (!(N_ND->OnlineStatus == OS_ONLINE)) // still on-line ?
        {
          Done=TRUE;
        }
        else
        {
          if (BullName=DupStr(N_ND->CurrentLine))
          {
            if (!BullName[0]) // did user just press return ?
            {
              Done=TRUE;
            }
            else
            {
              // strip spaces from start and end of the line
              RemoveSpaces(BullName);

              if (BullName[0]) // blank line after spaces have been removed ?
              {
                // nope, so check for menu options first.

                if ((toupper(BullName[0]) == 'Q') && (BullName[1] == 0))
                {
                  Done=TRUE;
                }
                else
                {
                  if ((toupper(BullName[0]) == 'L') && (BullName[1] == 0))
                  {
                    DisplayList();
                  }
                  else
                  {

                    // no menu option found, attempt to display the bulletin

                    DisplayBulletin(BullName,FALSE);

                  }
                }
              }
            }
            FreeStr(BullName);
          }
        }

      }
    }
  }

}

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

  /* set these so that we can access the paramaters in the rest of the program */

  gargc=argc;
  gargv=argv;

  /* Check to see if we've got any paramaters */

  if (argc <= 1)
  {
    CleanUp(INITERR_NOPARAMS);
  }

  /* Get the node number from the paramaters */

  if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  {
    CleanUp(INITERR_NOPARAMS);
  }

  /* Initialise and set the door/program name */

  Init("BulletinViewer");

  /* if the init() didn't fail then we need to ask HBBS for the pointer to
     it's data structures */

  if (BBSGlobal=HBBS_GimmeBBS())
  {
    /* Then we need to get the node's data structures for the node this door is going to run on */

    if (N_ND=HBBS_NodeDataPtr(N_NodeNum))
    {

      /* Now that's done, you can put the rest of your code here, or in the DoorMain() function */

      DoorMain();
    }
  }

  /* Cleanup and close libraries */

  CleanUp(INITERR_NOERROR);
}

/* **** End Of File ******************************************************** */
