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

struct Library *HBBSCommonBase=NULL;

struct BBSGlobalData *BBSGlobal=NULL;

static VOID cleanup(ULONG num)
{
  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);
  }

  SetProgramName(name);
}

int main(int argc,char *argv[])
{
  LONG loop,tonode;
  char outstr[2048];

  init("SendOLM");

  if (BBSGlobal=HBBS_GimmeBBS())
  {
    outstr[0]=0;
    if (argc>=3)
    {
      if (sscanf(argv[1],"%ld",&tonode))
      {
        if (tonode>=0 && tonode < BBSGlobal->BBSNodes)
        {
          for (loop=2;loop<argc;loop++)
          {
            strcat(outstr,argv[loop]);
            if (loop<argc-1) strcat(outstr," ");
          }

          if (HBBS_SendOLM(0,"SendOLM",tonode,outstr,0))
          {
            puts("OLM Sent");
          }
          else
          {
            puts("OLM Failed - node my be busy, offline or there is no user-online");
          }
        }
      }
    }
    else
    {
      puts("Invalid Parameters! usage: OLM <node> message...\r\n");
    }
  }
  cleanup(0);
}
