/*
** Reccoon TimeBank. A short demo of how to use the AppInfo-functions.
**
** Look in TimeBank.h if you want to know how to obtain APP-Keys.
**
** To compile, simply Cd to this directory and "SMake"
**
** Do whatever you want with this program and source.
**
*/

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

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

#include "TimeBank.h"

#include "Rcn_TimeBank.h"  // Created by BumpRev


UBYTE *			ProgramVersion	= VERSTAG;

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

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

UBYTE 			outbuff[100];

int Sluta( int );
int CXBRK(void);

#define puts(x) Write(Output(),x,sizeof(x))

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

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

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

	onexit((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;

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

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

/* Now to the TimeBank */

	TimeBank();

/* We're back, exit */

	exit(0);

}

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

int Sluta(int e)
{


/* Deallocate the memory Reccoon allocated at startup */

	if(doordata){
		ContinueBBS();
		CloseDoor();
	}
	doordata=0;

/* Close ReccControl and reccoon.library */

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


	return(e);
}

int CXBRK(void) { return(0); }

