#ifdef KICK_13
#include <libraries/arpbase.h>
#include <clib/arp_proto.h>
#endif
#include "source:launch/Launch.h"


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

#ifdef KICK_13
struct ArpBase *ArpBase;
#endif

#define F_IN	0
#define F_OUT	1

void main (WORD argc, char *argv[])
{
	extern struct Library *DOSBase;
	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);
	}
#ifdef KICK_13
	ArpBase = (struct ArpBase *)OpenLibrary ("arp.library", 33L);
	if (ArpBase)
#endif
	{
		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", Reader, HC, 1);
				if (Proc1)
				{
					Proc2 = StartProcess ("RAW Server", 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]);
		}
#ifdef KICK_13
		CloseLibrary (ArpBase);
#endif
	}
	exit (0);
}


void Reader (BPTR File[2])
{
	extern struct Library *DOSBase;
	ULONG Buffer;

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