/* 
** Name    : AEDoor.c
** Version : 1.2 
** Date    : 14 Apr 1993 
** Author  : B/\Zz/Eclipse & CFi/Eclipse 
** (c) 1993 by Eclipse. All rights reserved. 
*/ 
 


#include "AEDoor.h"
int	__oslibversion = 37;
void chkabort(void);

/*      Structures                              */
struct  JHMessage       *DoorMsg;
struct  MsgPort         *AEDoorPort, *AEReplyPort;
struct  Process         *DoorProcess;

/*      Prototypes                              */
void	SendCMD(LONG Command,LONG Data,UBYTE *String);
void	SendMsg(void);

/*      Global Vars                             */
UBYTE	NodeNumber;
UBYTE	AEPortName[16]; 
UBYTE	AEReplyName[16];

void main(int argc, char *argv[])
	{
	if (argc != 2)
		exit(RETURN_FAIL);
	
	NodeNumber = argv[1][0];
	sprintf(AEPortName, "AEDoorPort%lc", NodeNumber);
	if (!(AEDoorPort = FindPort(AEPortName)))
    exit(RETURN_FAIL);
	AEReplyPort = CreateMsgPort();
  DoorMsg = AllocVec(sizeof(struct JHMessage), MEMF_PUBLIC | MEMF_CLEAR);
	DoorMsg->Msg.mn_Node.ln_Type = NT_MESSAGE;
	DoorMsg->Msg.mn_ReplyPort = AEReplyPort;
	DoorMsg->Msg.mn_Length = sizeof(struct JHMessage);
	SendCMD(JH_REGISTER,0,DoorMsg->String);
/* you can do your door stuff here */


/* cleanup time                    */
	SendCMD(JH_SHUTDOWN,0,"\0");
	FreeVec(DoorMsg);
	DeleteMsgPort(AEReplyPort);
	exit(RETURN_OK);
	}

/*
** SendCMD - sends an AmiExpress door command to the doorport
** Input : Command = command to execute, Data = data direction,
**				 String = string to send to command
** Output: none
*/

void SendCMD(LONG Command,LONG Data,UBYTE *String)
	{
	DoorMsg->Command = Command;
	DoorMsg->Data = Data;
	strcpy(DoorMsg->String,String);
	SendMsg();
	}

/*
** SendMsg - sends a message to the ami-express door port
** Input :	None
** Output:	None
*/

void SendMsg(void)
	{
	struct Message *Msg = NULL;

	PutMsg(AEDoorPort,(struct Message *)DoorMsg);
	while (Msg != DoorMsg)
		{
		WaitPort(AEReplyPort);
		Msg = GetMsg(AEReplyPort);
		if ((Msg != NULL) && (Msg != DoorMsg))
			ReplyMsg(Msg);
		}
	}
