#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <stdio.h>
#include "damessage.h"
#include <math.h>

double PI180; /* rad -> deg */

void main()
{
PI180=atan(1)*4/180;
if(!FindPort("DA_Port")) /* Make sure, port doesn't exist twice !! */
	{
	struct MsgPort *daport;
	if(daport=CreateMsgPort()) /* Create the MsgPort */
		{
		ULONG signals;
		daport->mp_Node.ln_Name="DA_Port"; /* The name of the MsgPort and make it public */
		AddPort(daport);
		signals=(1<<daport->mp_SigBit)|SIGBREAKF_CTRL_C; /* Check for MsgPort and User-Break */
		for(;;)
			{
			ULONG sigs;
			struct DAMessage *msg;
			sigs=Wait(signals); /* Wait for messages */
			if(sigs&SIGBREAKF_CTRL_C) /* User break ? Then exit */
				{
				puts("***Break");
				break;
				}
			while(msg=(struct DAMessage *)GetMsg(daport)) /* Get message sent from DA */
				{
				printf("R:%f D:%f\n",msg->dam_RA/PI180,msg->dam_Decl/PI180);
				printf("A:%f H:%f\n\n",msg->dam_Az/PI180,msg->dam_Ho/PI180);
				ReplyMsg((struct Message *)msg); /* Reply the message. Absolutely necessary ! */
				}
			}
		Forbid(); /* Disable multi-tasking to prevent DA accessing the port */
		RemPort(daport); /* Cleanup stuff */
		DeleteMsgPort(daport);
		Permit();
		}
	}
else puts("Error: Public port 'DA_Port' already exists.");
}

void wbmain(struct WBStartup *msg)
{
/*
Warning !! This is a hack to redirect the output to a CON, when started from Workbench !
It works perferct with Maxon C++.
You can easily adapt this part for other compilers.
*/

if(!freopen("CON:////Digital Almanac II - Test Output/CLOSE","r+",stdout)) return;
stdin->Filehandle=stdout->Filehandle;
main();
}
