/*
** Reccoon DOOR template v1.00
**
** A shell for a door, does all the necessary stuff to make it
** work. Now you only have to think about the main coding.
*/

#include <stdio.h>
#include <exec/types.h>
#include <stdlib.h>
#include <string.h>
#include <exec/exec.h>
#include <dos/dos.h>

#define RLIB_MACRO
#include <Reccoon/Recclib.h>
#include <Reccoon/Reccoon2.h>
#include <Reccoon/All.h>
#include <Reccoon/DoorMacro.h>

extern struct DosLibrary *DOSBase;

#define VERSION "1.00"

/* __COMMODORE_DATE__ is Dice-only */

UBYTE *Version = "\0$VER: Door "VERSION" ("__COMMODORE_DATE__")";

struct CtrlMessage  ctrlmsg;
struct control  *   ctrl            = 0;
struct MsgPort  *   ctrlport        = 0;
struct MsgPort  *   ctrlreplyport   = 0;
struct Library  *   RecclibBase     = 0;
struct fig      *   fig             = 0;

void *          doordata=0;
struct DoorCallback *   dcb;
struct InternalData *   id;
struct ReccNode     *   rn;

int Sluta( int );

/*
** These defines simplify door programming
*/

#define print(x)    SendString(x, SS_NOBRK);        // Acts like BASIC's print function
#define cls()       SendString("~ai~", SS_NOBRK);   // Clears the screen
#define random(x)   rand()%x;                       // Returns a random with x as the highest possible

void main(UWORD argc, UBYTE *argv[])
{
UWORD line;

    /* The line number is passed as the last argument to the program */

    if(argc < 2 || argv[argc-1][0]<'0' || argv[argc-1][0]>'9' )
    {
        puts("This is a Rcn door program, and should not be run from the CLI.\n");
        exit(20);
    }
    line=atoi(argv[argc-1]);

    RecclibBase=OpenLibrary("reccoon.library",0);
    if(!RecclibBase)
        exit(20);

    /* Open ReccControl. Always use line number -1 for doors and utilities */

    ctrl=OpenRCtrl(0,65535,&ctrlmsg,&ctrlport,&ctrlreplyport);
    if(!ctrl)
    {
        CloseLibrary(RecclibBase);
        exit(10);
    }

    /* Set an exit trap so the program never exits without cleanup */

    atexit((void *)Sluta);

    /* Find this line's ReccNode structure */

    for(rn=ctrl->reccnodes;rn && rn->line!=line;rn=rn->next)
        ;
    if(!rn || !rn->flags.running)
        exit(10);

    /*
    ** Here we got all callback functions and internal data for this
    ** Reccoon line.
    */

    dcb = rn->doorcallback;
    id  = rn->internaldata;

    /* Get hold of the system configuration structure */

    fig = ctrl->fig;

    /* Reccoon need to allocate some memory etc for your door. */

    doordata = OpenDoor();
    if(!doordata)
        exit(20);

    /* Put your program WITHIN */

    /* these remarks... */

    exit(0);
}

/*
** Ansi Detection routine.
** Returns 1 if user has ansi selected
*/

ansidetect()
{
   struct StringList * string;
   string = id->bstrings;
   if(strlen(string->bstrings[112]) > 0)
      return(1);
   return(0);
}

/*
** Cleanup routine. This routine is always executed when
** this program exits.
*/

int Sluta(int e)
{
    /* Deallocate the memory Reccoon allocated at startup */

    ContinueBBS();

    if(doordata) CloseDoor();
    doordata=0;

    /* Close ReccControl and reccoon.library */

    if(RecclibBase)
    {
        CloseRCtrl(ctrlport,ctrlreplyport,&ctrlmsg);
        CloseLibrary(RecclibBase);
        RecclibBase=0;
    }

    return(e);
}

