#include <arpbase.h>
#include <arp_proto.h>
#include "source:launch/Launch.h"


void main (WORD argc, char *argv[]);
void Reader (BPTR File[2]);


struct ArpBase *ArpBase;

#define F_IN	0
#define F_OUT	1

void main (WORD argc, char *argv[])
{
	struct Process *Proc1, *Proc2;
	BPTR HC[2];
	BPTR RAW[2];

	if (argc < 3)
	{
		puts ("Specify two file to link.\n\nFor example: Loop hc:1 \"raw:0/0/640/100/HC11 numer 1\"");
		exit (5);
	}

	ArpBase = (struct ArpBase *)OpenLibrary ("arp.library", 33L);
	if (ArpBase)
	{
		RAW[F_OUT] = HC[F_IN] = Open (argv[1], MODE_NEWFILE);
		if (HC[F_IN] && IsInteractive ( HC[F_IN] ))
		{
			RAW[F_IN] = HC[F_OUT] = Open (argv[2], MODE_NEWFILE);
			if (HC[F_OUT] && IsInteractive(HC[F_OUT]))
			{
				Proc1 = StartProcess ("HC Server", (CPTR)Reader, HC, 1);
				if (Proc1)
				{
					Proc2 = StartProcess ("RAW Server", (CPTR)Reader, RAW, 1);
					if (Proc2)
					{
						Wait (SIGBREAKF_CTRL_C);
						KillProcess (Proc2);
						Write (RAW[F_IN], (char *)" ", 1);
					}
					KillProcess (Proc1);
					Write (HC[F_IN], (char *)" ", 1);
					WaitProcesses ();
				}
				Close (HC[F_OUT]);
			}
			Close (HC[F_IN]);
		}
		CloseLibrary (ArpBase);
	}
	exit (0);
}


void Reader (BPTR File[2])
{
	ULONG Buffer;

	while (1)
	{
		Read (File[F_IN], &Buffer, 1);
		Write (File[F_OUT], &Buffer, 1);
		if (0 != CheckKill ())
		{
			return;
		}
	}
}
